Ternaries, ||, and cleaner clode…
In my first post, I mentioned a post over on Doug McCune’s Blog that finally tipped me over the edge to start blogging. So, here’s it is…
I like ternary statements. What the flip is a ternary, you ask? This is a ternary:
var bigger:Boolean = (num1 > num2 ? true : false)
Ya know, it’s one of those ? things we use instead of an “if-else” statement. They often make code a lot cleaner; but, use them unwisely and you’ll end up with messier code than you otherwise needed.
Take this example (that I’ve seen pop up often in code). Let’s say you want to set the value of a variable called newVar to the value of myVar, unless myVar doesn’t exist or is an empty string, in which case you want to set it to “Hello There!”. The code I’ve seen to do that usually looks like this:
var newVar:String = myVar != null ? myVar : (myVar != "" ? myVar : "Hello there!");
Although that does the job on one line, it’s kind of tricky to read (although not as bad as the one Doug found). Some would say this is a better solution:
var newVar:String;
if (myVar != null && myVar != "") {
newVar = myVar;
} else {
newVar = "Hello there!";
}
Although this is certainly more readable, it feels like a lot of code to just set a default value.
This can be simplified a little by understanding truthy and falsy values. For any variable, the values null, undefined, NaN, and "" (empty string) are considered falsy. In other words, if you say if (myVar) {...} and myVar is any of those values, the if statement will evaluate to false. If it’s a valid value (say, “Hello World!”), then it evaluates to true.
This can be really handy in conditional statements to avoid having to check all the possible falsy values. Instead, you could use this:
var newVar:String;
if (myVar) {
newVar = myVar;
} else {
newVar = "Hello there!";
}
(Incidentally, you can also say if (!myVar) {...} to see if the variable is one of the falsy values.)
However, this is still a lot of code. Fortunately, the || (OR) operator also functions as a default operator. In other words, when used in an assignment statement, if the first value is falsy, the second value will be used instead. So, this bit of code will do exactly the same thing as all the others:
var newVar:String = myVar || "Hello There!";
You can add together as many default operators as you want. In this example, newVar will be set to the first of these variables with a truthy value:
var newVar:String = myVar || var2 || var3 || var 4 || "Hello There!";
Hello world!
Hello World! Okay, Okay, Okay. So I was reading a post on Doug McCune’s blog this morning, and I started a reply. Well, the reply started getting so long and drawn out that I finally said to myself, “Ya know, this really should be a blog post all to itself.” And thus tipped the scales… I finally did what I’ve been thinking about for ages and started a blog.
So, enough about that, here’s a quick run down of who I am…
Aaron King… Happily married to my sweet wife Aby. We’ve got a 17 month old son and another due in September.
Recently I worked on the Yahoo! Maps product, but left Yahoo! in January to join Clint Modien and Ben Lucyk at Esria, Inc.