Single character commenting

It's a pretty common practice to comment and uncomment big chunks of code during the development and testing of software. Here's an odd little hack from the ajaxian blog that can make this a little easier for blocks that you're constantly flipping on and off during development.

For C style comments, the following will be commented out:

/*
if ( foo == bar )
{
  dosomething();
  return();
}
// */

And the addition of a single '/' will uncomment the block:

//*
if ( foo == bar )
{
  dosomething();
  return();
}
// */

In languages that don't have the single line comment, such as CSS, you can do the same thing with only the block level comments.
Commented:

/*/
min-height:100px;
/**/

Uncommented:
/**/
min-height:100px;
/**/

You are probably talking to your screen right now, saying, "hey Jason, that commenting trick is marginally useful at best." I can only respond by reminding you that every keystroke is a beautiful and unique snowflake that must be cherished and never wasted.

A neat commenting trick - Link


Recent Entries

Comments

Oldest comments listed first.

Posted by: Twan van Laarhoven on March 2, 2008 at 5:51 AM

There is also a trick for 'toggling' comments, a single character can comment out one piece of code, and uncomment another:

/*
inside_comment();
/*/
outside_comment();
//*/

//*
outside_comment();
/*/
inside_comment();
//*/


Posted by: Jujubee on March 2, 2008 at 7:13 AM

If you're in C or C++, use the preprocessor instead, like this

#if 0
if ( foo == bar )
{
dosomething();
return();
}
#endif

Changing the 0 to a 1 uncomments. Bonus, you don't have to worry if there is an existing /**/ comment block in the code you're commenting out. Double bonus, you can nest #if statements.


Posted by: Matt H on March 2, 2008 at 7:41 AM

ifdef's are way better for removing code because they always work, even if you are removing code that has comments in it


Posted by: aoeu on March 2, 2008 at 7:50 AM

I sometimes put something like this while writing css files:

/* *
* { outline: 1px solid red; }
/* */

a.


Posted by: Tom Ritchford on March 2, 2008 at 9:53 AM

I used to do almost exactly the same thing, depending on syntax highlighting to reveal the comments.

I do occasionally but almost every other time I use #ifdefs. Much long but MUCH more formal and it allows me to see the code that's being hidden in the syntax highlighting - or conversely, if someone's reviewing my code and doesn't have syntax highlighting, the fact that those lines of code are not being compiled is far more obvious with the #ifdefs.


Posted by: Michael on March 2, 2008 at 12:50 PM

You know, quickly toggling CSS comments (with their damn C-style comments only) has always driven me crazy. This is a great idea.


Posted by: James on March 2, 2008 at 1:44 PM

It may be neat for a while, but when you return to your code in 6 months, you'll find that the code is now less readable.


Posted by: Matthew on March 2, 2008 at 2:12 PM

You could also use the blockcomment plugin for vim. Admittedly, it's a little more than a single character (select block + \x and same to uncomment)


Posted by: Man on March 2, 2008 at 3:09 PM

If you need to put documentation, liscence, or other rather uniform (i.e. you want to script their insertion) headers on something written in scheme and C you can do something like:

;;/*
;; Commentary here
;;*/

and have only one script. Depending on compiler settings the C compiler might whine about empty statements, but there's really no way arround that one. This way you only need one headerify script.


Posted by: Jean-Claude Wippler on March 2, 2008 at 3:10 PM

FYI, the Lua language has this feature by design - with "--[[" on the opening line and "--]]" on the closing line. To disable the comment, insert a space on the first line, i.e. change it to "-- [[".

See http://www.lua.org/manual/5.1/manual.html#2.1


Posted by: Wolf on March 3, 2008 at 12:15 AM

Use an editor where you can just toggle in and toggle out of comments? Textmate for example.


Posted by: Robin Message on March 3, 2008 at 4:04 PM

I've used

/*/
Old Version
/*/
New version
/**/

in the past, which can be toggle between the two blocks with an extra * in the top line.


Posted by: Hiro Protagonist on March 4, 2008 at 7:53 PM

I use this frequently:
#if 1
// New code here
dosomethingelse();
#else
// old code here
dosomething();
#endif

Changing the 1 to a 0 allows you to quickly switch between the old and new code. If you're making multiple changes, just do:
#define NEW_CODE 1
#if NEW_CODE
#else
#endif
This allows flipping multiple blocks between old and new with a single change.


Posted by: Daniel McLaren on March 9, 2008 at 11:51 AM

I wanted to post about the comment toggling but Twan and Robin both beat me to it! It's funny to see how many people have discovered and use that little trick.


Posted by: MIke Dugdale on April 2, 2008 at 3:48 AM

If you want to read this in its original form (from 8 years ago, no less) and lots of other C tricks, then head over to http://www.gamedev.net/reference/articles/article1239.asp


Leave a comment


Subscribe to MAKE!Subscribe to MAKE Magazine!

Subscribe today, save 42% and get web access to MAKE free. MAKE Digital Edition is available only to subscribers.

$34.95 / 1 year
(4 Quarterly Issues)

Subscribe now


Void your warranty, violate a user agreement, fry a circuit, blow a fuse, poke an eye out. Make: The risk-takers, the doers, the makers of things... Welcome to Make: Online!


CRAFT Maker Shed Maker Faire MAKE television
MAKE: en EspaƱol MAKE: Japan


Check out all of the episodes of Make: television

Make: Science Room

Connect with MAKE

Be a MAKE fan on Facebook MAKE on Facebook
Visit our Facebook page and become a fan of MAKE!
MAKE on Twitter MAKE on Twitter
Follow our MAKE tweets!
MAKE Flickr Pool MAKE on Flickr
Join our MAKE Flickr Pool!
    make_tips on Twitter

    MAKE's RSS feed is here.
    Add MAKE to iGoogle - GoogleGoogle.
    How to add MAKE to your RSS reader - Real simple.
    Add MAKE on FriendFeed




    Maker SHED

    Advertise here with FM.

    Why advertise on MAKE?
    Read what folks are saying about us!

    Click here to advertise on MAKE!



    Subscribe to MAKE Magazine!

    Make: Online authors!

    Gareth BranwynGareth Branwyn
    Senior Editor


    Phillip TorronePhillip Torrone
    Senior Editor
    | AIM | Twitter


    Becky SternBecky Stern
    Associate Editor
    | AIM | Twitter


    Marc de VinckMarc de Vinck
    Contributing Writer
    | AIM | Twitter


    John ParkJohn Park
    Contributing Writer
    | Twitter


    Sean RaganSean Ragan
    Contributing Writer
    | Twitter


    Matt MetsMatt Mets
    Contributing Writer
    | AIM | Twitter


    Dale DoughertyDale Dougherty
    Editor & Publisher
    | Twitter


    Shawn ConnallyShawn Connally
    Managing Editor
    | Twitter


    Goli MohammadiGoli Mohammadi
    Associate Managing Editor

    Kip KayKip Kay
    Weekend Projects
    | AIM | Twitter


    Collin CunninghamCollin Cunningham
    Contributing Writer
    | AIM | Twitter

    Adam FlahertyAdam Flaherty
    Contributing Writer
    | AIM | Twitter



    More contributors: Mark Frauenfelder (Editor-in-Chief, MAKE magazine), Kipp Bradford (Technical Consultant/Writer), Chris Connors (Education), Diana Eng (Guest Author), Peter Horvath (Intern), Brian Jepson (O'Reilly Media), Robert Bruce Thompson (Science Room)

    Suggest a Site!

    Current Podcast

    itunesdl.gif Weekend Project: Making Char Cloth Learn how to make a cheap and effective fire starter made from an old t-shirt. To download The Char Cloth video click here and subscribe in iTunes. See Char Cloth in action with the Fire Piston from William Gurstelle.... More...

    Get the Make: Online sent via email
    Enter your email to receive Make: Online each day:



    MAKE Fascination video series brought to you by Dow

    Make: Education

    Important please read


    Subscribe to MAKE Magazine!

    Recent Posts from the Craft: Blog