The Magic of 1
Yesterday I was doing some training and my student asked me to explain the logic behind this code:
<cfif 1>
<cfdump var="#dharma#">
</cfif>
It was something so obvious to me that it never occurred to me that it may be confusing, and looking at it with a fresh eye I can definitely see it doesn't make much sense. Here is the method behind my madness...
Laziness. We've all had cases where we are working on a page and we need to quickly add some debug code to a page. Typically we just type it in like so:
<cfdump var="#dharma#">
Then if we want to hide it, but we know we aren't quite done yet, we replace it with this:
<!---
<cfdump var="#dharma#">
--->
So now to go back and forth we can add and remove the CFML comments. Well I'm lazy. I pride myself on my laziness. I don't want to have to add/remove CFML comments. Plus I tend to typo that particular set of code. So I will often use the cfif 1 code instead. When I want to hide it, I switch to:
<cfif 0>
<cfdump var="#dharma#">
</cfif>
In case folks don't get what the 1 and 0 means, they are shorthand for true and false. ColdFusion considers any non-zero number to be true.
Comments
<cfset update_not_required = false>
... some more code ...
<cfif NOT update_not_required is false>
... do something ...
</cfif>
I still find that confusing today ;-)
I still run into these legacy snippets all over the place from time to time lol.
<cfif true>
... do stuff ....
</cfif>
<cfif (x eq 10) AND false>
... do stuff ....
</cfif>
I really wish that Adobe would do the same thing with cfdump as they did with cftrace. Make it so that if you have debugging turned on in the adiministrator it will show and ignore it if it's not turned on. This way we could leave it in and not have to go through all the switching crap.
You also can't think a simple evaluation of true or false (1 or 0) is hard to read. I'm sure Ray was pointing it out as a tip for beginners (don't mean to speak for you Ray)...
Haven't you ever done:
<cfif query.recordCount>
Same premise and hardly unreadable.
Then use <cfif dbLevel LT n> whatever </cfif>.
You can scatter debugging code with different levels in the <cfif>. I use 1 to 10, where 1 means I always want to see the information when I am debugging and 10 means that I'm desperate.
I leave the code in and set dbLevel to 0. Whenever I need to debug I start with 1 and work my way up.
Thanks for inspiring creative experimentation in the indulgence of laziness. :-)
<cfif 1 eq 1>
and
<cfif 1 eq 0>
For commonly-needed dumps, like dumps of the form or session struct, I typically would embed cfdumps in my navigation template, which would display the dumps only when the app is in development mode. HOWEVER, with ColdFire, that won't be necessary anymore, will it?? ;-D
@Ben: You know, for the longest time, I didn't even realize CF would evaluate expressions in parentheses to boolean values for assignments or conditional expressions. Being of the C++ vintage, where we love to fit as much code into as compact a space as possible, I was very happy when I realized CF supported that. :-)
Thanks for bearing with me. It's been a long day.


someone I worked with used to always add an application.devMode and wrap his dumps around that.
Another person I worked with at one point swore by wrapping all dumps in a structkeyexists(url, "debug") etc.
Same exact scenario, just different thought patterns on it I guess :)