-
Recent Posts
Archives
Meta
Notes on software development
Starting with SDK 3 Flex ActionScript compiler supports conditional compilation that can be used similarly to #if/#else pragmas in C# or C++. It works somewhat differently, but it’s pretty close.
To use it define a compile-time constant in your debug build, e.g.
–define=BUILD::DEBUG,”true”.
In Flash Builder it’s done in “Flex Compiler” tab of the project properties. Alternatively, you can define it in the command line:
mxmlc -define=BUILD::DEBUG,”true”,
or in your ANT build (if you use it):
<mxmlc … >
<define name=”BUILD::DEBUG” value=”true”/>
</mxmlc>
In your release build define the same constant as
–define=BUILD::DEBUG,”false”.
Then you can use it in the code like this:
public class Debug { public function assert(condition : Boolean) : void { BUILD::DEBUG { if (!condition) { throw new Error("Assertion failed"); } } } }
This function will behave similarly to Debug.Assert() in C# or assert() in C++. It’ll throw an exception in the debug build, but will do nothing in the release. If you’re not familiar with assertions, this Wikipedia article can get you started.
You can also include/exclude entire methods and classes. Check out Adobe’s documentation (PDF) for more details and examples.