ColdFusion Muse

The Boolean-O-Matic: ColdFusion's Weird Relationship With Truth

Mark Kruger February 5, 2010 12:28 PM Coldfusion Tips and Techniques Comments (7)

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.

Read More
  • Share:

7 Comments

  • Brad Wood's Gravatar
    Posted By
    Brad Wood | 2/5/10 3:02 PM
    "I apologize for my long hiatus"

    Ha- that's not what she said...

    Glad to see you back!
  • Martijn van der Woud's Gravatar
    Nice summary! One minor detail:

    myQry.recordcount GT 1

    I think it must be
    myQry.recordcount GTE 1

    or, equivalently
    myQry.recordcount GT 0
  • Sergii Galashyn's Gravatar
    Posted By
    Sergii Galashyn | 2/5/10 7:36 PM
    Nice summary, thanks. Just as minor addition. Railo makes these comparisons unified: each example above returns true/false only. Though "Yes"/"No"/0/1 will work in cfif, of course.
  • 8riaN Page's Gravatar
    Posted By
    8riaN Page | 8/8/11 8:04 PM
    I agree, nice summary, thanks!

    Sergii is, of course, right about Railo outputting both #1 AND "Yes"# and #"Yes" AND 1# to the browser as the string true, but it is perhaps worth noting that #10+("Yes" AND 1)# still =]equals 11 under Railo. (It doesn't just make 10 louder)
  • Phillip Senn's Gravatar
    Posted By
    Phillip Senn | 2/27/12 3:55 PM
    I wonder if there's a convert from true/false to 1/0 function already built into ColdFusion?
  • Jason Durham's Gravatar
    Posted By
    Jason Durham | 3/5/12 1:55 PM
    Another interesting nuance...

    isValid("range",true,0,1) returns "Yes"
  • Once Upon A Time Leather Jacket's Gravatar
    Regardless of the fact that you don't have any acquaintance with it, you utilize Booleans each time you make a cfif proclamation. Still, it's astounding what number of cutting edge designers don't completely get a handle on all the ways that ColdFusion has of assessing something as True or False.