PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Funky /*, */, // comment trick

Dave Johnston · 19 Aug 2003, 15:47 UTC
Eek - Can anyone spot the syntax-colouring 'errors'? :) The code/colouring works fine in MSVC!

Just discovered this trick this afternoon and found it quite handy with all my debugging... wondered if anyone else uses it?

Code:

// good code!
printf("elephants eat grass");
x = y * 14;
pNtr = pNtr + x;
/* // buggy code!
printf("elephants don't eat grass");
y = x * 14;
pNtr = pNtr - iApples;
*/


If I wanted to switch from the top code to the bottom code, I'd usually move the slash-star's around so that I get this:

Code:

/*// good code!
printf("elephants eat grass");
x = y * 14;
pNtr = pNtr + x;
*/ // buggy code!
printf("elephants don't eat grass");
y = x * 14;
pNtr = pNtr - iApples;


But that's a lot of work... and I'm a lazy programmer who can't afford that many keystrokes. So, now I do this:

Code:

// good code!
///* // note '//'
printf("elephants eat grass");
x = y * 14;
pNtr = pNtr + x;
/*/
printf("elephants don't eat grass");
y = x * 14;
pNtr = pNtr - iApples;
//*/


Notice the top section of code is commented-in (ie, will be compiled), but the bottom section is commented-out (ie, will be ignored). Now I can just remove a '//' and switch it round!

Code:

// good code!
/* // REMOVED '//'
printf("elephants eat grass");
x = y * 14;
pNtr = pNtr + x;
/*/
printf("elephants don't eat grass");
y = x * 14;
pNtr = pNtr - iApples;
//*/


There. Simple.

Yes, it is a slow day :)
I'm lazier than you, Dave. I just use #if.
Dave Johnston · 19 Aug 2003, 16:03 UTC
Digby wrote:
I'm lazier than you, Dave. I just use #if.


I bet it's not as much fun though.
mlepage · 21 Aug 2003, 15:50 UTC — Commenting vs. Conditional Compilation
The two methods are appropriately named commenting and conditional compilation.

Here's a rough style guide: use comments for prose comments, and use conditional compilation to compile your code conditionally.

See how appropriately named those techniques are?

Put in that perspective, using comments to conditionally compile code is a misuse of a technique. It becomes less clear what is comment (perhaps including sample code) and what is disabled code (that could be reenabled). Further, comments do not nest, making it hard to disable large chunks of commented code.

Please, just use comments for commenting, and macros for conditional compilation.
If someone comments out a section of code and doesn't add a textual comment saying why the code is commented out, I would assume it's not because this is sample code. The programmers that are anal enough to go about decorating their source with sample code are the same type of people that put text in the sample explaining this. At least, that's what I've seen in my 15 years of being a software engineer.
Dave Johnston · 21 Aug 2003, 18:43 UTC
I knew there were to be names associated with commenting styles, but I'd never seen conditional comments before, so was quite surprised when I 'invented' them myself!

They are pretty ugly IMO - even with descriptive text, the syntax of conditional commenting is untidy and as every programmer knows (but rarely abides to), good code is clean code :) The commenting symbols have no structure - at least with a slash-star you know there has to be a star-slash somewhere else for it....
mlepage · 21 Aug 2003, 19:41 UTC — Clarification
Let me clarify. Commenting is called "commenting," and should be restricted to comments and function-header-type-stuff. Using macros to decide which code to compile is called "conditional compilation" and is the preferred way to disable code.

Using comments to disable code, I usually hear spoken as "commenting out code," and is a practice I frown upon. Using macros is the preferred way to disable code.

When I find commented out code in a code review, I want to see it either disabled with an appropriate flag, or removed from the source (still accessible through source control).

My rule of thumb is: comments contain comments, not code.

An exception is a short usage example, if you're putting documentation type comments in function headers. I've tended away from that myself, as to me it indicates that your function isn't very self-documenting.
i use a trick like this using macros. it can easily be expanded to work using/checking defines to switch (check _DEBUG_ (I think this is it in eVC???) define) modes.

Code:

#if DEBUG // DEBUG != 0 but still needs to be defined
#define SWITCH_DEBUG(x) x
#define SWITCH_RELEASE(x)
#else
#define SWITCH_DEBUG(x)
#define SWITCH_RELEASE(x) x
#endif

#define SWITCH(x,y) \
SWITCH_DEBUG( x ) \
SWITCH_RELEASE( y )

void myfunc(void) {
SWITCH( // debug code
cout << "look what i can do" << endl;
, // release code
clog << "Just log the output instead";
clog << endl;
// it is also multiline
)
}



NOTE: i only tested this using GCC 3.2 on my linux box.
Note that the code is even more usefull if packaged in a header... but you knew that. This just helps readability compaired to ugly comments
Pejo Software - Per · 22 Aug 2003, 22:13 UTC
Digby wrote:
If someone comments out a section of code and doesn't add a textual comment saying why the code is commented out, I would assume it's not because this is sample code. The programmers that are anal enough to go about decorating their source with sample code are the same type of people that put text in the sample explaining this. At least, that's what I've seen in my 15 years of being a software engineer.
:)