Division of Presentation and Logic

by Stephen Fluin 2009.05.15

One of the most important concepts in programming is the separation of Presentation and Logic. There are a lot of ways to accomplish this. The design pattern known as Model View Controller, or MVC is an example of this where you divide your program into three parts. Using this pattern allows you to ensure that the model contains all of the logic and data for the universe of your program, the view allows users to access and present this data, and the controller allows the user to provide updates to the model.

Ways to accomplish the separation of presentation and logic

Templating

Templating is a standard used in many languages. If you use Mediawiki, CakePHP, ASP.NET, or even a custom framework, you are taking advantage of the power of templating.

With the concept of templating you are creating layers within an application. These layers create an abstraction barrier that allow you to make decisions at one layer that have less of an impact on the other layers of your application. For example, when you use .NET and you write a .aspx page, you are able to use controls, elements, and references to other parts of your applications. In this example, by writing methods and creating custom objects in .NET separate from the specific page or interaction you are working on, you are later able to rearrange or customize these interactions, without needing to see or worry about how they function.

Game Design / GUI Applications

The standard loop used in game design includes three components. These components are, Collect User Input, Update Game State, and Render to the screen. Most new game designers know that if you do not follow a loop structure like this, you end up with issues where user input blocks game progress, or where the game can become descynchronized so that updates from multiple players aren't handled at the right times, or where threads enter race conditions because visual updates are conflicting with logical updates.

Problems with doing so

The main problem with following a practice of dividing presentation and logic is that it is more difficult to start out doing things properly. Even though this division will definitely save a lot of time in the middle and end of a project, it can seem like overkill to ensure that you implement this properly at the beginning. If you don't do this, however, it is likely that you will make mistakes in the architecture of the application, and that the model you have in your head will be impossible to effectively translate into code.

Summary

It all comes back to the standard practice of setting up appropriate abstraction barriers and establishing a division of responsibility between different parts of the code. Keeping presentation and logic as separate as possible helps the human brain focus on a single part at a time, and makes the entire software development process easier.


permalink