Thursday, February 28th, 2008
Every keystroke is a prisoner - a neat commenting trick
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:
-
-
foo();
-
/*
-
bar();
-
baz.foo = 200;
-
return{
-
dolly:clone()
-
}
-
// */
-
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.
-
-
foo();
-
//*
-
bar();
-
baz.foo = 200;
-
return{
-
dolly:clone()
-
}
-
// */
-
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:
-
.test{
-
border:1px solid red;
-
/*/
-
background-color:blue;
-
/**/
-
margin:1em;
-
}
-
-
.test{
-
border:1px solid red;
-
/**/
-
background-color:blue;
-
/**/
-
margin:1em;
-
}
-












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.
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;
}
@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.
In JS, I often use the following 2 comment blocks toggle:
Let’s try that one more time and see if Ajaxian borks the comment again, shall we ?
Good idea! Unfortunately doesn’t work with Perl…
@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
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).
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.
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!
…i dig it…
This was blogged last year at http://51elliot.blogspot.com/2007/09/comment-switches.html
In PHP, it’s even easier to use the single # character to comment/uncomment blocks of code:
/*
..code
*/
to:
#/*
..code
#*/
there is another neat trick.
disable first
tabTitle.innerHTML = /* ‘‘+tab.title+’‘; // */ tab.title;
disable second
tabTitle.innerHTML = ‘‘+tab.title+’‘; // */ tab.title;
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.