Null Statements

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

Example 19-23. 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 Controlling the Loop.

Example 19-24. 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.)

Example 19-25. 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:

Example 19-26. 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.189.251