Thursday, February 28th, 2008

Every keystroke is a prisoner – a neat commenting trick

Category: JavaScript, PHP, Tip

<p>My favourite kind of tutorial or trick are the ones that are very easy to do but make a lot of sense – you know the ones that make you slap your forehead and say “why didn’t I do that before?”.

Dirk Ginader blogged about a commenting trick (in German) that is one of these. He rightly claims that when you develop, you will comment and uncomment a lot as you optimise and test your code. This means either inserting lines with comment and deleting those lines, adding and deleting the /* */ by hand or using a shortcut of your editor of choice (I loved Homesite for ctrl+shift+m). In any case, it means highlighting several lines to comment in or out.

If you however use the following syntax then you only need to delete one slash to comment and uncomment a section:

javascript
< view plain text >
  1. foo();
  2. /*
  3. bar();
  4. baz.foo = 200;
  5. return{
  6.   dolly:clone()
  7. }
  8. // */

The trick is the // before the closing */. With this you can just add another slash before the opening one and uncomment the block this way.

javascript
< view plain text >
  1. foo();
  2. //*
  3. bar();
  4. baz.foo = 200;
  5. return{
  6.   dolly:clone()
  7. }
  8. // */

This works in many languages, alas not in CSS as there is no single line comment.

Update: in CSS you can just delete and add and remove an asterisk:

  1. .test{
  2.     border:1px solid red;
  3.     /*/
  4.     background-color:blue;
  5.     /**/
  6.     margin:1em;
  7. }
  1. .test{
  2.     border:1px solid red;
  3.     /**/
  4.     background-color:blue;
  5.     /**/
  6.     margin:1em;
  7. }

Posted by Chris Heilmann at 3:57 am
16 Comments

+++--
3.9 rating from 45 votes

16 Comments »

Comments feed TrackBack URI

Nice trick. Doesn’t work with TSQL though, but then if you’re using MS IDEs you have the Ctrl+K,Ctrl+C and Ctrl+K,Ctrl+U key chords to comment and uncomment selected text respectively.

Comment by Ben — February 28, 2008

Update: Works in CSS, too.

Comment:
.test {
border:1px solid red;
/*/
background-color: blue;
/**/
margin: 1em;
}

Uncomment:
.test {
border: 1px solid red;
/**/
background-color: blue;
/**/
margin: 1em;
}

Comment by fraggy — February 28, 2008

@fraggy yes, he updated the post and I was just about to edit this one. Oh well, I still will for those who are not comment subscribed.

Comment by Chris Heilmann — February 28, 2008

In JS, I often use the following 2 comment blocks toggle:

/* //

Comment by Mathieu \'p01\' Henri — February 28, 2008

Let’s try that one more time and see if Ajaxian borks the comment again, shall we ?


/* // toggles the comment blocks
NAY
/*/
YAY
/**/

Comment by Mathieu \'p01\' Henri — February 28, 2008

Good idea! Unfortunately doesn’t work with Perl…

Comment by Rommeech — February 28, 2008

@Mathieu, perl has a distinct advantage; the Smart::Comments module on CPAN. Its not exactly the same, but I find it an invaluable tool during the testing/debugging process

Comment by evank — February 28, 2008

The extensible key commands (that generated wrapper tags/text around selection) of Homesite was a killer feature — this is one of the main reasons I’ve continued using it 5 years after its final release.

I’ve started using Aptana (Rails + Javascript) and really miss the functionality — I’ve tried to duplicate it with eclipseMonkey but it’s hit or miss.

Another fun tip, is to use undo (ctrl+z) as a mechanism for ‘jumping back’ to previous code block (hit redo/ctrl+y once you are there).

Comment by sciamop — February 28, 2008

I don’t understand why you would use this technique since any decent text editor or IDE has this ability with a keyboard shortcut, but this is pretty cool if you’re still using Notepad for your development. But then again – if you were using Notepad – you wouldn’t get the syntax highlighting which would make this technique pretty unreadable.

Comment by Andy Kant — February 28, 2008

Yay! Homesite FTW! I have Aptana/Eclipse with CFEclipse and I never use it. ColdFusion Studio is still doing it for me even today. I hate this website btw. I come in in the morning at 9 ready to work and I say to myself ‘I’ll just check the RSS feed of Ajaxian to see if there’s anything interesting’. 45 minutes later I have to physically peel myself away to get started on my work. Absolutely invaluable. Thanks to all who spend the time to keep it current!

Comment by starkraving — February 28, 2008

…i dig it…

Comment by Mark Holton — February 28, 2008

This was blogged last year at http://51elliot.blogspot.com/2007/09/comment-switches.html

Comment by DarrenDR — February 29, 2008

In PHP, it’s even easier to use the single # character to comment/uncomment blocks of code:
/*
..code
*/

to:

#/*
..code
#*/

Comment by SeanO — March 1, 2008

there is another neat trick.
disable first
tabTitle.innerHTML = /* [a href="+tab.link+" rel="nofollow">'+tab.title+''; // */ tab.title;

disable second
tabTitle.innerHTML = [a href="+tab.link+" rel="nofollow">'+tab.title+''; // */ tab.title;

Comment by sekostar — March 11, 2008

This looks like one of those things that is clever but not wise. It’s convenient while you’re working intensely with that block of code–but when you’re done, you (maybe) have code that looks disabled but isn’t. Not so easy for other developers (or you, later) to follow.

Not a problem if you remember to remove the confusing comments when you’re done, but, well, I never remember stuff like that.

Comment by eostrom — March 13, 2008

Very, very neat trick.

I use Komodo, and the Ctrl + 3 to comment and Ctrl + Shift + 3 to uncomment make my life easier.

Comment by hdragomir — May 15, 2009

Leave a comment

You must be logged in to post a comment.