Comments in BIML script #til

A lot of my blog posts in the near future will probably be short #til (today I learned) type posts.  This post is about some weirdness I noticed when working with comments in BIML.

To set the stage, I’m a big fan of writing helpful comments into the code as I write it.  Comments are ignored by the compiler you are working with, they are just sort of there, hanging out and hopefully helping you as you work or when you come back to work days, months or even years later.  Sometimes I wrap a line in comments so I can start fresh but still get back to the original code, sometimes the comments are reminders for myself of things I need to do or keep track of.  Those comments get deleted as I work.  Other times I comment the intention of my code.  This is sort of a pet peeve of mine, reading comments in code that document HOW something was done but not WHY it was done.  Sure, sometimes code gets complex and you SHOULD document what is going on, but it’s also pretty useful to have the occasional comment explaining WHY we are doing all this stuff to begin with …

So yeah … back to the weirdness I discovered when writing comments in BIML.

In BIML, you wrap a line you want to ‘comment out’ with <!– where you want to begin the comment and –> where you want to end the comment.

For example, in the following code snip I’ve commented out everything between the opening BIML tag and the closing BIML tag.  In this case the comment block includes multiple lines.  Sometimes you will put the opening comment at the beginning of a line and the closing comment at the end of a line to comment out only one line at a time.

note:  If you copy this into a fresh BIML file, everything between line 2 and 15 will be green (to indicate it is commented out).

Easy peasy.  Now HERE is the weirdness.  Although everything in the body of this code is commented out (including the C# biml script where I am playing with a C# dictionary) the C# code will still run if I right click on my BIML file and select ‘Check BIML for errors’.

At first this confused me.  Then I stopped to think it through (a practice I highly recommend even if it’s more fun to just blindly bang out code) and I realized that to comment out the C# code I would need to use C# comments.  The // characters together are the C# version of a comment.  If I go back and add // just before the Messagebox.Show command (line 12 below) it will prevent the MessageBox from popping up when I test the BIML code.

Once I thought it through, it makes sense.  BIML and C# mark their comments differently, and I suspect C# is being evaluated on a separate pass through the code anyway … so using both types of comments would be required.

There you go.  I hope this helps you as you begin to add more C# to your BIML.


Leave a Reply

Your email address will not be published. Required fields are marked *