Toby Smith

The best comment syntax

24th April 2013

I’m not going to talk about heirarchical comments here. I just wanted to write about a little nugget that I’ve found very useful, so that you can use it too!

//*///

This is valid in javascript, and I’m sure you could something similar in C and C++ too.

Why is this cool?

function GetSomeValue() {
//*///
var a = CHUNK OF CALCULATION
return a;
//*///
var b = CHUNK OF CALCULATION
return b;
}

With the deletion of the first character of the first “//*///” you can comment out the first return. (Imagine this was a substantial chunk of early outs).
The second instance will act as the closing comment for it, without having to add one. It’s useful if you know you’re going to be flicking bits of code on/off for a brief while.

This comment syntax also allows you to add inline comments afterwards:

function GetSomeValue() {
/*/// Inline comments are valid here!
var a = CHUNK OF CALCULATION
return a;
//*/// Also valid here!
var b = CHUNK OF CALCULATION
return b;
}

All in all they’re pretty nifty.
Personally I think I’d go for something like:
#{
#}

#{ being equivalent to /*///.
and
#} being equivalent to //*///

You could even extend this so that:
#{{
#}}
Would also be valid comments. And wouldn’t be closed by #}
This would mean that you could plop multi-line comments in various places, and not have to remove them later if you want to comment out chunks of code later!

function GetSomeValue() {
return 4;
#{{ TESTING ASSUMPTION
#{ This code does some calculation
about various things!
#}
var a = CHUNK OF CALCULATION
return a;
#} Might want to block off this code later
var b = CHUNK OF CALCULATION
return b;
#}}
}

I know it’s not the cleverest chunk of code in the world. Or actually does anything…or is really “code”. But, it might save you some time!

Leave a Comment

http://www.thjsmith.com/feed">RSS Feed
  • Terms of use
  • Privacy Policy