Pages

Monday, April 4, 2011

Writing better CFML Series - Part 1: Booleans

Boolean values are one of the most basic programming building blocks.  Basically boolean values they are switches that can hold only two values, true of false.   Just like a light switch can only be in two poisitions: "on" or "off".

In ColdFusion we are given a lot flexibility of what a boolean is.  ColdFusion will recognize the string values "yes or no", the boolean values of "true and false", and numeric values "0 and anything but 0" as booleans values.

To demonstrate I've written a simple function called outputBooleanValues as shown below:
<cfscript>
function outputBoolValues(boolean yesNoBoolean, boolean trueFalseBoolean, boolean numericBoolean){
 if(arguments.yesNoBoolean){
  writeoutput("Yes/No Boolean is <strong>true!</strong>" & "<br>");
 }
 else{
  writeoutput("Yes/No Boolean is <strong>false!</strong>" & "<br>");
 }
 
 if(arguments.trueFalseBoolean){
  writeoutput("True/False Boolean is <strong>true!</strong>" & "<br>");
 }
 else{
  writeoutput("True/False Boolean is <strong>false!</strong>" & "<br>");
 }
 
 if(arguments.numericBoolean){
  writeoutput("Numeric Boolean is <strong>false!</strong>" & "<br>");
 }
 else{
  writeoutput("Numeric Boolean is <strong>true!</strong>" & "<br>"); 
 }
 
}
</cfscript>

One thing I'd like to point out right away about this script is how booleans are handled.  One of the most common mistakes that new programmers follow is comparing a boolean variable to its boolean value.  Lets look at line 2 of the above script:

<cfscript>
 if(arguments.yesNoBoolean){...}
</cfscript>

A lot of new programmers would write that line as follows:

<cfscript>
 if(arguments.yesNoBoolean eq true){...}
</cfscript>

One of the nicest things about working with boolean values is that we don't have to compare them to their value when writing if/else statements.   Remember that when writing an if/else statement you are comparing an expression to its boolean value.  This means is that you can evaluate just the boolean variable.  Comparing it to its boolean value is redundant and not needed.

Now that we've gotten that out of the way, lets demonstrate how ColdFusion uses many different representations of true/false.   When I run the following code through the outputBoolValues function:

<cfscript>
 outputBoolValues("yes",true,584);
</cfscript>

I get the following output:








One thing that's important to note here is that the value "584" returns "true", in fact any number that  is not "0" returns true.  This can be really powerful when evaluating array's and in other numerical operations as I"ll show later.   Now lets switch the true values to false values and run the function again:

<cfscript>
 outputBoolValues("no",false,0);
</cfscript>

Now you can see that all my values are "false":






Now that we've got a good understanding of booleans we can start having fun with them and seeing how they can be used in our everyday coding.  One common way to use numerical booleans is to check if lists and array's have any elements or not.  Lets create an array with zero elements and check if has any elements:

<cfscript>
 myArray = [];
 
 if(arrayLen(myArray)){
  writeoutput("My array has elements :-)");
 }
 else{
  writeoutput("My array has NO elements :-(");
 }
</cfscript>
 

Running that script would produce "My array has NO elements :-(".  Now if we change the script to add an element to the array :

<cfscript>
 myArray = ["Red"];
 
 if(arrayLen(myArray)){
  writeoutput("My array has elements :-)");
 }
 else{
  writeoutput("My array has NO elements :-(");
 }
</cfscript>

We now get the output "My array has elements :-)".  The same type of logic can be applied to lists, queries, and mathematical operations.

What if we wanted to evaluate many boolean variables at once?  We can use AND statements to tie many boolean values together like so:

<cfscript>
 if(true AND true AND true AND true){
  writeoutput("Its all true");
 }
</cfscript>

If we change any of those values to "false" the statement "Its all true" would not print.  When evaluating more than one boolean variable using "AND" statements every item must evaluate to true for the entire expression to be true.  We can even combine the different ways of expressing booleans:

<cfscript>
 if(1 AND "yes" AND true){
  writeoutput("Its all true");
 }
</cfscript>

I hope this has helped you get a better understanding of booleans and how ColdFusion uses them.  Since this is my first post in what I hope will be many more I'd really appreciate your feedback.  Was this information too technical, not technical enough?  Was it presented in a logical way?  Was there anything I could have done different to more clearly present the concepts?