Most of you might comment out code like this:
...
main();
// alternateMain();
...
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();
/**/
If you want to uncomment, just add a *
to the first line:
/**/
experimental();
/**/
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();
/**/
Now, the first block is commented, but the second isn't. Just add a *
:
/**/
experimental();
/*/
stable();
/**/
Aaand it was toggled and we are running experimental()
!
You can even put them inline:
use(/**/ stable /*/ experimental /**/);
If you want to comment out both, just remove the *
in the middle:
/*/
experimental1();
//
experimental2();
/**/
And if you want to uncomment both, add a *
in the middle:
/**/
experimental1();
/**/
experimental2();
/**/
Awesome right?
Now, this looks like the end of it, but there's more!
Check this out:
/**/
experimental1();
/**/
experimental2();
/**/
give();
/**/
If you want to comment out a line, just remove one ending /
from the top of that line:
/**/
experimental1();
/**
experimental2();
/**/
you();
/**/
Also works for multiple lines!
/**/
experimental1();
/**
experimental2();
/**
up();
/**/
You can pretty much infinitely extend that. And of course, don't let these comments past the 'just testing stuff' phase...
Top comments (5)
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.
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)
Nice! I use to do this all the time; nowadays I lazily make the code editor do all the work 😝
Yep.
CMD+/
. Done.Nice article.