May 2014

Gavin Pickin

When A Boolean is a Boolean and when it isn't - CFML Language Headaches

CFML Language, Techie Gotchas

As usual, ColdFusion has the ability to make me look like a talent developer one minute, but can make me look like crazy fool the next. Am I surprised... no, not in the fact it can do it, but this time, in the way it did it. As the title suggests, I was working with some Booleans, and like most languages, ColdFusion treats a 0 as False, and everything else True. This is not what got me... what got me is, that ColdFusion does not always hold that true.

Interesting right? Frustrating, absolutely. So here is the situation.

For these examples, I'm using -1 as the value in question.

<cfif -1 >

Knowing that 0 is false, everything else is true, then -1 is TRUE.
Thanks to we can see that Railo and ColdFusion agree, it is true.

Great... no problem yet.
So lets try the full expression, not shorthand.

<cfif -1 is true>

Ok, so that is true too right? WRONG.

Apparently, if you use shorthand, the rule holds true, but if you compare it, it is not true.
is, eq, == in both script, and in tags, in Railo and ColdFusion both work this way.

So evaulating as a boolean and comparing to a boolean is different.
Simple, maybe obvious when you think about it, but when I see ( -1 == true ) or I see ( -1 ) I assumed its doing the same logic... and I was a crazy fool for thinking so.

I tried this with other numbers too... I changed the order of the items, incase it evaluated differently, but its consistent.

The CF9 docs talk about it, and mention in Boolean Comparisons, that non zeros are true, but not sure how to do a boolean comparison unless its shorthand, to get it to work.

Food for thought.
Word of wisdom for today... test test test and never assume anything.

Have a good one.

by Andrew Scott
05/08/2014 01:28:24 PM

Yes the boolean conundrum, it has always been in my eyes.

If the value is greater than 0, then it is true and anything below 1 is false.

Is that not what your seeing?

by Gavin
05/08/2014 01:41:50 PM

No... I thought that was the case... for a long time. But, as a boolean... 0 is false, everything else is true. Not negative positive. But when comparing, cfml does weird stuff.

Run this on

true false nothing?


true false nothing?

They both answer with "nothing?"


true false nothing?

is true... like it should be. The issue is, when you compare to true and false, it converts true to a number, so only 1, or 0 are actually comparative.

If you just have if (x) its doing a real boolean comparison, so it converts the -1 to a boolean, and then evaluates it.

by Peter Boughton
05/08/2014 02:07:13 PM

There is no reason to compare to true or false - any such comparison means you ALREADY HAVE A BOOLEAN and thus should be testing it directly.

What's very likely happening is that because you are starting with -1 and using the equality operator, the second operand is treated as the same type, a numeric (i.e. converted to 1), and since -1 doesn't equals 1 it correctly says false.

by Gavin
05/08/2014 02:25:27 PM

Peter, good point. So I tried <cfif true eq -1> true <cfelseif false eq -1> false <cfelse> nothing </cfif>

Its nothing.

The issue came up, defining conventions, and we were leaning towards explicit longhand expressions... because apparently it's more readable... but obviously in this case, it provides a different result.

Just a quirk to be aware of. It seems they are converting true to 1, regardless of its order, 1st or 2nd operand.

Interesting though.

by Peter Boughton
05/09/2014 01:57:25 AM

> we were leaning towards explicit longhand expressions

It's not "explicit longhand" it's over-verbose, duplicated and completely entirely redundant.

Whoever "we" is needs their head introduced rapidly and repeatedly to a large heavy object until they either see sense or stop pretending to be a programmer.

Blog Search