Efficiently commenting code

Siddharth - Sep 5 '21 - - Dev Community

Most of you might comment out code like this:

...
main();
// alternateMain();
...
Enter fullscreen mode Exit fullscreen mode

But this isn't a really efficient way to comment. If you need to toggle that comment, you need to delete/add at least two characters. And also, sometimes you are trying two different things, and need to comment one and uncomment another. Things get complex really quick

Here, I'll show you some efficient ways to comment out stuff. This is based on JavaScript, but should work for other languages like CSS, C, etc. (And you probably can adapt it to other languages).

Toggleable comment

For commenting a block of code and making it easier to uncomment, you can do this:

/*/
experimental();
/**/
Enter fullscreen mode Exit fullscreen mode

If you want to uncomment, just add a * to the first line:

/**/
experimental();
/**/
Enter fullscreen mode Exit fullscreen mode

You can't "nest" comments here, so one trick you can use is to put the comment text in the last comment:

No:

/*/
never();
/* Some explanatory text here blabla */
experiment2();
/**/

Yes:

/*/
gonna();
experiment2();
/* Some explanatory text here blabla */

Nice?

We can pretty easily extend this. Check this out:

/*/
experimental();
/*/
stable();
/**/
Enter fullscreen mode Exit fullscreen mode

Now, the first block is commented, but the second isn't. Just add a *:

/**/
experimental();
/*/
stable();
/**/
Enter fullscreen mode Exit fullscreen mode

Aaand it was toggled and we are running experimental()!

You can even put them inline:

use(/**/ stable /*/ experimental /**/);
Enter fullscreen mode Exit fullscreen mode

If you want to comment out both, just remove the * in the middle:

/*/
experimental1();
//
experimental2();
/**/
Enter fullscreen mode Exit fullscreen mode

And if you want to uncomment both, add a * in the middle:

/**/
experimental1();
/**/
experimental2();
/**/
Enter fullscreen mode Exit fullscreen mode

Awesome right?

Now, this looks like the end of it, but there's more!

Check this out:

/**/
experimental1();
/**/
experimental2();
/**/
give();
/**/
Enter fullscreen mode Exit fullscreen mode

If you want to comment out a line, just remove one ending / from the top of that line:

/**/
experimental1();
/**
experimental2();
/**/
you();
/**/
Enter fullscreen mode Exit fullscreen mode

Also works for multiple lines!

/**/
experimental1();
/**
experimental2();
/**
up();
/**/
Enter fullscreen mode Exit fullscreen mode

You can pretty much infinitely extend that. And of course, don't let these comments past the 'just testing stuff' phase...

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .