Monday, March 22, 2010

If statement syntax shortcut


Many times have I come across a situation where I have an extremely simple "if statement" that takes up 5 or so lines of code and I've though to myself "I really wish I could express this code in a single line without losing a great deal of readability"

Well look no more... Actionscript 3.0 code such as this:

if (myVar >= 10) {
trace("foo1");
} else {
trace("foo2");
}

can now easily be compressed in to a single line utilizing an alternate method of syntax such as this:

(myVar >= 10) ? trace("foo1") : trace("foo2");

Nice huh? Probably not as easy to read - but for those of you with large class files, or if you are slightly OCD when it comes to white-space conservation as I am, it's a perfect method of syntax compression.

No comments:

Post a Comment