PDA

View Full Version : If Statement Syntax Question


madcat
11-11-2008, 05:42 PM
All of the following examples work without issue. Just wondering which is recommended or best practice. Should any one of these be avoided? Why?

In books, usually the curly braces are used... in practice, I see a lot of advanced coders using the first example...


if (condition) trace("works");

if (condition) { trace("works"); }

if (condition) {
trace("works1");
trace("works2");
trace("works3");
}

if (condition)
trace("works1");
trace("works2");
trace("works3");


Thanks!

creatify
11-11-2008, 06:03 PM
As long as the shorthand versions aren't known to be slow, I'll take any chance I have to type less (especially with AS3).

With if statements, I haven't come accross any benchmark tests to see which of your examples would be faster. With this conditional, imho, it comes down to personal preference, and the choice to make things as legible as possible, especially if programming within a group.

For example (and I'm not sure this works in AS3, haven't tried it):

(foo) ? [func1(), func2()] : func3();


can be pretty confusing for other less-experienced coders, so in legibility respect, it might be avoided.