Ternaries, ||, and cleaner clode…

March 22, 2007

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!";

Entry Filed under: Flex. .

1 Comment Add your own

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Calendar

March 2007
S M T W T F S
     
 123
45678910
11121314151617
18192021222324
25262728293031

Most Recent Posts