September 30th, 2008 · Posted by Tejus Parikh ·

The Separation of Content and Presentation

The long-held best practice is that an ideally structured website will have it’s content completely separated form it’s presentation. For websites, there’s no fault I can find in this approach. After all, you can change a lot about the site through some simple css tricks, stylesheets give you the most tools to work around browser bugs, and inline javascript is ugly and inflexible.

However, the situation changes when you stop making web-sites and start using the web to build applications. For one, markup is no longer there to suggest to a browser “hey I think this should be bold.” It defines necessary functionality for user interaction. Elements become interactive controls.

This doesn’t mean you can’t separate out the control functionality from presentation, but more that it doesn’t make sense. The fundamental reason for separation of concerns is to allow one aspect to change without the other. However, this no longer holds true. A pretty common semantic refactor is changing the ID of an element because it’s name no longer describes what it does. In JQuery, this means you’ll a situation like this.

<script type="text/javascript" charset="utf-8">
    function attach() {
        $('#submit_button').click(function() { /* do some nasty js for ajax */ });
    }
</script>
 
<button id="submit_button">Request More Info</button>

“Submit_button” is a pretty crappy name, so when you change it, you need to change two different places. The concerns aren’t so separate after all.

With a modern, interactive web application, it makes sense to put the functionality inline with the markup around the content. It’s how you would define things in most thick-clients anyway. It reduces the amount of problems that can arise through simple refactors. It’s also a cleaner for the code-base, since it allows one to move elements around without the need for a monstrous, custom-functions library.

Popularity: 4% [?]

Tags: Opinion

2 Responses to “The Separation of Content and Presentation”

  1. Matthew Quinlan Says:

    Indeed. Good insights on the priorities of web site design versus web application design. Readability (ability to read the code and understand what it does) and maintainability overtake the separation of code & content concerns.

  2. Rusty Says:

    I respectfully disagree. The reason for the separation of presentation from behavior is so that you are empowered to change behavior without mucking around with the biz logic. I agree that “submit_button” is a terrible name but that’s not what you referred to. You referred to the id. Identifiers are not meant to be meaningful, they are for identifying the item in a group of similar or dissimilar items. If the id were, instead, “kjhgfvhkagvkgbw12″, then the issue you describe is a moot point. Why would you change the id anyway?

    By committing to standards compliant, simple xhtml and style exclusively using css and a little js sugar, reuse goes way up. formatting issues are easier to resolve. Code is MUCH clearer when I can focus on html and ignore the js or vice versa.

    In my opinion, websites should be managed using cms systems. Web applications need to delineate lines of responsibility clearly so that future maintenance and enhancement continue to be accessible and not complicated by unstructured spaghetti code.

    Html is a mark up script that is designed for display. Javascript is a powerful dynamic language capable of sophisticated object oriented design. Take advantage of both.

Leave a Reply