Thursday, May 28, 2009

Cool Flash Tip of the Week 1

I'm going to start a series that will occur every week. A cool flash tip or something I think not many people know about. Be on the lookout every week for a new tip. This week's CFT is ......drumroll......." The Tertiary Operator ".








I spend alot of time traversing flash posts looking at other peoples code and I often see a large if else block to change a simple variable. Using the Tertiary Operator we can simplify this into one line of code. Shorter code is always better in my opinion. Plus can save you a couple bytes in your code.



Here is an example of what I see.















var myBool:Boolean = false;
var myVar:Number = 12;

if(myBool == true)
{
myVar = 15;
}
else if(myBool == false && myVar == 10)
{
myVar = 40;
}
else
{
myVar = 0;
}



Now this is all well and dandy, but it is a waste of space and can be simplified by a lot. One, when testing a Boolean there is no need to test to true or false. Instead if you want to test if something is true, you can just place the variable in the if parenthesis. This will automatically test to true. To test if a variable is false, use !. Here is an example.






var myBool:Boolean = false;
var myVar:Number = 12;

if(myBool )
{
myVar = 15;
}
else if(!myBool && myVar == 10)
{
myVar = 40;
}
else
{
myVar = 0;
}



This already shortens up our code a bit. But we can take this a step further using the Tertiary Operator. The syntax for this is pretty simple once you know what it means.






//Tertiary Syntax

(Test) ? True : False;






Basically, If the test is true, the code after the ? executes. If the test is false the code after the : executes.








  • ? = if

  • : = else







You can also string together Tertiary statements to make and if else if else etc. Here's how we write the same code from above over using the Tertiary Operator.






var myBool:Boolean = false;
var myVar:Number = 12;

myVar = (myBool ) ? 15 : (!myBool && myVar == 10) ? 40 : 0;






WOW, look how much space we saved, and it i still readable. I implore everyone to start using this and save space =D. Be aware that it is advised no to string Tertiary statements past "If..Else". If you liked this idea of CFT ( Cool Flash Tip ), then leave a comment, subscribe to the feed or / and follow me on twitter. It would be great to get some feedback on if this is helpful or not and maybe some tips you would like to see featured here.






Happy Flashing

18 comments:

  1. Allows Doing Great Work Man

    Thank's !!
    .......

    i have have question ?!!!

    what is you name ?:)

    ReplyDelete
  2. been using this for a while when working in Flex and need a conditional in an mxml tag, but for some reason I never think to use it when writing AS. Does make for cleaner code.

    ReplyDelete
  3. Allows Doing Great Work Man

    Thank’s !!
    …….

    i have have question ?!!!

    what is your name ?:)

    ReplyDelete
  4. I like the tertiary operator quite a bit, and it is great for simple assignments.

    However, with the example you given, you've nested the operator. Personally, I do not like this style, as it breaks the scan-ability of the code.

    So something like:

    var username = user ? user.name : "";

    is great. Quick to read and easy to see what is happening. Anything more complicated, I prefer to see it broken out like:

    if(myBool) {
    myVar = 15;
    } else if (myVar == 10) {
    myVar = 40;
    } else {
    myVar = 0;
    }

    Which allows to scan line by line without having to stop and examine a line to really determine what it is doing.

    ReplyDelete
  5. True, you have a good point. I agree with you in this sense. I guess it comes down to personal preference. I would most likely break it apart too, but just wanted to illustrate this way just in case people want to nest it.

    Stay tuned until the next tip =D

    ReplyDelete
  6. [...] DIRECT LINK » This entry was written by admin and posted on May 31, 2009 at 6:29 pm and filed under Web Desgin. Bookmark the permalink. Follow any comments here with the RSS feed for this post. [...]

    ReplyDelete
  7. I agree with Tyler.
    It is good to reduce the but the structure should give some meaning

    ReplyDelete
  8. An important improvement Tyler made that wasn't elaborated on is the removal of "!myBool" from the second if statement. That test was redundant since it would only happen if the first test didn't find myBool == true.

    ReplyDelete
  9. @Jay, true, I was just demonstrating the usage of the ! operator.

    ReplyDelete
  10. And like a book i read once said:
    "The tertiary operator saves on size in you scripts. But it hardly saves on file size and apart from that, programmers tend to avoid using the tertiary operator because of its cryptic syntax"

    Wise words. But that book was about C++ and I didn't know it existed in AS3.0

    ReplyDelete
  11. Hi, good post. I have been woondering about this issue,so thanks for posting. I’ll definitely be coming back to your site.

    ReplyDelete
  12. Nice Post!

    Keep posting such a GOOD Tips :)

    ReplyDelete
  13. thanks for the tip. great short cut

    ReplyDelete
  14. You know so many interesting infomation. You might be very wise. I like such people. Don't top writing.

    ReplyDelete
  15. Your site is worth beeing in the top cause it contains really amazing information.

    ReplyDelete