Categories
ActionScript

Auto Format your logic!

I just discovered a pretty serious bug with Auto-Format (MX 2004):

If you run the following code, you’ll get “true 2”:

function test() {
    if (true) {
        if (true) {
            trace("true 2");
        } else { // false 2
            trace("false 2");
        }
    } else { // false 1
        trace("false 1");
    }
}

test();

Now if you auto-format this code, and then run it, you’ll get:

true 2
false 2
false 1

Why? Because Auto-Format turns the code into:

function test() {
    if (true) {
        if (true) {
            trace("true 2");
        } else {
            // false 2
        }
        trace("false 2");
    } else {
        // false 1
    }
    trace("false 1");
}

test();

Note the last two trace() statements, they’re moved outside of the else blocks! This is because of the comments at the end of the two else statements (which are now inside the else blocks). So watch where you put your comments! You can try this by auto-formatting mx.events.EventDispatcher.as and see the same (make sure you don’t save it!)

By the way, I always put my curly braces like this, so I haven’t ran into this bug before, until I tried to auto-format EventDispatcher.as:

function test() {
    if (true) {
        // comments here
    } else {
        // and here
    }
}

The lesson of the day is: Don’t put your comments at the end of a statement and use auto-format, or you’ll run into logic changes like this!

3 replies on “Auto Format your logic!”

Leave a Reply

Your email address will not be published. Required fields are marked *