Writing compound statements

Compound statements contain a list of statements within braces ({}). The statements within these braces are indented from the compound statement. The following ActionScript code shows an example of this:

if (a == b) {
    // This code is indented.
    trace("a == b");
}

Place braces around each statement when it is part of a control structure (if..else or for), even if it contains only a single statement. The following example shows code that is written poorly:

// bad
if (numUsers == 0)
    trace("no users found.");

Although this code validates, it is poorly written because it lacks braces around the statements. In this case, if you add another statement after the trace statement, the code executes regardless of whether the numUsers variable equals 0:

// bad
var numUsers:Number = 5;
if (numUsers == 0)
    trace("no users found.");
    trace("I will execute");

Executing the code despite the numUsers variable can lead to unexpected results. For this reason, add braces, as shown in the following example:

var numUsers:Number = 0;
if (numUsers == 0) {
    trace("no users found");
}

When you write a condition, don't add the redundant ==true in your code, as follows:

if (something == true) {
    //statements
}

If you are compare against false, you could use if (something==false) or if(!something).