Friday, 25 March 2011

Flash AS3: If Else shorthand

All programming is a matter of style. Many people prefer a shorthand if..else format, which works by condensing the statement into one line. (perhaps at the expense of readability?)

Take a conventional if statement like the following:
if(catLegs >= 4){
   trace("cat can run!");
}else{
   trace("cat has missing legs..");
}

Using a ternary operator it can be condensed into one line following the format:
(condition) ? ifTrueDoThis : ifFalseDoThis;

Rewriting the example above would give us:
(catLegs >= 4) ? trace("cat can run!") : trace("cat has missing legs..");

No comments:

Post a Comment