One of the most important organizational rules to follow is keeping what is known as your user interface and business logic separate. In this lesson we'll explore what user interface and business logics are, and what each handles.
Consider a basic calculator application. Its code falls into two categories: Code that performs calculations, or code responsible for interacting with the user.
The code that handles arithmetic is the business logic. It's the 'inner workings' of the application that performs calculations and returns a value. It's what takes the numbers
5, adds them together, and arrives at
add() function we've created is a business logic function.
The code that handles interacting with users is the user interface logic. It retrieves and displays information from the user and provides it to the business logic to calculate. While buttons on a calculator may be labeled with numbers, they're just visual buttons. User interface logic is what translates clicking on this area of the page:
... into the number
4. After all, we cannot perform addition on buttons, but you can perform addition on numbers. The user interface logic registers that the user has pushed the button labeled "4". It then provides the number
4 to the business logic where we may perform calculations with it.
Let's say we also press the buttons labeled "+" and "5". The user interface logic also translates these interactions into the number
5 and recognizes it will need a method for addition. The business logic then adds the numbers
5 together, and returns
9. The user interface logic can then display this result to the user:
User interface logic handles interacting with the user including displaying or gathering information. The business logic handles calculating or manipulating information behind the scenes.
Our goal is to write clean, well-organized code. Because user interface and business logic have separate purposes, their code should always be separate. Keep this rule in mind as we begin to explore event handling in the coming lessons.
You may be wondering about
window.alert(). These are both considered user interface logic, because they handle gathering and displaying information to the user. However,
window.alert() really shouldn't be in our code at all — very few users like to be prompted or alerted (and both now have strong associations with hacky sites and malware). We've been using these
window methods because they are the easiest way for beginners to get a user response, and we will continue to use them for a little while longer because they are so easy to use — but be aware they should generally be avoided.
Business logic: The code responsible for handling the evaluation and manipulation of data; does not require any user interface.
User interface logic: The code responsible for the interaction between the user and the application; handles tasks such as event listening, user input forms, DOM manipulation, display and styling. We haven't learned how to write the actual code for this (except via
window methods), but we will soon!
Because they are responsible for entirely separate things, code for business logic and user interface logic should always be separated.