So this is very old news, but I discovered recently that not everyone may know about this. ColdFusion uses short circuit boolean evaluation. Ok, so what in the heck does that mean? What it means is when ColdFusion is evaluating an expression, if it finds a way to end quicker, it will. So, in English, consider this statement:
Johnny 5 is a robot and is alive.If you wanted to check if that statement was true, and immediately discovered that Johnny 5 was actually a cucumber, you don't need to test the second half of the condition, right? Anything FALSE AND TRUE will equal FALSE. (I actually had an entire class on boolean logic in college. One of the few classes I actually use in my day to day job.)
So let's look at a ColdFusion example. What if you wanted to check for the existence of a key inside a structure that is also inside a structure? Consider this code sample:
<cfif structKeyExists(myscope, "mydata") and structKeyExists(myscope.mydata, "johnny5")>
Pay attention to the second half of the expression. If mydata was not a valid key in myscope, you would get an error, right? Well since ColdFusion uses short circuit boolean evaluation, it will never get to the second expression if the first one doesn't evaluate to true. This is handy as it saves us from quite a bit of typing. It also let me reference cheesy 80s robot movies in my blog which is always a good thing. Now I just need a way to get Maximillian into an entry.
Archived Comments
Hi Ray,
This is something I noticed before and never thought more of it other than *cool*. Certainly didnt know the technical name "Short Circuit Boolean Evaluation" but now I do :-)
Its handy when you need to do something such as <cfif isdefined("dan") AND dan EQ "me"> without using CFPARAM.
Dan.
Great reminder Ray. I can't tell you how many times this feature has made my coding life much, much easier.
Maximillian? You could always mention how the Macromedia / Adobe "log a bug" form seems to work.
;-)
--
Adam
(yesyes, I know they DO get looked at, but it's a terribly user-unfriendly system)
Does IIF() work in the same way?
Now I feel silly. So much wasted typing . . .
Many thanks for the hint!
Best WIshes,
Peter
To mikeD:
No, in my experience, IIF() does NOT short-circuit, which is a major pain in the butt given how convenient short-circuiting is. I certainly wish that it did act that way.
But if I remember right, you can use DE inside the IIF to get the same result.
All of which is SUPER hard to read. I would tell my developers to avoid IIF.
I did study boolean algebra at uni too and really liked it. As far as maths are concerned, I've never been interested by anything but this subject, and set theory(SQL). Whatever I learned I though "what the hell do I care if I want to program?". Those 2 areas of maths immediately struck a chord, and again, appart from what I learned when I was 7(+/*-), that's the also the only maths I use in my job. I don't know about the US, but in France, maths are way too advanced if you don't study straight maths. I studied accounting before computer science and had two math modules: financial math, composed of series and other similar things, which makes a lot of sense (i.e. mortgage calculation), and standard maths like differential equations, complex numbers and whatnot. Some people say doing advanced math, even unrelated, is a good thing as it hones you problem-solving skills, and logic, but if you ask me, I've always felt it was total waste of time, and could be used for things that are more important(for whatever you do, that is. I'm certainly not saying that maths are useless!). For example, most students with me at uni couldn't speak or understand much english, when all the programming you do is english-based, and so is a good chunck of the available knowledge (blogs, tutorials...). Speaking of which, for the first time yesterday, I checked out the french coldfusion community, and realized it was pretty much inexistant. I've also never heard of french guys in the english-speaking community. maybe I should start a blog in both french and english, hey?
Anyhoo, sorry for the long comment, and if it looks like I'm ranting, but I'd be curious to know what you guys think. Do you feel you did more maths than you should have? whatever complicated stuff you've learned, have you ever used it at work, and if a company needs to write math-intensive code , can't they hire a coder AND a mathematician?
tof
Do I get brownie points for being able to say "The Black Hole" without having to look anything up?
Cool!
And yeah, CF uses this... I didn't know the technical term, but I do know that other languages don't support it. They go on to check the status of the other side of an AND before returning the result, which, as I understand it, can be maddening at times. Since CF is pretty much the only language I use a lot (other than MXML, AS, JS, and SQL, that is) I can't honestly speak to their pain. ;)
Laterz,
J
Tof,
I've always found that there is a big difference between a "waste of time" and "not knowing what it's used for". Having gotten my Master's in CS focusing on AI, let me tell you that a lot of the learning techniques used today could not have been done without a lot of complex math. I'm sure this applies to many other areas as well, but AI is the one I'm most familiar with.
Q: How much weight should I give to an employee candidate if they spell "ColdFusion" as "Cold Fusion"?
Ignore it. I wouldn't let it bug me.
Ray, great post. I love short circuit evaluation. A while back I compared it to regular expressions and found in testing that while RegExp are easier to write, they are out performed easily by short cicruit evaluation:
http://www.bennadel.com/ind...
Ben, this is a pretty interesting post. I had heard about Michaels solution before, but didn't know others were trying it as well.
Maybe I am misiing something here, but isn't this obvious? What you are saying is that you shouldn't write: <cfif structKeyExists(myscope.mydata, "johnny5")> without first checking whether there is a key called 'mydata' in the myscope structure? Or is there something else going on?
As a side note, I know that using 'StructKeyExists()' is faster than 'IsDefined()', but surely it is better to use 'IsDefined()' with multiple nested structures, like:
<cfset foo = {}>
<cfset foo['bar'] = {}>
<cfset foo['bar']['foo'] = {}>
<cfset foo['bar']['foo']['bar'] = {}>
<cfset foo['bar']['foo']['bar']['foo'] = "bar">
Otherwise a conditional to test for this could get quite busy:
<cfif StructKeyExists(foo,"bar") AND StructKeyExists(foo['bar'],"foo") AND StructKeyExists(foo['bar']['foo'],"bar") AND StructKeyExists(foo['bar']['foo']['bar'],"foo")>
#foo['bar']['foo']['bar']['foo']#
</cfif>
As opposed to:
<cfif IsDefined("foo.bar.foo.bar.foo")>
#foo['bar']['foo']['bar']['foo']#
</cfif>
Well, it's hard to know what I was thinking over a decade ago (grin), but while this is obvious to most programmers, I can imagine it may not be for folks who may come to development from a non-traditional background. Even if it does, the idea that you can have a Y that would throw an error normally but is safe if X is true may not be something they understand.
Sure. Normally when I have a struct that deep I'm not digging that deep from the top. So I'm already inside a condition for foo[bar][bar] before I start looking for foo.
I get you Ray. Makes sense. I guess I am so use to using this stuff everyday, that it seems like child play.
Yes. Thats true, but I often use the above when analysing REST API responses. Sometimes you have to jump right in, 3 or 4 structs deep for a value. But, as I said, this is a bit of a red herring, on my part...
Despite, saying that this is obvious, I came across something today, that I thought was obvious for many years and found out that I have been wrong all this time. I always thought that any value equal to or less than zero evaluates to false when tested as a boolean. Shock horror when I find that -1 evaluates to true! So, I guess, what is obvious to one person, isn't to another...