Lesson Tuesday

Well-organized code is easier to debug, it appears more professional, and is easier to revisit later. Writing organized code is a highly-valued skill in the programming field. When working on a team, you'll often collaborate with other developers. Clean code is easier for others to understand and contribute to. Before we begin writing more complex JavaScript, let's make sure we understand how to organize our code using best, most professional practices from the very beginning.

One of the most important organizational rules to follow is keeping what is known as your user interface and business logic separate. We haven't written any user interface logic yet, but we will in upcoming lessons. In this lesson we'll explore what user interface and business logics are, and what each handles.

User Interface and Business Logics

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 back-end, or business logic. It's the 'inner workings' of the application that performs calculations and returns a value. It's what takes the numbers 4 and 5, adds them together, and arrives at 9. The functions we've explored so far are all back-end logic.

The code that handles interacting with user is the front-end or user interface logic. It retrieves 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:

button-on-calculator

... 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 labelled "4". It then provides the number 4 to the back-end logic where we may perform calculations with it.

Let's say we also press the buttons labelled "+" and "5". The user interface logic also translates these interactions into the number 5 and recognizes it will need a method for addition. The back-end logic then adds the numbers 4 and 5 together, and returns 9. The front-end/user interface logic can then display this result to the user:

result-from-calculator

User interface/front-end logic handles interacting with the user; including displaying or gathering information. The business/back-end logic handles calculating or manipulation information 'behind the scenes'.

Separation of Logic

Remember, we want to write clean, well-organized code. Because user interface and business logics have separate purposes, their code should always be separate. So far, we've only written business logic, so we don't have much to worry about. But keep this rule in mind as we begin to explore user interface logic with jQuery in the coming lessons.

Also, it's okay if everything we discussed isn't entirely clear right now. Just understand the basic differences between these two logics, and know they should be separate. Throughout the week we will see what this actually looks like in practice. By keeping this basic information in mind before we write more complex JavaScript, you'll learn the most professional practices from the very beginning!

Terminology


  • 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 have not covered how to write the actual code for this yet; don't worry!

Summary


Because they are responsible for entirely separate things, code for business logic and user interface logic should always be separated. We have not yet written any applications that use both; but as we explore jQuery in the upcoming lessons, always keep this rule in mind.