19.3. Null Statements

 < Free Open Study > 

In C++, it's possible to have a null statement, a statement consisting entirely of a semicolon, as shown here:

C++ Example of a Traditional Null Statement
while ( recordArray.Read( index++ ) != recordArray.EmptyRecord() )    ;

The while in C++ requires that a statement follow, but it can be a null statement. The semicolon on a line by itself is a null statement. Here are guidelines for handling null statements in C++:

Call attention to null statements Null statements are uncommon, so make them obvious. One way is to give the semicolon of a null statement a line of its own. Indent it, just as you would any other statement. This is the approach shown in the previous example. Alternatively, you can use a set of empty braces to emphasize the null statement. Here are two examples:

Cross-Reference

The best way to handle null statements is probably to avoid them. For details, see "Avoid empty loops" in Section 16.2.


C++ Examples of a Null Statement That's Emphasized
 while ( recordArray.Read( index++ ) ) != recordArray.EmptyRecord() ) {}       <-- 1 while ( recordArray.Read( index++ ) != recordArray.EmptyRecord() ) {       <-- 2    ; }

(1)This is one way to show the null statement.

(2)This is another way to show it.

Create a preprocessor DoNothing() macro or inline function for null statements The statement doesn't do anything but make indisputably clear the fact that nothing is supposed to be done. This is similar to marking blank document pages with the statement "This page intentionally left blank." The page isn't really blank, but you know nothing else is supposed to be on it.

Here's how you can make your own null statement in C++ by using #define. (You could also create it as an inline function, which would have the same effect.)

C++ Example of a Null Statement That's Emphasized with DoNothing()
#define DoNothing() ... while ( recordArray.Read( index++ ) != recordArray.EmptyRecord() ) {    DoNothing(); }

In addition to using DoNothing()in empty while and for loops, you can use it for unimportant choices of a switch statement; including DoNothing() makes it clear that the case was considered and nothing is supposed to be done.

If your language doesn't support preprocessor macros or inline functions, you could create a DoNothing() routine that simply immediately returns control back to the calling routine.

Consider whether the code would be clearer with a non-null loop body Most of the code that results in loops with empty bodies relies on side effects in the loop-control code. In most cases, the code is more readable when the side effects are made explicit, as shown here:

C++ Examples of Rewriting Code More Clearly with a Non-Null Loop Body
RecordType record = recordArray.Read( index ); index++; while ( record != recordArray.EmptyRecord() ) {    record = recordArray.Read( index );    index++; }

This approach introduces an additional loop-control variable and requires more lines of code, but it emphasizes straightforward programming practice rather than clever use of side effects. Such emphasis is preferable in production code.

 < Free Open Study > 


Code Complete
Code Complete: A Practical Handbook of Software Construction, Second Edition
ISBN: 0735619670
EAN: 2147483647
Year: 2003
Pages: 334

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net