Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
The One Character Block Comment (vancelucas.com)
44 points by vlucas on July 21, 2009 | hide | past | favorite | 37 comments


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.


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


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!


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


Anyone who discovered, went bananas for and then eventually stopped using this trick on their own, raise your hand.

:)


In C I don't use block comments, I just use:

    #if 0
      ...
    #endif
One character, no comment syntax 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 also an old trick to test whether your code was compiled as a C++ or not:

  2//**/2
    == 1) ? "C" : "C++"


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.


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


No. It's just for hack value.


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.


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


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


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.


Most coders don't have the choice.

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.


On their personal blog?


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.


So was (is?) COBOL.


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


For the gedit kids feeling left out in the audience, check out the plugin "code comment". Who loves you, baby?


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


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.


He keeps saying "backslash" when it's a forward-slash (or just "slash").


Suppose it depends on whether your normal language is rtl or ltr?


uuh surely most editors can do this for you? m-x comment-region in emacs for instance


I think the point of this is it's application agnostic.


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


It's usually command / in TextMate.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: