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,238 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,238 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,238 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
May 21, 2013 at 9:25 AM
Turning Off and On Identity Column in SQL Server
you are awesome..i am lucky to get this blog between such a garbage one....Thanks, Prashant ... read »
May 20, 2013 at 4:38 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Dana, Your confusion is well founded, since this is a very confusing features. In fact, it ONLY works if you use array notation. Meaning, that this: arrayToList( query[ "columnName" ] ) ... read »
May 20, 2013 at 4:34 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
I was thinking chicken and the egg, I wouldn't have expected it to work in the valuelist going in I guess. Maybe I just need a beer, long day :) ... read »
May 20, 2013 at 4:29 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Dana, That's if you're trying to reference a specific row. In this case, we're trying to reference the entire query column as one cohesive value. So, you are correct that if you wanted to output a ... read »
May 20, 2013 at 4:24 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
I thought when you used array notation to reference queries you always had to have the row or it would throw a similar error as well? ... read »
May 20, 2013 at 11:45 AM
Using jQuery's Animate() Step Callback Function To Create Custom Animations
This is really useful. I found out that you don't actually have to use a dummy css property (surprisingly). To animate a property in a linear-gradient for instance I did this this.css('someLinearGra ... read »
May 20, 2013 at 10:51 AM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Josh, Oh snap! You're totally right! I'm not sure I've ever tried that. I did know that you can call a number of other array-methods on ColdFusion query columns: http://www.bennadel.com/blog/167 ... read »
May 20, 2013 at 10:45 AM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Ben - I believe you can achieve the same functionality with ColdFusion's built in ArrayToList() function. ArrayToList( users[ "id" ] ); ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools