ColdFusion Method Definitions Can Come After The Code That References Them

Posted February 20, 2007 at 8:48 AM

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:

 Launch code in new window » Download code as text file »

  • <!--- 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.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Feb 20, 2007 at 9:44 AM // reply »
30 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.


Feb 20, 2007 at 9:45 AM // reply »
30 Comments

That should be "your call will execute." I haven't had my coffee yet this morning. :-)


Feb 20, 2007 at 10:02 AM // reply »
36 Comments

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


Feb 20, 2007 at 10:09 AM // reply »
7,572 Comments

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.


Feb 20, 2007 at 10:21 AM // reply »
30 Comments

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.


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 20, 2010 at 12:07 PM
Drawing On The iPhone Canvas With jQuery And ColdFusion
Simply awesome. Saved my day. ... read »
Mar 20, 2010 at 9:00 AM
Building A Fixed-Position Bottom Menu Bar (ala FaceBook)
I would like to say thx for an easy way to create a bottom bar. I do have a ?. Is it possible to center the bar if i want to resize it to ex 85%. Regards Offenbach ... read »
Mar 19, 2010 at 7:26 PM
MySQL 3/4 - com.mysql.jdbc.Driver And allowMultiQueries=true
Thank you very much for this post. Adding allowMultiQueries="true" in context.xml didn't help until I added it to url as allowMultiQueries=true Good idea is to use prepared statements and it will he ... read »
Jim
Mar 19, 2010 at 4:49 PM
Nobody Puts Baby In The Corner!
Wow. This is like suddenly finding a support group for your secret shame. I'm not alone! I always liked this movie, even though it is extremely cheesy. I just wish Jennifer Grey hadn't gotten the ... read »
Mar 19, 2010 at 4:47 PM
Application.cfc OnRequest() Method Affects OnError() Arguments
@Jason and @Ben, I've been doing some CF9 refactoring on our systems and noticed an odd occurrence with onError as well. Found a way to work around my problem, but what I saw was... Background: Our ... read »
Jim
Mar 19, 2010 at 4:44 PM
Shoot 'Em Up Starring Clive Owen And Paul Giamatti
I actually enjoyed this movie quite a lot. It was different, certainly, but I think they were going for more of a Quentin Tarentino-"wow, that was weird"-vibe than an actual spoof. Once I realize ... read »
Mar 19, 2010 at 4:34 PM
An Intensive Exploration Of jQuery With Ben Nadel (Video Presentation)
Hey I guess the video is down. Is there anyway you can upload to youtube or vimeo or some other service? Greatly appreciated. ... read »
Mar 19, 2010 at 4:24 PM
ColdFusion CFPOP - My First Look
@Ben Thanks for the follow up! The root of the problem had to do with being able to trace bounced emails to specific records in a DB table. Let's say you run an email campaign and you get 1,000 bou ... read »