DEV Community

Siddharth
Siddharth

Posted on • Updated on

Efficiently commenting code

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...

Latest comments (5)

Collapse
 
val09865 profile image
val09865

I don't think this is a goods advice. Most IDE have a comment toggle fracture where you juste select some lines of code then press a simple shortcut and the IDE automatically add the comment character depending on the language of the file. You don't have to think about adding or removing character.
Your advice only apply to people who use basic text editors which is almost no one.

Collapse
 
siddharthshyniben profile image
Siddharth

It depends on how you code. I prefer to never use the mouse when possible, so selecting text is a no no (of course we can select by keyboard, but that takes a while). If you are used to the mouse, go ahead and use that method.

This is the method I personally use (in Vim) since it makes it so easy to toggle between two blocks of code by deleting just one character (I usually set a mark at that character so I can jump there easily)

Collapse
 
auroratide profile image
Timothy Foster

Nice! I use to do this all the time; nowadays I lazily make the code editor do all the work 😝

Collapse
 
jamesthomson profile image
James Thomson

Yep. CMD+/. Done.

Collapse
 
vishal2369 profile image
Vishal2369

Nice article.