7 ms·
The One Character Block Comment
- jff 17y agoThe 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.
- skip 17y agoIt'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.
- jasonkester 17y agoIt'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.
- latortuga 17y agoNot to mention TextMate, e (the windows textmate clone) and vi...I feel like this is a pretty basic text editor feature, for programmers at least.
- mixmax 17y agoIt's probably telling that his code sample is PHP 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.
- bkudria 17y ago(Disclaimer: I work in PHP) PHP is popular. But the language sucks. Big-time. Popularity does not mean something is automatically good, in a technical sense.
- mixmax 17y agoAbsolutely 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.
- moe 17y agoMy 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.
- philwelch 17y agoSo was (is?) COBOL.
- 17y ago
- rwolf 17y agoFor the gedit kids feeling left out in the audience, check out the plugin "code comment". Who loves you, baby?
- jackdawjack 17y agouuh surely most editors can do this for you? m-x comment-region in emacs for instance
- wmeredith 17y agoI think the point of this is it's application agnostic.
- shimon 17y agoEmacs 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". :)
- jf 17y agoYes! 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.
- jseifer 17y agoIt's usually command / in TextMate.
- huhtenberg 17y agoAnyone who discovered, went bananas for and then eventually stopped using this trick on their own, raise your hand. :)
- jcromartie 17y agoIn C I don't use block comments, I just use: #if 0 ... #endif One character, no comment syntax hacks.
- thunk 17y agoI 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.
- Periodic 17y agoThis 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.
- waldrews 17y agoPlease 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.
- Mintz 17y agoI think the purpose the author shared this technique for is when you need to constantly comment/uncomment a block of code for testing. I agree with you, though. Once you're done with your experiment, reset the commented lines to something more conventional.
- moe 17y agoEven 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.
- pbhjpbhj 17y agothe 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!
- mjgoins 17y agoOh if I could upvote this comment more times I would. The example code here made me immediately think of the lines of code that caused the Debian SSL catastrophe.
- nollidge 17y agoHe keeps saying "backslash" when it's a forward-slash (or just "slash").
- pbhjpbhj 17y agoSuppose it depends on whether your normal language is rtl or ltr?
- akronim 17y ago> this trick will work with any language that supports both single-line and block style comments isn't this just a horrible workaround for not having a debugger?
- nimbix 17y agoOr, you could use an editor more advanced than notepad.exe and use a "toggle comment" command which which allows you to switch between //line 1 //line 2 //... //line N ...and... line 1 line 2 ... line N ...with more flexibility than using a syntax trick.
- dfranke 17y agoThis is also an old trick to test whether your code was compiled as a C++ or not: 2//**/2 == 1) ? "C" : "C++"
- timf 17y agoIs there a situation where you would need this instead of "#ifdef __cplusplus"? e.g. http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html#faq-32.4 http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html#...
- dfranke 17y agoNo. It's just for hack value.
- yason 17y agoThis 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.
- bodhi 17y agoSeems 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.
- vlucas 17y agoI 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.