Please please don't do this. It hurts readability, and relies on a subtle grammar ambiguity. Without a highlighting editor (e.g. reading a diff in a code review) I'll be scratching my head trying to read this. And tools like editors that have an imperfect parser are likely to get confused.
There's a chance you'll confuse yourself (or your code reviewer or other reader) and spend an hour debugging to find what you thought was commented out wasn't. That's not worth it to save one keystroke.
Even then, any editor worth its salt supports comment toggling with a single keystroke anyways. Good editors (vim, cough) can even toggle lines or paragraphs regardless of cursor position.
So I agree, this hack is more trouble than its worth.
the FF webdeveloper extension is the one I find easiest for live CSS editing - though I'm using firebug more and more - it doesn't have comment toggling (it's just a text field). Such tricks are handy.
Unless you're trying to work them around CSS comment hacks!
Emacs users: try the comment-dwim command, usually bound to M-;. This will toggle the commented-ness of the selected code and be relatively smart about it. "dwim" stands for "do what I mean". :)
Yes! And for the times when you are in a mode that comment-dwim doesn't know the comment syntax for, you can use the string-rectangle (C-x r t) command to comment regions.
This is fun and has some hack value but I hope nobody ever uses such a kludge in real code.
It totally fails with crappy C compilers that decide to feature C++ comments unannounced. IIRC gcc does this unless you give -ansi or -std=c89. And C99 officially supports the // -comments but hasn't got C++ features, of course.
It's a nice trick, but I will probably stick to Ctrl-K-C (block comment) and Ctrl-K-U (block uncomment) in Visual Studio, just out of habit. I hate leaving big block comments in the middle of a module anyways: hurts readability.
It's probably telling that his code sample is PHP. Though I seem to remember Eclipse having support for CTRL+/ style block commenting/uncommenting. Most other good IDEs (and emacs, as noted below) do it too.
But yeah, I sometimes take for granted how nice VS.NET is at these little detail things (though I use resharper-style CTRL+/ bindings for this particular feature). Thanks to this author and his pain for reminding me how good we have it.
Excuse me, but last time I checked PHP was one of the most widely deployed languages in the world, which a lot of people smarter than you and I spend their working time hacking. Can we please stop the PHP bashing.
Absolutely agree, and judging by the score of my parent comment maybe I didn't make myself clear.
I don't mean to say that PHP is a perfect language by any means, far from it. My point was more that just because some code example is written in PHP you can't automatically infer that the code is inferior. I personally think that's terribly arrogant, but that may just be me.
My point was more that just because some code example is written in PHP you can't automatically infer that the code is inferior.
Perhaps I'm terribly arrogant but I do make that inference. Simply because the number of good coders who would pick PHP when given a choice is very, very, very, very, very, very, very, very, very, very, very small. And shrinking.
I'm sure that if it was up to the programmers many big sites would be hacked out in Haskell, but as it is an awful lot of them happen to be done in PHP. And Coders have jobs and bosses.
I didn't intend any PHP bashing. I was just pointing out that PHP is quite a way behind in the IDE department. Apart from some half-hearted Eclipse plugins like PDT, nobody has stepped up to the plate. Certainly not in the way that Netbrains did for Java or MS did for .NET.
I've done my share of PHP development over the years, and have clients to this day that need their PHP sites maintained. And every time I need to touch those sites, I wish I had a tool powerful as, say, ColdFusion Studio 4.5, which came out in 2001.
If you can point out such a development environment for PHP, I'd be quite grateful.
I find it really helpful to have a duplicate-and-comment-block command, so you can immediately start hacking on an uncommented dup. Used with another command to dump blocks to a junk file, it's like a wee block-level SCM.
This is a pattern I tend to use as well because it lets you reference known working code when changing things, and easily switch between development and stable code. However, usually it's just one line, so "copy, paste, comment, move" works well enough.
The real solution is to make your code work properly every time.
Or, in Plan 9's acme editor, we just highlight the desired text and execute the following command in the tag area:
Edit s/^/\/\//g
Then when we're ready to go back, we just select again and do:
Edit s/^\/\///g
which is the same as the previous command but with one forward slash moved.
Alternately, in almost every case it's easy enough to just stick in /* */ comments around it and delete them as necessary.
I must say I didn't expect so much hate for this comment hack. Hack? That's right, it absolutely is a hack - and it's definitely not the most elegant solution.
I think a lot of people really missed the point here, and used comments as a way to flaunt their clearly more impressive and infinitely superior geek prowess by using IDEs and emacs.
The point was to present a solution that was fairly platform and language agnostic. If I had included an IDE shortcut in the article, I would have had to go through a long list of all the IDEs out there and all their corresponding shortcuts. I would have then been inevitably flamed for missing some or for not representing a specific programming language.
Oh, and don't forget to bash PHP or point out how much I suck at programming for using PHP. Because everyone knows only cool developers and true geeks don't use PHP.
Seems to be a lot of hate on this trick here. One difference between this trick and "I just press <shortcut key> in my IDE" is that this will comment out one block of code, but will also uncomment a second block at the same time.
There's a chance you'll confuse yourself (or your code reviewer or other reader) and spend an hour debugging to find what you thought was commented out wasn't. That's not worth it to save one keystroke.