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:
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:
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!


I wonder what the same code returns in Railo though..
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.
<cfreturn user.getRole().getRoleName() and such.
It's really easy to leave one out in the middle somewhere.
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>
[Add Comment] [Subscribe to Comments]