Hello muse readers. I apologize for my long hiatus (which means a stretch of time where I was absent - it's not a size joke). I have been swamped with closing out the old year and implementing plans for the new year. I'm afraid our little chats were put on the back burner temporarily. However, now that new year has begun I am committed to continuing our friendship. I'd like to start out with something simple. Indeed, some of you may find this to be ColdFusion 101.
This post is going to discuss Boolean values. A Boolean is one of those datatypes more defined by how it is evaluated than by what it contains. The muse definition is that if something can return a "true" or "false" in the context of a logic statement (cfif) it is a Boolean. It may be other things as well, but it has the properties of a boolean and returns one of 2 states - true or false. Interestingly, every language handles Booleans differently and many of them use the same wild west sort of approach that ColdFusion uses - where several things can be used as Booleans.
Even if you don't know it, you use Booleans every time you create a cfif statement. Still, it's surprising how many advanced developers do not fully grasp all the ways that ColdFusion has of evaluating something as True or False. And having said that I am fully aware that some smarty-pants developer will immediately inform me of some new way I haven't seen before of evaluating true or false (thank you sir, may I have another).
Anyway, I'd like to take a little journey into the world of Booleans to start off my 2010 blogging. Note: this post has a number of neat "tips and tricks" that you may have not seen before. Whether you choose to use them can depend greatly on your environment, the structure of your code and the standard you are using (especially in a team environment). I'm not advocating for or against, although I have my own preferences. I'm only putting it out there as another arrow in your quiver. So with that caveat taken care of, let's begin.
Here's some deep background. You may have heard it said that ColdFusion is a "typeless" language. This is certainly true, although with cfparam, JavaCast and CFC argument declarations it is possible to specify the required "type" of a variable. What does it mean that CFML is typeless? It means that you can create variables willy nilly without specifying what "type" of data they contain (string, decimal, float, date etc). Under the hood CF does all sorts of checking to figure out what you want. For example, if you do something like:
ColdFusion will note the lack of quotes, evaluate the variable and create a variable x with a type of "numeric" – all hidden from you. This allows you to do something like:
Of course this wouldn't be possible if ColdFusion did not know (or at least be able to assert) that x was a numeric value. ColdFusion performs this same magic with dates and strings and other types of primitive values. Typelessness is what makes it possible for a developer to concatenate a number with a string or Boolean with a number or whatever. ColdFusion flips through the data types it can possibly use and figures out which one will work for the statement you have written. In some other languages this is not possible. In SQL for example if you try to concatenate a character column with a numeric column the server will complain about an "incompatible data type" and you will have to re-cast the column into the proper type.
When it comes to Boolean values ColdFusion's penchant for figuring things out for you has some crazy nuances. But let's start with a "normal" example.
Notice a few things. First, I'm using the word "true" without quotes. I believe ColdFusion knows this is a boolean value and it needs no "conversion" to figure out how to evaluate it in the CFIF statement (I'm assuming here). As a boolean it is either true or false. But what if we did this?
Now ColdFusion no longer has a Boolean variable to work with. Instead it has the "string" false. But just like with the numeric conversion ColdFusion ticks through the possibilities and knows that the string "false" can stand in for the Boolean "false". So the code above works (indeed, it doesn't even matter what case you use for the string).
Ok... so ColdFusion can evaluate the string "false" and "true" as the real live Boolean false and true - so what. All that means is that I don't have to remember whether I put quotes around certain variables - right? Well there's more to this saga. ColdFusion can also evaluate "YES" and "NO" in the same way. Early on there were some language choices made for CFML where certain attributes of tags took "YES" or "NO" for the string value. For instance, cfqueryparam uses YES and NO for the "list" and "null" attributes.
I'm not sure why yes and no were chosen over true or false. My guess would be that the origin is in some other language familiar to the CFML authors - or perhaps they felt it would be more intuitive for folks coming from HTML. Either way, the strings Yes and No may also be converted to Boolean. So the following works:
There are two things to note here. First, to set a yes/no it must be a string with quotes around it. If you try this:
The third way that CF handles Boolean values closely mimics the world of C programming. In C any numeric value (signed integers I believe) can be evaluated as true or false. The rule is, if it is a zero it is false. Everything else is true. So, 1,2,9,-40 ... all true. Only zero is false. The exact same rule applies in CFML but CF handles any number (integer or not). A decimal, or whether a number is negative or positive do not matter. zero is false, everything else is true. Try it for yourself using this little snippet:
How is this useful? One way is in evaluating the success of a query. Instead of doing the following:
Finally, there are some nuances you may not have thought of regarding how boolean values are handled in your IF/Else logic blocks. Consider this block of code.
Let's take a look at 2 properties here. First, you may not know that pretty much any logic block can be output to the screen like so:
You can return a number by evaluating a true/false value with a number as in - true/false first AND number second. This example returns a 1:
You can see there is a great deal happening under the hood when you build a logic statement. Perhaps it occurred to you, "If I can output the values of a logic statement to the screen, why can't I set it as a variable?" You absolutely can. Remember our example?
Once again let me say I'm sorry for my lengthy absence. Even the muse needs a break to recharge his batteries from time to time. I hope to begin posting my long anticipated (by me mostly...) cloud computing series soon. Meanwhile, I look forward to comments on this post from the bright folks who read my blog and always contribute richly to my way of looking at things.