mycroes

There's always time to play

Tuesday, April 14, 2009

Magic comments

So you're writing some code, and there seems to be an error somewhere. As a real programmer, you're going to use printf or echo to debug your code. I happen to be writing a lot of PHP lately, so I also debug with var_dump. Not that it really matters, because after all it boils down to eliminating lines with errors, and as long as you use a programming language that supports // for single line comments and /* ... */ for multiline comments you can use magic comments.

Honestly, there's no magic involved, just a bit of logic. Let's say there's some code consisting of 3 blocks of lines:
[block 1]

[block 2]

[block 3]

If I want to comment one block, I could simply put /*, */ around it like this:
/*
[block 1]
*/

[block 2]

[block 3]

However, soon enough I figure I needed to comment block 2 for a while. Because I'm lazy, I try to type no more than needed, so it becomes this:
//*
[block 1]
/*/
[block 2]
*/

[block 3]

I'm almost happy now, but it seems I need to switch back to having block 1 commented again instead of block 2... Of course I can remove the added characters, or I can just add a few more:
/*/*
[block 1]
/*/

[block 2]
/*/
//*/

[block 3]

Now if I decide that I need to comment block 2 again, I only need to edit one character:
//*
[block 1]
/*/
[block 2]
/*/

//*/
[block 3]

And it's actually possible to chain these by using a /*/ between 2 blocks. If the block in front was commented, the next block won't be commented. If the block in front however was not commented, this block will be commented. And there you go, boolean logic with comments, it's almost magic.

1 comment:

DINDA AULIA AZZAHRA PUTRI PURWANTO said...

This 'magic comment' approach to toggling code blocks is a brilliant software parallel to how we diagnose hardware faults. From an electrical engineering perspective at Telkom University Surabaya, it works exactly like using physical jumper pins or single-pole double-throw (SPDT) switches to isolate specific circuit branches during a bench test. Finding low-overhead ways to cleanly route signals between test paths without tearing down the entire breadboard layout is a fantastic debugging skill. https://bee-sby.telkomuniversity.ac.id/integrasi-robotika-dan-ev-saat-mobil-jadi-robot-cerdas-di-jalanan/