Raymond Camden's Blog Rss

Calling a function... did you really call it?

11

Posted in ColdFusion | Posted on 06-25-2009 | 2,908 views

Just ran across an interesting bug in some code I'm reviewing. I've done this myself a few times so I certainly don't blame the original developer. Read this real quick and tell me what the output is:

view plain print about
1<cffunction name="returnNothing" output="false" returnType="string" hint="Ask Paris Hilton about WW2.">
2    <cfreturn "">
3</cffunction>
4
5<cfif len(returnNothing)>
6    Yes, this ran, and it never should!
7<cfelse>
8    Perfect - I got nothing back. As I expected.
9</cfif>

If you said "Perfect...", then you are incorrect. Notice the cfif. We called the UDF, and it should have returned a blank string, right? Well, we didn't actually call the UDF. See the missing ()?

So why did len() return true? You can see this for yourself. Running:

view plain print about
1<cfoutput>#returnNothing#</cfoutput>

outputs:

cftest22ecfm1623321220$funcRETURNNOTHING@76e45ffe

Which I believe is simply a toString version of the UDF. (Actually I compared it to #toString(returnNothing)# and I got the same thing.)

So - raise your hand if you've made this mistake as well!

Comments

[Add Comment] [Subscribe to Comments]

actually, I haven't! I have just made every other mistake in the book besides this one
Yeah, the fact that functions are objects in ColdFusion is the source of many little errors that I've made over the years. Sometimes very subtle ones too...

I wonder what the same code returns in Railo though..
I'm sure I have made the same mistake in the past, but it was refreshing to know I spotted the error first time reading the code :)
I, for one, have never made that mistake. And for the record, "I'm shocked, shocked to find that gambling is going on in here!"
I'll admit I've done this before, but probably more so in JavaScript than in CF.
I was just muttering to myself the other day that I have to stop forgetting to leave the "()" off the end of my Transfer function calls ("user.getFirstName" instead of "user.getLastName()"). :)

I think it's because in my mind I associate those ORM functions with the query-based variables I still use when looping through query results.
The code in question actually _was_ a Transfer-based call.
Interesting - would never have thought to do it that way to begin with. Great question/example!
Transfer calls are where I always make that mistake, too, especially when chaining composed objects:

<cfreturn user.getRole().getRoleName() and such.

It's really easy to leave one out in the middle somewhere.
Oh most certainly. Back in the day I don't think there was a ColdFusion mistake that I have written.

This also reminds me of another issue I found just recently while reviewing someone code. Going to use the same example above to illustrate.

<cfif len("returnNothing")>
Yes, this ran, and it never should!
<cfelse>
Perfect - I got nothing back. As I expected.
</cfif>
Nope. Never done that.

[Add Comment] [Subscribe to Comments]