Home > Flex 4 Examples > Conditional Compilation

Conditional Compilation

There is a cool compiler flag that allows you to conditionally compile code into your binaries called -define.

This really cool feature has an annoying side effect though:

private function doSomething():void
{
    if ( whatever == true )
    {
        //Do something
    }
    KEY::debug
    {
        else
        {
            trace("Some debug information here");
        }
    }
}

This throws a compile error. I have a lot of places in the code where in the else of a condition I just want to trace something out, but if I put the debug compiler statement just around the trace inside of the else, I get a compiler warning when using the command line tools:

/path/to/target: col: xx Warning: Empty statement found where block of code expected. Did you type ’;’ accidentally?

Sure, I can re-write the code with the debug stand alone with another IF condition that is the opposite of what the actual code is doing, but I think it is more natural and makes the code clearer to read later when maintaining to just optionally include the else in certain build environments.

Another option is to use another key, such as this:

private function doSomething():void
{
    KEY::debug
    {
        if ( whatever == true )
        {
            //Do something
        }
        else
        {
            trace("Some debug information here");
        }
    }

    KEY::release
    {
        if ( whatever == true )
        {
            //Do something
        }
    }
}

The obvious pitfall is code duplication, which is also bad for maintenance.

If you agree, you can vote here.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: