Flash CS4 supports conditional compilation

With all the excitement about CS4 and the major features it brings (3D, the new timeline and so on), some less ‘shiny’ improvements tend to be overlooked.

Such is the case with conditional compilation. Developers familiar with “real” compilers will instantly know its power.
Conditional compilation allows for blocks of code to be compiled or not based on constants declared in the IDE. In the simplest form, think of it as of a more advanced version of the old “Omit Trace Actions” Publishing option. It allows the developer to optimize the generated code depending on the target medium (e.g. Web vs AIR) or the development phase (i.e. when compiling an app for debugging, the developer may want to switch on more warning traces or display additional info on screen).

The syntax for conditional compilation is dead simple:

function conditionals():void
{
  CONFIG::DEBUG
  {
    trace('Debug mode is on');
  }
}

You need to define the CONFIG::DEBUG constant in Flash CS4 publishing options.

The example above is very simple, and you may think you could achieve the same thing by defining a const DEBUG:Booolean in your code and then testing for its value with an if statement. The conditional compilation however increases performance by not performing the test at runtime and results in a smaller code, since potentially big chunks of code won’t be included at all in the compiled code.

Oh, and you can now specify the execution timeout (previously available only in Flex).

Happy coding!

PS: I can’t wait to test the performance gains with the new Vector class vs the plain Array. My flash chess should get a speed boost, allowing for more features.

Armand Niculescu

Armand Niculescu

Senior Full-stack developer and graphic designer with over 25 years of experience, Armand took on many challenges, from coding to project management and marketing.

4 Responses

Comments are closed.