Ternary whoozawhats?

6 Jul

Are you some sort of code-efficiency freak? Yep… I figured you were. If you don’t know about this already, then it will be a shock when I tell you that you could cut parts of your code in half its line size!!!

var bool:Boolean = false;
if(bool == false){
    trace("bool is definitely false");
} else {
    trace("bool is definitely true");
}

That’s the if/else simply-read way…
While it may be a little bit easier to comprehend, this saves a little more space if placed in the stead of if/else statements.

var bool:Boolean = false;
trace(bool ? "Bool is clearly true":"Bool is definitely false");

Breaking it down…
3 unnecessary lines cut. While ternary operators may be a little bit more difficult to read, it is quite simple once analyzed.
Let’s just look at how the typical ternary operator is.

The following is the basic template…

condition ? ifConditionIsTrue:ifConditionIsFalse

If it is your first time working with these… please take note of your condition, because Example A has a different output than Example B.

Example A

var iAmReading:Boolean = false;
trace(iAmReading ? "I'm reading right now... please keep quiet":"Sorry, too busy putting this online.");

Example B

var iAmReading:Boolean = true;
trace(iAmReading ? "I'm reading right now... please keep quiet":"Sorry, too busy putting this online.");
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: