Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
I am the chief technical officer at InVision App, Inc - a prototyping and collaboration platform for designers, built by designers. I also rock out in JavaScript and ColdFusion 24x7.
Meanwhile on Twitter
Loading latest tweet...
Ben Nadel at the Stammari Suberbowl XLIV Party (Feb. 2010) with:

ColdFusion Method Definitions Can Come After The Code That References Them

By Ben Nadel on
Tags: ColdFusion

Well, not always, but sometimes. I always thing about ColdFusion as a top-down processing system. And, it is. But what I forget is that the page gets compiled before it gets run. Because of that (or at least this is what I am assuming), ColdFusion methods that are declared within a given template can come AFTER the code that references them so long as that reference is also in the same template.

In other words, this runs perfectly well:

  • <!--- Get some reassurance. --->
  • #GetReassurance( "Am I good enough?" )#
  •  
  • <cffunction
  • name="GetReassurance"
  • access="public"
  • returntype="string"
  • output="false"
  • hint="Reassures you that you are good enough.">
  •  
  • <cfreturn "Look dude, you are the man! Don't fight it." />
  • </cffunction>

Logically, I would expect that page to fail as the line that calls GetReassurance() is referring to a user defined method that has not yet been defined. But, since this method gets compiled with the page, I guess same-template references do not require a particular order.

If the ColdFusion user defined method was put into a CFInclude, this would fail. This only works if the method is defined IN THE SAME PAGE.



Reader Comments

Right you are. In fact, even if you put a cfabort after your udf call and before your udf definition, I believe your call well execute successfully under most circumstances. Obviously if your udf references global variables defined after the cfabort, that probably wouldn't be the case.

Reply to this Comment

That is cool! I would have assumed that wouldn't have worked. I wonder if ColdFusion loads UDFs in the parsing stage?

Reply to this Comment

I think UDFs get loaded as actual Java classes right? I guess they get compiled down to separate java byte code before the pages starts running. Of course I know nothing about that aspect, so I may be way off the mark.

Reply to this Comment

Ben, I think you're right. I know that is the case with functions in CFCs, so I would think it would be similar for plain ol' CFM files.

Reply to this Comment

Post A Comment

You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
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.