Borland Online And The Cobb Group Present:


October, 1995 - Vol. 2 No. 10

C++ Language tip - Enabling and disabling code sections using comments

Programming by trial-and-error may not be the best approach, but there are times when it's necessary. If you're trying to write a specific function or code segment, you may be able to think of two or three ways to accomplish the same task. Obviously, you'll want to use the one that's more efficient, more accurate, or both. And you won't know which approach is the best one until you try all of them out.

In fact, creating two different algorithms for the same task has another advantage­­it can be an effective means of checking your basic design. After all, the odds are fairly small that you'll create two separate algorithms that both produce an incorrect result.

When you're ready to try one algorithm instead of another, you'll probably use the standard comment tokens to disable a single line (//) or a series of lines (/* */). In this article, we'll show how you can enable or disable large sections of code using a special combination of the standard comment tokens.

Token comments

The standard comment tokens behave in a predictable fashion. To place a comment on the end of a line, or to disable an entire line, you simply add the // token in the appropriate place, as in

int BarneyCt = 0; // Initialize running total

or

// DisabledFunctionCall(32);

In each case, the compiler ignores everything that follows the // token.

In contrast, you must use the /* and */ tokens in matched pairs. When the compiler processes the /* token, it will ignore everything it scans until it finds a */ token, at which point it begins processing again.

If you're trying to disable one section of code and enable another, this is the type of comment you'll probably use. Unfortunately, using this type of comment can become difficult because you must maintain the matched pairs of tokens.

Instead of using either type of comment token by itself, you can combine them to create a more powerful comment­­the combination comment. The combination comment has two forms: the start/disable/enable form

/*///

and the end form

//*///

To see how this combination of tokens works, let's examine each type individually.

The start/disable/enable comment

The start/disable/enable form of the combination comment contains two elements: /*/ and //. The first portion, /*/, acts as either the beginning of a comment section (if it appears first) or as the end of a comment section (if it appears after a /* token).

When the compiler first processes these three characters, it finds the /* token and begins ignoring all the characters and tokens that follow (including the / that appears right after the /* token). When the compiler encounters the /*/ combination again, it ignores the first character and then recognizes the */ token as the end of the commented section.

The second portion of the combination comment, //, allows you to place a description of the comment on the same line. It doesn't matter if the combination comment is at the beginning or the end of the commented section. If it's at the beginning, the compiler will ignore it anyway, since it's searching for the */ token. If it's at the end of a commented section, the compiler will process the // token and therefore ignore the remainder of the line.

The end comment

The end form of the combination comment contains the same sections as before, plus an additional slash (/) character at the beginning. The additional slash forces the compiler to treat the line as an unconditional end comment (in other words, it will always act as an end comment).

If you've disabled a section of code just prior to a line that contains the end combination comment, the compiler will find the embedded */ token and will resume processing the file on the next line. If this line appears by itself, the compiler disables everything following the initial double-slash (//) and then resumes processing on the next line.

To demonstrate how the combination comments work, we'll create a simple source file. Since the Integrated Development Environment (IDE) identifies comments automatically, you won't even need to create a compilable source file to test this tip.

Calling the comment line

Begin by launching the Borland C++ IDE. When the IDE's main window appears, choose New from the File menu. In the editing window, enter the code from Listing A.


Listing A: COMMENT.CPP

#include <iostream.h>

int main()
{
  /*/// start-disable-enable comment

  cout << "Algorithm #1" << endl;

  /*/// start-disable-enable comment

  cout << "Algorithm #2" << endl;

  //*/// end comment

  return 0;
} 

When you finish entering the code, examine the lines that are active and the lines that have become comments. Initially, the first cout statement is part of a comment block, and the second appears as normal code, as shown in Figure A.


Figure A - You can use the combination comment to temporarily disable one section of code...

Next, add a slash to the beginning of the first combination comment. When you do, the IDE will reinterpret the combination comments, enabling the first cout statement and disabling the second, as shown in Figure B.


Figure B - ...and then re-enable it with a single keystroke.

Conclusion

Comments are useful for helping other programmers follow your source code, but they're also valuable when you need to selectively enable or disable sections of code. By learning to use the combination comment, you can alternate between two algorithms or code segments easily.

Mark Timperly, a Borland C++ programmer in Westerley, Rhode Island, contributed the tip in this article. You can contact Mark via CompuServe at 70543,3621. To thank Mark for sending us this tip, we're sending him a check for $25. Send your Borland C++ tips to bc++_dev@merlin.cobb.ziff.com.

Return to the Borland C++ Developer's Journal index

Subscribe to the Borland C++ Developer's Journal


Copyright (c) 1996 The Cobb Group, a division of Ziff-Davis Publishing Company. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of Ziff-Davis Publishing Company is prohibited. The Cobb Group and The Cobb Group logo are trademarks of Ziff-Davis Publishing Company.