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

DK's Gravatar when I'm testing code a lot of times I'll paste 'and 1 eq 0' etc in a conditional to test the other side of the if statement

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 :)
# Posted By DK | 3/20/07 10:25 AM
todd sharp's Gravatar If you're going to go that route DK (IE dump everything if you're in dev mode) then why not just use isDebugMode() instead?
# Posted By todd sharp | 3/20/07 10:34 AM
Darren's Gravatar I once had a student write a piece of code like this:

<cfset update_not_required = false>

... some more code ...

<cfif NOT update_not_required is false>

... do something ...

</cfif>


I still find that confusing today ;-)
# Posted By Darren | 3/20/07 10:47 AM
DK's Gravatar todd - it wasn't really dumping everything. They used it solely for one offs and stuff like that. It was also used not just for dumps but to run other smippets etc. I didn't say it was ideal, just thats how other peoples brain processed a similar concept :D

I still run into these legacy snippets all over the place from time to time lol.
# Posted By DK | 3/20/07 10:53 AM
Ben Nadel's Gravatar I do that all the time. However, it might be more clear to just use the true/false values.

<cfif true>
... do stuff ....
</cfif>

<cfif (x eq 10) AND false>
... do stuff ....
</cfif>
# Posted By Ben Nadel | 3/20/07 11:21 AM
Edward T.'s Gravatar I love using the variable approach in those switches. It just helps when you have redirects, buried includes, redirects, etc. Hey, it's not a comprehensive framework for AoP, right? Just quick debugging....
# Posted By Edward T. | 3/20/07 11:25 AM
Sam's Gravatar I like to have a boolean variable, say "development_mode" or "debug_mode", and wrap stuff like that in there. That way, I just go to one place and turn debug_mode on or off for the entire application. Less stuff to change if you need to examine more than one place, which I often find myself doing...
# Posted By Sam | 3/20/07 11:58 AM
Tony Petruzzi's Gravatar I always do application.settings.testmode and make it true or false. Same thing as devmode and what not.

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.
# Posted By Tony Petruzzi | 3/20/07 12:34 PM
Matt Osbun's Gravatar I worked at a place that used a combination of the request IP address and the server that served the request to set an application mode of Production (one of the clustered production servers serving a request from the internet), Test (any request made to the test web server), or Development (a development machine serving a request, or a production server serving a request that originated from itself). We would then use the Application Mode for variable dumps, email addresses and subjects, alerts, and probably a lot of other things that I've managed to forget already.
# Posted By Matt Osbun | 3/20/07 12:38 PM
Mark Shepherd's Gravatar I can see two small advantages of commenting out your debug code, rather than enclosing in <CFIF 0>: (1) the CFIF tag must be evaluated at runtime, which slows down your app a tiny bit; (2) the code is harder to read and understand (as evidenced by this blog post:).
# Posted By Mark Shepherd | 3/20/07 1:30 PM
todd sharp's Gravatar You _can't_ seriously think evaluating a <cfif> could possibly result in any noticable performance hit.

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.
# Posted By todd sharp | 3/20/07 2:38 PM
Raymond Camden's Gravatar To be clear - and I thought it was clear in the post. The situation I described was a case where I needed to _temporarily_ add something to a page. It was not meant for production.
# Posted By Raymond Camden | 3/20/07 2:42 PM
Steven Schulman's Gravatar What do you think about putting <cfset dbLevel = 0 /> at the beginning of the code.

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.
# Posted By Steven Schulman | 3/20/07 6:14 PM
Rob Wilkerson's Gravatar This gave me a good laugh. I'm pretty lazy, too, but this particular shortcut never occurred to me. Usually I'm in a position to use ctl-z/ctl-y to un/redo my temporary comments.

Thanks for inspiring creative experimentation in the indulgence of laziness. :-)
# Posted By Rob Wilkerson | 3/21/07 6:16 AM
Rick Root's Gravatar Ray, that's pretty funny - I do this all the time but it's never occurred to me to shorten it like that... I've always done

<cfif 1 eq 1>

and

<cfif 1 eq 0>
# Posted By Rick Root | 3/21/07 8:24 AM
Joshua Curtiss's Gravatar Lol! Good stuff! Commenting and uncommenting the dump is always so irritating!

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. :-)
# Posted By Joshua Curtiss | 3/21/07 5:25 PM
Ben Nadel's Gravatar @Josh,

Yeah, CF is pretty sweet!
# Posted By Ben Nadel | 3/21/07 5:30 PM
Joshua Curtiss's Gravatar Ok, after reading my comment again, I see it is a waste of bytes. First, ColdFire doesn't currently display Form/Session/Application/etc dumps. Although that'd be nice. Secondly, I obviously knew you could use parens in conditional expressions. What I didn't realize is that you could do stuff like: <cfset MyBool=(x GT 10)>. I love that. For some reason, your code snippet triggered this random thought. :-)

Thanks for bearing with me. It's been a long day.
# Posted By Joshua Curtiss | 3/21/07 5:47 PM