Listen To Sean Corfield When It Comes To OnMissingMethod()

Posted July 30, 2007 at 8:41 AM by Ben Nadel

Tags: ColdFusion

I recently blogged about the new ColdFusion 8 OnMissingMethod() function that is available to ColdFusion components. I had assumed that it was meant for error handling and that my experiment to use OnMissingMethod() to handle non-existing functions was a silly, to-be-avoided misuse of the new functionality. Sean Corfield, the man who originally asked that this be added to the ColdFusion feature-set, informed me that it was quite the opposite; not only was this not ever meant for error handling, it was specifically added to allow ColdFusion components to handle Getter / Setter methods without ever having to write them.

So, please ignore my blog post and look forward to those by Sean Corfield; he says that he will be writing a good deal on OnMissingMethod() in the future.



Reader Comments

Jul 30, 2007 at 10:37 AM // reply »
79 Comments

The best use is actually how he said it. Think things like: the programmer can define methods that do what they are supposed to just by the way they are named.

So you can have (an ActiveRecord example from Rails):

find_by_name_and_city_and_state(nameToSearchFor,cityToSearchFor,...)

The method doesn't exist, but you define the handler to say something like:

if method_name starts with "find_by"
determine which columns to find by
perform the query with the given arguments
endif

Last night, I was working on a script to let me create objects in a game. I wanted to do things like:

create_small_shiny_with_a_black_leather_hilt_weapon.named("Long Sword") do
damage_of(1.d6 + 2)
...
end

So I used method_missing to define a handler for when the method matches the pattern "create_\w*_weapon" to extract the description from the method name. This allows me to have a cleaner syntax for describing objects in the game.


Jul 30, 2007 at 11:09 AM // reply »
11,314 Comments

@Sam,

It sounds good, and since it is not actually working via Exceptions, it should be fairly fast (minus the overhead of the missing method name evaluation). I am, however, not used to to even coding with Getters / Setters (due to my caveman brain) and look forward to Sean explaining what the best practices are for something like this.


Jul 30, 2007 at 12:12 PM // reply »
8 Comments

Actually, onMissingMethod has no overhead (apart from any processing you do inside it) compared to a standard method.

So go nuts! :)


Aug 1, 2007 at 1:47 AM // reply »
3 Comments

I have mixed emotions regarding using onMissingMethod vs. a code generator and actually creating getter/setter methods. The biggest drawback I see is that you lose the introspective self-documenting aspect. Dump a CFC and you can see all of the methods available. Do a getMetaData and you can walk the list and do your own documentation routines.

Without the code, you're more in the dark. Is it getCustomerNumber() or getCustomerID()?Is it getArticleName() or getArticleTitle()?

Or do I need to write a documentation function that checks the field list and generates virtual documentation for virtual functions?


Aug 1, 2007 at 7:27 AM // reply »
11,314 Comments

@Michael,

I suspect that that is going to be the biggest concern people express for this sort of programming. This type of set up is going to make documentation an essential part of the developer's life (both the developer of such components as well the user). The foggiest area for this, I think, is going to be in a Peter Bell type situation where he might have some base iterating business object (IBO) whose job it is to handle these generic getter / setter methods for composed objects. In that case, documentation would be near impossible as the IBO component you need to use might not even be tied directly to the ultimate functionality. In that case, you would need to know what object is being composed and have its documentation!


Aug 1, 2007 at 11:40 AM // reply »
79 Comments

@Michael, Ben:

I don't share the concern. A static code generator simply doesn't have the ability to generate the same kinds of methods as I've given examples for above.

In the ActiveRecord example above, I suppose technically it could generate every combination of columns for every combination of and/or and in every possible order.

Even given that possibility, do you really want it to? That would clutter your interface and make everything else (the important stuff) hard to find. I'd rather manually add a line to my documentation that says "methods such a find_by_column1_and_column2 are available."

In the second example, there is simply no way you could generate it, aside from the give enough monkeys enough time and they'll write Shakespeare quality plays. Or in other words, generate every combination of characters into words in the English language and in any order...

I don't think such brute force is a feasible solution to either problem. Much better to dynamically generate the missing method.


Aug 1, 2007 at 12:09 PM // reply »
11,314 Comments

@Sam,

I am in no position to argue for or against dynamic method handling as I really have not experience with this. I guess, the point is that leveraging the dynamic stuff is great, so long as there is documentation about how it can / should be used.


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Jun 19, 2013 at 11:31 PM
Directive Link, $observe, And $watch Functions Execute Inside An AngularJS Context
@Ben, bunch to learn indeed, but thats fun part : ) ... read »
Jun 19, 2013 at 10:41 PM
Referencing ColdFusion Query Columns In A Loop Using Both Array And Dot Notation
Burdock-roots Are you going fat day by day? You need to be good for your family and make some money too. So we bring for you a best product that helps you to be more energetic every day. You will b ... read »
Jun 19, 2013 at 9:52 PM
Working With Inherited Collections In AngularJS
I recognize the applicability of your solution, and how easy it makes to share data across multiple views or even "submodules" of rather simple application. But it seems to me that it creat ... read »
Jun 19, 2013 at 9:38 PM
Directive Link, $observe, And $watch Functions Execute Inside An AngularJS Context
@Alesei, Glad you like it. Even after working with AngularJS for months, I still get a bunch of unexpected, "$digest is already in progress". So hard to debug sometimes! ... read »
Jun 19, 2013 at 9:36 PM
Working With Inherited Collections In AngularJS
@Mike, The relationship of $scope values is definitely an interesting thing! But it's not simple - it really forces you to understand prototypal inheritance, which is not at all a simple topic! Gla ... read »
Jun 19, 2013 at 9:35 PM
Experimenting With The Amazon Simple Storage Service (S3) API Using ColdFusion
@Joe, Oh, super interesting! I had only thought to url-encode the signature; but I think that's because the S3 docs actually have a special NOTE telling you to do so. It would have never occurred t ... read »
Jun 19, 2013 at 9:32 PM
Experimenting With The Amazon Simple Storage Service (S3) API Using ColdFusion
@Richard, Glad you like! Hopefully I'll have some more interesting stuff coming. This morning, I blogged a bit more about generating the pre-signed, query string authenticated URLs; but, then deeme ... read »
Jun 19, 2013 at 9:31 PM
Filter vs. ngHide With ngRepeat In AngularJS
@Mike, Honestly, in the majority of cases, I would say there isn't going to be a difference. Both approaches have trade-offs. If you use the filter, then you have fewer DOM elements and fewer $scop ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools