http://www.insideria.com/2009/10/create-cleaner-actionscript-wi.html

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. 

posted on 2009-11-09 13:24  sungo  阅读(212)  评论(0编辑  收藏  举报