Boolean Saves Time, and Space
by David Patricola <david.patricola@mindspring.com>
Boolean values can be a time trimmer when you're crunching 10 hours of code in a
5 hour deadline. IF statements can become quite burdensome at times, constantly
typing out the conditional. With boolean values, you can shortcut that
conditional typing slightly. Here is why.
<cfset flag = 1>
<cfset results = "">
<cfif flag eq 1>
<cfset results = "foo">
<cfelse>
<cfset results = "bar">
</cfif>
Pretty simple, right? No hidden functionality here. Let's take a "little off the
top", barbershop-style, showing you how I do it.
<cfset flag = 1>
<cfset results = "">
<cfif flag>
<cfset results = "foo">
<cfelse>
<cfset results = "bar">
</cfif>
Voila! Overall, it's not much, but with IF statements in bulk this can be a
helluva little trick.
How does it work? Well, the value of flag is set to "1". In boolean terms, 1
means true/on, while 0 means false/off (Access DB users will notice this as
well). Since an IF statement, at its base, merely checks one of two possible
conditions for a variable, this works perfectly.
<cfif flag> literally processes to
<cfif 1>, and since 1 means "true", that part of
the IF statement is executed.
Coincidentally, this technique can be done if your variable is has a value of
"TRUE" or "FALSE", as ColdFusion treats these two words in the same manner as
1/0. Now, back to your regularly scheduled program.