Lately I’ve been finding that the way in which i write code hasn’t really advanced at all since i started 3 years ago. Sure I’ve learnt more of the Adobe AS3/Flex library/s, and learnt several new workflows but the actual way which i write code hasn’t changed. So in this tutorial I’ll teach you how to write condensed if statements. and some other methods of doing comparisons.
For instance i might write something like this quite often:
if(myCheckBox.selected == true)
{
someVar = true;
}
else
{
somevar = false;
}
This is probably the most long winded way of performing that simple action.
Heres one way the if statement can be condensed by using shorthand:
if(myCheckBox.selected == true)
someVar = true;
else
somevar = false;
See much cleaner
You can omit the braces {} if you only have one statement in the if body. For instance this will not work:
if(myCheckBox.selected == true)
someVar = true;
someFunction();
else
somevar = false;
This would result in a compiler error.
However for this particular operation (changing a value to true or false with a comparison) we can make it even more simple:
someVar = (myCheckBox.selected == true);
This works because the comparison evaluates to true or false.
But say you want to use something other than true or false, how would you do that? The answer is quite elegant:
someStringVar = (myCheckBox.selected == true)?"Checked":"Not Checked";
Here is the syntax if it’s not completley clear (from adobe livedocs):
expression1 ? expression2 : expression3
Evaluates expression1, and if the value of expression1 is true, the result is the value of expression2; otherwise the result is the value of expression3.
Well i hope this little snippet will help you write cleaner more efficient code
Peace
Bill



