Home  >  

Create cleaner ActionScript with conditional compiling

Author photo
October 26, 2009 | | Comments (22)
AddThis Social Bookmark Button

A large part of any ActionScript project is code dedicated to testing and debugging. These parts of your application may be needed while in development, but usually not needed within a production environment. You might think that making a release build will take care of everything. Really, this only takes out the internal debugger code but not any of the traces or code that you might of defined while creating your project. The only way to strip this code out of your code base is with conditional compiling.

Conditional compiling can be used in any ActionScript development environment and after you set it up would be used something like this.

function log( message:String ):void 
{
 	CONFIG::Debug { 
  		trace( message ); 
  	}
}

In this example we have set a compiler constant called CONFIG::Debug. The compiler is able to check if this constant is set to false and, if so, strip out any code defined inside the following block. If the constant is set to true, the code is left alone and is able to function normally. These statements can wrap any function, property, or block of code. You can define as many constants as you like and even use them as values for properties within your application like this.

var useDebugging:Boolean = CONFIG::Debug;

The most obvious use for this functionality for is to strip out debugging and testing code that doesn't really need to be in your project when it is released. There are also other situations that this can be useful: like managing legacy code or enabling and disabling features within a much larger code base to create a smaller more optimized swf.

How to setup your compiler constants There are many different ways to set this up, skip down to your environment and we can pick backup after you have everything set up.

Flash
Go to the top menu File > Publish Settings. Select the Settings button next to the ActionScript 3 Script drop down. In the "Config constants" menu you will need to add your constants name and value here.

Flex ANT Task

<mxmlc ... >
    <define name="CONFIG::Debug" value="false"/>
</mxmlc>

Flex-config.xml file

<compiler>
        <define>
                <name>CONFIG::Debug</name>
                <value>false</value>
        </define>
</compiler>

Command line argument

-define=CONFIG::Debug,false

After you have everything setup
In all of these setup examples we have set one constant CONFIG::Debug to false. You are also able to set these values to other data types and even run simple conditionals.

You can define a String like this.

-define+=CONFIG::myConst,'Project Name'

Or do simple equations --this is especially useful if you are using a automated build process that takes different inputs.

-define+=CONFIG::myConst,'4 - 1'

Evaluates to the Boolean value false.

-define+=CONFIG::myConst,'1 > 2'
These types of equations are useful but don't seem to work to actually strip out code like we did in the examples above.

You are also able to use other constants within these equations like this.

-define+=CONFIG::Debug,false -define+=CONFIG::myConst,'CONFIG::Debug && false'

Conclusion
There are many ways that these compiler constants can be used in your project, but more than anything else they give you a layer on top of your projects code that is able to control what gets compiled into your project. This is incredibly useful when trying to make a small production swf. If more developers know about how to use these tools, code bases will be able to be built differently. Our code can change based on its environment --and larger libraries of code would be able to be created without the need for our compiled projects to grow.

I hope people start using these features a little more, it will give everyone more control over the end result.

Read more from Tyler Larson. Tyler Larson's Atom feed talltyler on Twitter

Comments

22 Comments

Davide Zanotti said:

Very, very useful tip, thanks Tyler!

Paul Jean said:

Great stuffs! It's nice there is something that I can follow now. Thanks for this tutorial!

Paul Jean

Sandro said:

I didn't even know that this was possible until now! Will start using it immediately! Thanks alot for that information :)

John C. Bland II said:

This is a great unsung feature but the problem is with tooling. It is still too manual.

Great article.

Ben Garney said:

I want to use these features, but I can't rely on users remembering to correctly add the constants. There needs to be a way to check for the _presence_ of such a compile time constant, otherwise it is useless for any large scale deployment.

The C preprocessor works this way and is tremendously useful (you can do #if ANYTHING and if ANYTHING is not defined it just skips it). This is not as useful because every single constant that is used must be accounted for by the developer before they can even compile the codebase at all.

Tyler Larson said:

It would be possible to check if these constants are defined and warn the people that don't have this setup. Something like this should work.

if( CONFIG::Debug != null ) {
// Start application
}else{
// Show information about how to setup
}

satish said:

Tyler, This is one of the best tip. Thanks a lot.

n!co said:

Good tip, thanks.

An other way, is to use flex compiler options as define in this page :
http://www.adobe.com/devnet/flex/articles/server_perf_03.html

Fernando said:

Cool, thanks!
I'll try this with FlashDevelop.

John said:

Does this currently work with Flex 3 / FlexBuilder 3, or is this a Flex4 /FlashBuilder thing?

Greg Burch said:

John - it works in Flex 3.

Ruy Adorno said:

I was wondering if there is a way to use it with mxml tags? Any thoughts?

Will said:

Awesome tip! I have been wondering how to do macro like structures in actionscript :)

Will said:

Awesome tip! I have been wondering how to do macro like structures in actionscript :)

Will said:

Awesome tip! I have been wondering how to do macro like structures in actionscript :)

Carlo said:

If CONFIG::Debug is false, the compiler will cut away the code or just skip itduring execution? I mean: if I include a class only in a conditional statement, will that class be included in the swf when CONFIG::Debug is false?? - To save space

David said:

Great post. Perfect for less experienced developers (like myself).

Tyler Larson said:

Thanks for all of the compliments, it's good to hear because it is my first post for InsideRIA.

@Carlo - This code is cut out of your project and not included in your compiled swf. Your swfs will be smaller if you design them correctly. One thing to watch out for is variable declarations.
public var myClass:MyClass;
This is going to compile in MyClass even if you never construct this object. But as long as you dont have references to these classes anywhere but inside of the CONFIG::Debug statements this code from this class will not be included.

valyard said:

Thanks. I didn't even know that this was possible.

Steve said:

Fantastic posting.

I can now use this instead of a project-wide find-and-replace for adding debug code!

Thanks.

Ian Appleby said:

If you use this approach to remove all refereneces to a swc, will it be left out, or will you have to use other logic in ant tasks or similar to drop the include?

Chris Lilley said:

I don't think the


if( CONFIG::Debug != null ) {
// Start application
}else{
// Show information about how to setup
}

trick works, because if you don't define some value for CONFIG::Debug, the compiler will complain that property Debug is undefined. This is one of the great frustrations with this technique.

Leave a comment


Tag Cloud

iPad

What's your take on the iPad? (Putting aside the Flash/iPad flame war)

Answer

Latest Features

Recommended for You

@InsideRIA on Twitter

Archives

  • Or, visit our complete archive.  

About This Site

Welcome to the premiere community site for all things RIA sponsored by O'Reilly Media and Adobe Systems Incorporated.