Wednesday, 28 January, 2004

More Visual Basic wonkiness

Item #467 in my ever-growing list of What's Wrong with Visual Basic:

Using line breaks as statement terminators is a terrible idea, especially when the language doesn't define an "inline comment" lexical element.  Why?  Consider this bit of code:

strHtmlStuff = "<tr>" & _
    "<td>Last Name</td>" & _
    "<td>First Name</td>" & _
    ' Age and weight removed pending approval & _
    '"<td>Age</td>" & _
    '"<td>Weight</td>"  & _
    "<td colspan=2>&nbsp;</td>" & _
    "<td>Notified</td>" & _

The idea here is simple:  I wanted to comment out a few lines of code temporarily.  But you can't do that because the comment character (the apostrophe) means "comment to the end of the line," which makes a complete hash of what I'm trying to do here.  Since the statement ends at the end of the line, everything after the first comment line is considered the start of a new statement.  Big bad compilation error.  Ugly.

This isn't much of a problem when you're developing new code, but when you're maintaining existing code it's often necessary to comment out sections of old code and add remarks that tell why and when the code was removed.  Visual Basic makes this impossible.  It's bad enough that the problem exists in VB 6 and VB Script  It's almost criminal that the problem persists in a "modern" language like Visual Basic .NET.