The Way To Great




条件编译允许我们告诉编译器选择性的不编译给定一部分代码除非我们给定的特定条件满足才对该部分进行编译,显然这是在FLEX3中ADOBE刚刚推出的新特性,之前我都没有听说过,知道今天造成J.C.发给了我LIVE DOC上的链接。




第二为这段代码够将AIR 和FLEX两个版本的应用程序,假设你有一个非常有用的类你想将它编译成FLEX和AIR版本的,而AIR版本的可能需要额外多一点点的代码,那么你就可以使用条件编译来包含在这些代码,然后选择性的将其编译成AIR 或者是FLEX版本的程序


Flex compiler supports conditional compilation

| | Comments (5)
AddThis Social Bookmark Button

Conditional compilation allows developers to tell the compiler not to include certain parts of their code unless a set of conditions are met. Apparently Adobe added this feature to Flex 3, though I hadn't heard anything about this until this morning when Jim Cheng sent me the LiveDoc link.

You might be wondering, why would I ever use something like that? Well, I'm glad you asked. Conditional compiling is incredibly useful in a few different situations, and chances are you're already solving some of the problems conditional compiling address the hard way. Ever flagged a section of your code with a big static variable to turn it on and off? Ever written a debug method you commented out later, or simply removed all the references to? If you're from the old C++ world, ever used #ifdef? If the answer to any of these questions is "yes" or "maybe" or even "no, but as a curious developer I'd sure like to know more!", then read on.

First, conditional compilation is useful in complex debugging. Sometimes you want to run a bit of debug code that tells you something a simple debug session won't. Rather than commenting this code out or flagging it with some sort of compiler variable, you can now code debug sections in to your heart's content. Just tell your compiler to ignore anything in the "debug" namespace and you'll be fine.

Second, it's great for building simultaneous AIR builds with your flex apps. Let's say you've got a class that's useful for both an AIR app and a Flex app, but the AIR app needs a little extra code, or includes a feature your Flex app can't support. Rather than reproducing all of the useful code or extending the class and overriding the methods in question, you can flag certain sections to only be compiled for AIR and ignored by Flex.

Third, it's useful for monkey-patching. Monkey-patching is a messy process that I don't recommend, but sometimes it's the best solution to a problem. In these situations it'd be nice to easily toggle on and off our monkey patched classes to see if they're working, how they're affecting the application, and hopefully remove them when Adobe implements the bug fixes we're undoubtedly patching around. :)

So check it out. If nothing else, by removing the need for commented-out debug methods or big static variable flags, conditional compilation will help you write cleaner code and earn you the respect and admiration of your co-workers.

个人分类: RIA
想对作者说点什么? 我来说一句