I ran across an interesting little tidbit in a JavaScript book, I think, or online. Likely I saw it in something Douglas Crockford wrote, but couldn’t be certain. Anyway, it has to do with comments in JavaScript. The language supports two comment types:
1. Single line using the double slash:
// This is a comment.
2. Multiline (also called block) using the slash-asterisk combination:
/* This comment can go over multiple lines. */
The argument made (wherever it was made) is that you shouldn’t use the multiline comment style as the */ character combination can appear in JavaScript code. Namely, it might appear in a regular expression match, when slashes are used to delineate the pattern and the asterisk is a pattern modifier that looks for zero or more of something. A syntax error will arise if you use block comments around some code that includes a problematic regular expression:
/* some code var x = 'some string'.search(/[a-z]*/g); some code */
That’s obviously a useless example in itself, but it shows a situation in which a problem is caused by using block comments.
All that being said, I wouldn’t suggest that you avoid using multiline comments. They’re quite handy, both for adding lots of documentation and for debugging purposes (by making blocks of code inert). But the potential conflict is something to be aware of, particularly when you apply multiline comments to render some code non-executing and all of a sudden have a syntax problem.
Must have been old advice surely? Just checked JavaScript the Definitive Guide (fifth edition) from O’reilly by David Flanagan and there was no discouraging against using multiline comments (nor was there in the other two books). Most people should/would/could use an editor with syntax highlighting, that would easily spot any issues.
Thanks for the response (and for continuing to check out the blog). Actually, I’m almost positive I read this in Douglas Crockford’s newest book, JavaScript: The Good Parts. But regardless, you could end up with syntax errors using block comments, so it’s not really a new/old thing. Still, I agree that I’m not going to stop using them, but I thought it was an interesting thing to be aware of. And, of course, some programmers take every little possible problem quite seriously!
Found it here: http://oreilly.com/catalog/9780596517748/preview
“In JavaScript, those pairs can also occur in regular expression literals, so block comments are not safe for commenting out blocks of code.”
Have to admit, that is the most ridiculous paragraph I have read in a Programming book so far.
Hey! Thanks for confirming the source. I still have half a brain left in me. It’s hard to argue with Crockford when it comes to JavaScript, but I agree that “not safe” is a stretch here. Thanks again for following up on this!
What really gets me is that CSS doesn’t allow for single line comments using the // method. It’s nice that CSS has the multi line comment, but sometimes it’s nice to be able to do just that one line…
But I digress, Crockford also recommends against using ++ to increment numbers in javascript. He’s definitely a cautious programmer.
Thanks for the comments. What’s Crockford’s argument against ++? Is it a matter of pre- and post- and precedence?