Nested comments in MXML

Block comments -- no, don't!Recently, while using Flex 2 Alpha 1, I found that I could not comment out a section of MXML that itself contained comments. As a developer, I'm used to quickly commenting out bits of code while debugging and Flex Builder even provides a handy shortcut for this: Source -> Block Comment.

Unfortunately, this feature currently has two shortcomings that make it a less-than-ideal solution.

Firstly, as I mentioned above, it cannot be used to comment out MXML that itself contains comments and, secondly, it cannot be used to comment out ActionScript code within mx:Script tags.

The second issue shouldn't be too hard to fix and even the first one can be avoided with a little workaround.

The first shortcoming is actually a limitation of XML: You can't nest XML comments.

The Block Comment feature currently places comment tags <!-- --> around a selected bit of MXML. This works correctly unless the markup itself contains comments, in which case, the document ceases being valid XML. So, what's the workaround? Simple: You can use a script to comment out areas of MXML that contain comments.

So, instead of <!-- -->, the Block Comment feature could wrap the selected text with the following tags:

    <!-- MXML BLOCK COMMENT -->
    <mx:Script>
        <![CDATA[
            /*
                <!-- Put anything here,
                        including other XML comments -->
            */
        ]]>
    </mx:Script>

Concise, well-written comments that clarify the intent of code is the mark of a good programmer. The importance of commenting not just your ActionScript code but your MXML increases with Flex 2, as more logic becomes contained in the MXML via data binding, validation, states, etc. And, as more and more comments are placed in the MXML, the current Block Comment feature will break more often and for a larger number of users when commenting out blocks of code while debugging.

If, however, the suggestions herein are implemented, the Block Comment feature should become indispensible and widely used. Here's to that! :)

Comments