A Handful of Code

In my last post about page controllers, Jason Holden asked on which pages I use page controllers. This is a great question and speaks to a larger issue of code organization that applies to CSS and JavaScript as well.

When I have JavaScript or CSS that is used on only one page, it doesn't make sense to put it in the .js or .css files that are called on every page of that site. Over time, that behavior would make those files unnecessarily large as well as making the code more difficult to find from the page.

If I have a sample-page.cfm, then I can either put the CSS into a <style> element on the page or in a sample-page.cfc. The same goes for JavaScript (or even controller logic).

I don't want to create an external file every time I need a single line of CSS or JavaScript, but neither do I want to have lines upon lines of non-content code in my file.

The balance that I have chosen to strike is 5 lines of code in my file. I will allow myself five lines of code that could otherwise be in an external file before I actually create one. This is an arbitrary guideline, but it helps me to have a built in rule to decide when to move code to an external file.

This applies to controller logic as well. I don't ever have queries in my .cfm files, but I do sometimes interact with model components. This is really controller behavior, but so long as I have fewer than five lines of it, I leave it in the file. If I have more than that, then I create a page controller.

I think this sort of guideline leads to code that is much easier to manage than a guideline that always insists on an external file (or one that never uses one).

This does not, however, justify breaking encapsulation for any number of lines.

I have been using this rule for about a year now and I have been really happy with it.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
I've been using your PageController ideas since your last post. I really like how clean it makes my view page, but it seems like readability/manageability is the major advantage. I haven't found an instance where code in my controller would have broken encapsulation if it were placed directly into my view page. What do you think?
# Posted By Jason Holden | 4/13/08 6:48 PM
Jason,

I think most of the things that you could put in a view but might go in a page controller rightly belong in the controller rather than the view (as JavaScript belongs in a .js file and CSS belongs in a .css file).

I don't think I would exactly call that an encapsulation issue though, perhaps more of an issue of separation of concerns.

The only reason I mentioned encapsulation is that I don't think the "Handful of code" rule should be used to justify breaking encapsulation. For example, I don't think it should be used to justify using shared-scope variables in a model component.
# Posted By Steve Bryant | 4/14/08 2:21 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.