Prefer open/close syntax when commenting

In many languages, there are two ways to comment:

  • Open/close (often used for multi-line comments), i.e. /* my comment */
  • Single open (often used for end-of-line comments), i.e. // my comment

This may be trivial for you, but if your language does not rely on line breaks to mark the end of a code line, then it may actually make more sense to stick with the multi-line comment syntax as a best-practice.

Take JavaScript for example, which is often minified (and not always by a dedicated minifier):

var a = 'b'; // initialize a
var b = 'c';

Now, let's minify it to one line:

var a = 'b'; // initialize a var b = 'c';

Our second assignment statement is commented out.

While many folks out there make use of online services to minify JavaScript, this may not always be feasible (such as when using a home-brewed script to move your development site files to a live version, hence minifying JS code).

Why follow me on Twitter?

  • I tweet about new technologies, services or libraries I find interesting
  • Yeah, sometimes I'll post a pet-peeve or rant about something trivial
  • If I discover something that made my web development life easier, I share it
  • I'll shout out any handy tip that I think might be useful to other devs

This entry was posted in JavaScript. Bookmark the permalink.

Updated: 2012-04-23

Phil LaNasa follow us in feedly