Dragons in the Algorithm
Adventures in Programming
by Michael Chermside

Separation of Concerns

Once upon a time (in the dark ages of web application development) we built our applications as a single monolithic Perl CGI script, or perhaps a large JSP file containing the entire application. The code looked something like this:

costs.jsp

<% Cost[] costs = CostHelper.calculateCosts(loanData, currentDecision); %>
Current Costs:


    Cost
    Amount

  <% for(int i=0; i

      <%costs[i].getName()%>
      $ <%costs[i].getValue()%>

  <% } %>


  Explain these

And it was confusing. But after a time we became enlightened and we realized that we should separate out the business logic from the rest of the presentation. We used tag libraries or velocity macros or any of a host of other technologies, and the code looked something like this:

CostController.java

Cost[] costs = CostHelper.calculateCosts(loanData, currentDecision);
modelAndView.addObject("costs", costs);

costs.vm

Current Costs:


    Cost
    Amount

  #foreach( $cost in $costs )

      $cost.name
      $ $cost.value

  #end


  Explain these

And it was confusing. But after a time we became enlightened and we realized that we should separate out the styling from the rest of the presentation. We used CSS markup in separate files, and the code looked something like this:

CostController.java

Cost[] costs = CostHelper.calculateCosts(loanData, currentDecision);
modelAndView.addObject("costs", costs);

costs.css

.costName {
  font-weight: bold;
}
.helpLink {
  text-align: center;
  font-style: italic;
}

costs.vm

Current Costs:


    Cost
    Amount

  #foreach( $cost in $costs )

      $cost.name
      $ $cost.value

  #end


  Explain these

And it was confusing. But after a time we became enlightened and we realized that we should separate out the dynamic JavaScript from the rest of the presentation. We called this unobtrusive JavaScript, and the code looked something like this:

CostController.java

Cost[] costs = CostHelper.calculateCosts(loanData, currentDecision);
modelAndView.addObject("costs", costs);

costs.css

.costName {
  font-weight: bold;
}
.helpLink {
  text-align: center;
  font-style: italic;
}

costs.js

$(function() {
  $('.popup').click(function() {
    window.open( $(this).attr('href'), 'Help');
    return false;
  });
})

costs.vm

Current Costs:


    Cost
    Amount

  #foreach( $cost in $costs )

      $cost.name
      $ $cost.value

  #end


  Explain these

And it was better. Things get a little bit confusing because each screen is rendered from several different files instead of from a single one, but separating out the concerns of business logic, styling, and dynamic behavior made each piece easier to understand and work with.

Recently, though, I became enlightened, and realized that we still have too many things going on in the same file. The HTML file still contains two independent concerns: the definition of the structure and the actual text. These facets are not really related, and they tend to be edited by different people. (HTML designers don't necessarily produce the marketing copy and other text.) So this is a perfect candidate for another separation of concerns. That's why my current project is organized like this instead:

CostController.java

Cost[] costs = CostHelper.calculateCosts(loanData, currentDecision);
modelAndView.addObject("costs", costs);

costs.css

.costName {
  font-weight: bold;
}
.helpLink {
  text-align: center;
  font-style: italic;
}

costs.js

$(function() {
  $('.popup').click(function() {
    window.open( $(this).attr('href'), 'Help');
    return false;
  });
})

cmstext.properties

cost-title: Current Costs:
cost-col1-label: Cost
cost-col2-label: Amount
cost-help: Explain these

costs.vm

#cms("cost-title")


    #cms("cost-col1-label")
    #cms("cost-col2-label")

  #foreach( $cost in $costs )

      $cost.name
      $ $cost.value

  #end


  #cms("cost-help")

The code that processes this allows no markup in the cmstext.properties file -- any metacharacters will be properly escaped. So in principle it would be easy to give this file directly to the business to edit without IT help -- or even to allow them to edit it on the live website without needing a QA cycle. The down-side is that we have yet another file to deal with: time will tell whether the benefits outweigh that cost.

Posted Mon 27 October 2008 by mcherm in Uncategorized