ColdFusion Booleans are Short Circuit Booleans

I was never entirely sure as to whether or not ColdFusion booleans where short circuit or not.

(If you are not sure what a short circuit boolean is – check out this article)

I got a tad bored, so I decided to run some tests to see what was what, and lookee here, CF uses short circuit booleans.

I have a feeling in 6.0, this wasn't always the case, but in 6.1 it is definatley the case.

Pretty simple testbed – 2 funtions:

<cffunction name="ftrue">
 <cftrace text="true" inline="true"> 
 <cfreturn true>
</cffunction>

and

<cffunction name="ffalse">
 <cftrace text="false" inline="true"> 
 <cfreturn false>
</cffunction>

and so I ran some tests, and these are the results i came out with (somewhat stripped):

true & false
[CFTRACE] – true 
[CFTRACE] – false 
——————————————————————————–
false & true
[CFTRACE] – false 
——————————————————————————–
true & true
[CFTRACE] – true 
[CFTRACE] – true 
——————————————————————————–
false and false
[CFTRACE] – false 
——————————————————————————–
true or false
[CFTRACE] – true 
——————————————————————————–
false or true
[CFTRACE] – false 
[CFTRACE] – true 
——————————————————————————–
true or true
[CFTRACE] – true 
——————————————————————————–
false or false
[CFTRACE] – false 
[CFTRACE] – false 
——————————————————————————–
(true or false) and true
[CFTRACE] – true 
[CFTRACE] – true 
——————————————————————————–
(false and true) and false
[CFTRACE] – false 

So what does this mean?  Well, for starters – you can do things like this:

if(isDefined("x"))
{
 if(x eq 1)
 {
  x = x + 1;
 }
}

can become

if(isDefined("x") AND (x eq 1))
{
 x = x + 1
}

Without any fear of error, because if the isDefined() fails, the short curcuit doesn't have to check the right hand side, as false and true will always equal false, no matter what.

It's an interesting thing to note when doing your boolean logic, and should open up some interesting ways of making some algorithms just a tad bit faster.

 

Leave a Comment

Comments

  • Calvin Ward | July 15, 2004

    if(isDefined("x") AND (x eq 1))
    {
    x = x + 1
    }

    The above should work in ColdFusion 4.5, 5, 6 and 6.1 🙂

  • Gatssby | July 15, 2004

    For what it’s worth: I have always assumed CF booleans were shortcircuit and have always coded according to that assumption (from version 4.5 up) and have never gotting into trouble for doing so.

  • Steve Nelson | July 15, 2004

    This short circuit subject came up in CF3. From what I recall Allaire added short circuiting into CF in version 3.1, but I’m not 100% sure.

  • Spike | July 16, 2004

    Short cicrcuit booleans were added in CF either 4.0 or 4.5 and they have been fully supported (i.e. the same way as they are in 6.1) ever since.

  • Mark | July 16, 2004

    Ah well,
    Not new or revolutionary, but at least now I definatley know that they are.

    I never was entirely sure. :o)

  • Judith Dinowitz | July 16, 2004

    Actually, the ability was added in ColdFusion 4.01, and it was (as my husband Michael puts it) "the one feature that made the move from 4.0 to 4.01 truly worth it." : )

    Michael gave a quick overview of short-circuited boolean evaluation in his "ColdFusion with Style" article, published in Fusion Authority in February 2000. The URL is:

    http://www.fusionauthority.com/Article1.cfm/ArticleID=2511

    Hope that helps,

    Judith Dinowitz
    Editor
    Fusion Authority
    http://www.fusionauthority.com

  • Spike | July 16, 2004

    Yeah, that sounds about right. I clearly remember the release when they added it because I was teaching a lot of ColdFusion classes at the time. I just couldn’t remember which 4.x one it was, but I was fairly sure it was earlier than 4.5. Thanks for the clarification Judith.