Structuring CFCs a little like Ruby
Tuesday, May 18th, 2010As I hit the books and learned about Ruby on Rails a couple years ago, there was one thing that I admired about how Ruby handles public and private sections of classes.
Take this, for example:
class PostsController < ApplicationController before_filter :find_post, :only => [:show, :edit, :update, :destroy] def show # ... end def edit # ... end def update # ... end def destroy # ... end private def find_post @post = Post.find(params[:id]) end end
Using the private keyword, the interpreter knows that all methods defined in the section afterward are private. It’s a pretty clean way of demarcating different blocks of code that have different roles.
While we have no such set of keywords for CFML except at the function level, why not use comments to do similar structuring of your CFCs?
<cfcomponent extends="Controller"> <!-------------------------------------> <!--- Public ---> <cffunction name="init"> <cfset filters(through="findPost, only="show,edit,update,destroy")> </cffunction> <cffunction name="show"> <!--- ... ---> </cffunction> <cffunction name="edit"> <!--- ... ---> </cffunction> <cffunction name="update"> <!--- ... ---> </cffunction> <cffunction name="destroy"> <!--- ... ---> </cffunction> <!-------------------------------------> <!--- Private ---> <cffunction name="findPost" access="private"> <cfset post = model("post").findByKey(params.key)> </cffunction> </cfcomponent>
A perfect translation? No. But an idea on how to use some simple comments to make your life easier when maintaining your code in the future.
I’ve taken it a step farther and also have been creating “Filters” sections for filters, just before “Private” methods.



May 18th, 2010 at 11:58 pm
Is grouping your methods by access-level really the best organizational scheme?
Since you must define the access-level in each method in CF anyway, why not take the liberty of organizing your methods in whatever way makes the most sense for the project?
You’ll probably end up using CTRL-F to find specific methods later anyways.
May 19th, 2010 at 12:08 am
Yeah it’s all subjective really isn’t it, and can rely on the project.
I do like to take this approach though with commenting. I like to lay out sections of my CFC in ways that are logically grouped so that I can easily find them. Groupings might be: Accessors, Modifiers, Remote (+facades), Public, Private. It’s all about what works right
Also, I personally think a lot of programmers still undervalue time invested in code comments.
May 19th, 2010 at 5:52 am
Russ, you’re right in that my personal approach does organize things in a manner that suits Wheels’s structure well.
I think that having some clearly marked sections is important, and I just thought that it was pretty cool that Ruby has a mechanism for one approach built into their language. I argue that you always want private methods at the bottom because they should be of less concern to people using your classes, so this works well.
Here’s how I’m currently organizing controllers and models in Wheels:
Controllers
init()
Actions sorted alphabetically
Filter methods sorted alphabetically
Private methods sorted alphabetically
Models
init()
Public methods sorted alphabetically
Validation handlers sorted alphabetically
Callback handlers sorted alphabetically
Private methods sorted alphabetically
Steve, I’ll note that if you’re sorting by accessors (get) and then modifiers (set), then you’re probably already sorting at least pseudo alphabetically.
May 19th, 2010 at 6:25 am
This is not all that revolutionary. I’ve typically laid out my CFC’s in this fashion because that is how I learned to do it in C++ 14 some years ago.
May 19th, 2010 at 9:40 am
Yep. I organize mine by access and then alphabetically. It just makes the most sense that way and keeps things easy to find. It is nice to know that is a common approach elsewhere as well.
May 19th, 2010 at 9:47 am
WilGeno,
Good for you. I’ve done this sort of thing in different ways over the past 10 years, but I’ve never really put much thought into it. Just exploring the space.
May 20th, 2010 at 9:11 am
I use similar layout rules for my CFCs with Getters / Setters at the top, public,
private and then the constructor at the bottom. Keeps it all nice and ordered and with Code Folding if CFEclipse I can jump straight to the method I want to edit, inspect.