12.5. Boolean Variables

 < Free Open Study > 

It's hard to misuse logical or boolean variables, and using them thoughtfully makes your program cleaner.

Use boolean variables to document your program Instead of merely testing a boolean expression, you can assign the expression to a variable that makes the implication of the test unmistakable. For example, in the next fragment, it's not clear whether the purpose of the if test is to check for completion, for an error condition, or for something else:

Java Example of Boolean Test in Which the Purpose Is Unclear
if ( ( elementIndex < 0 ) || ( MAX_ELEMENTS < elementIndex ) ||    ( elementIndex == lastElementIndex )    ) {    ... }

Cross-Reference

For details on using comments to document your program, see Chapter 32, "Self-Documenting Code."


Cross-Reference

For an example of using a boolean function to document your program, see "Making Complicated Expressions Simple" in Section 19.1.


In the next fragment, the use of boolean variables makes the purpose of the if test clearer:

Java Example of Boolean Test in Which the Purpose Is Clear

finished = ( ( elementIndex < 0 ) || ( MAX_ELEMENTS < elementIndex ) ); repeatedEntry = (  elementIndex == lastElementIndex ); if ( finished || repeatedEntry ) {  ... }

Use boolean variables to simplify complicated tests Often, when you have to code a complicated test, it takes several tries to get it right. When you later try to modify the test, it can be hard to understand what the test was doing in the first place. Logical variables can simplify the test. In the previous example, the program is really testing for two conditions: whether the routine is finished and whether it's working on a repeated entry. By creating the boolean variables finished and repeatedEntry, you make the if test simpler: easier to read, less error prone, and easier to modify.

Here's another example of a complicated test:

Visual Basic Example of a Complicated Test

If ( ( document.AtEndOfStream() ) And ( Not inputError ) ) And _    ( ( MIN_LINES <= lineCount ) And ( lineCount <= MAX_LINES ) )  And _    ( Not ErrorProcessing() ) Then    ' do something or other    ... End If


The test in the example is fairly complicated but not uncommonly so. It places a heavy mental burden on the reader. My guess is that you won't even try to understand the if test but will look at it and say, "I'll figure it out later if I really need to." Pay attention to that thought because that's exactly the same thing other people do when they read your code and it contains tests like this.

Here's a rewrite of the code with boolean variables added to simplify the test:

Visual Basic Example of a Simplified Test
 allDataRead = ( document.AtEndOfStream() ) And ( Not inputError ) legalLineCount = ( MIN_LINES <= lineCount ) And ( lineCount <= MAX_LINES ) If ( allDataRead ) And ( legalLineCount ) And ( Not ErrorProcessing() ) Then       <-- 1    ' do something or other    ... End If

(1)Here's the simplified test.

This second version is simpler. My guess is that you'll read the boolean expression in the if test without any difficulty.

Create your own boolean type, if necessary Some languages, such as C++, Java, and Visual Basic have a predefined boolean type. Others, such as C, do not. In languages such as C, you can define your own boolean type. In C, you'd do it this way:

C Example of Defining the BOOLEAN Type Using a Simple typedef
typedef int BOOLEAN;

Or you could do it this way, which provides the added benefit of defining true and false at the same time:

C Example of Defining the Boolean Type Using an Enum
enum Boolean {    True=1,    False=(!True) };

Declaring variables to be BOOLEAN rather than int makes their intended use more obvious and makes your program a little more self-documenting.

 < 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