Boolean Saves Time, and Space

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.



All ColdFusion Tutorials By Author: David Patricola
  • Boolean Saves Time, and Space
    Boolean values are excellent ways to save on precious coding time when you are using a boatload of IF statements in an application. Having only 2 possible states allows you to kind of cheat on creating your IF statements and minimize redundancy.
    Author: David Patricola
    Views: 8,087
    Posted Date: Thursday, July 24, 2003
  • E-mail Scramble
    This simple CFC scrambles your e-mail into ASCII-equivalent characters, reducing the chance of your e-mail being phished by bots and spiders significantly.
    Author: David Patricola
    Views: 4,505
    Posted Date: Sunday, July 9, 2006