30 Jan 2006

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! :)

Creative Commons LicenseThe Nested comments in MXML article by Aral Balkan, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Noncommercial 2.0 UK: England License.

Add Your Comment

Spam Protection by WP-SpamFree

Nested comments in MXML

  1. I know what you mean, it’s such an annoyance to have to comment this way. the // is still my favorite.

    Joseph
  2. Hello World!!!

    dbk111
  3. Assateaque’s ponies walk right up to your car for treats, almost like begging dogs.  Too bad they have been “tamed”  I’m not sure that the ponies are drugged though.  Pony Penning week is conducted the last week of July.  The ponies are swum across an inlet from Assateaque to Chincoteaque and herded down mainstreet to ccda a pony pen where they are examined and recieve vacinations.  The young ones are auctioned off to ensure sustainability on the island, and raise money for the local firemen.  If weigh less than 100 pounds you can even attempt to ride them.  It was great fun!

    shaz GM lovely
  4. Wow 10 lines of code that could be 1 line of code…. not ideal

    Eric
  5. Thank you for the comment information for Flex. I was looking for this information.

    Sandcarving
  6. what is the correct code of comment in mxml when you are adobe flex builder…

    yeah