|
|
|
|
|
|
|
is easier to read and less prone to error than if you coded the test the long way: |
|
|
|
|
|
|
|
|
if (inputChar >= A && inputChar <= Z ||
inputChar >= a && inputChar <= z ||
inputChar >= 0 && inputChar <= 9 ) |
|
|
|
|
|
|
|
|
In fact, this complicated logical expression doesn't work correctly on some machines. We'll see why when we examine character data in Chapter 10. |
|
|
|
| |
|
|
|
|
Naming Value-Returning Functions |
|
|
|
| |
|
|
|
|
In Chapter 7, we said that it's good style to use imperative verbs when naming void functions. The reason is that a call to a void function is a statement and should look like a command to the computer: |
|
|
|
| |
|
|
|
|
PrintResults(a, b, c);
DoThis(x);
DoThat(); |
|
|
|
| |
|
|
|
|
This naming scheme, however, doesn't work well with value-returning functions. A statement like |
|
|
|
| |
|
|
|
|
z = 6.7 * ComputeMaximum(d, e, f); |
|
|
|
| |
|
|
|
|
sounds awkward when you read it aloud: Set z equal to 6.7 times the compute maximum of d, e, and f. |
|
|
|
| |
|
|
|
|
With a value-returning function, the function call represents a value within an expression. Things that represent values, such as variables and value-returning functions, are best given names that are nouns or, occasionally, adjectives. See how much better this statement sounds when you pronounce it out loud: |
|
|
|
| |
|
|
|
|
z = 6.7 * Maximum(d, e, f); |
|
|
|
| |
|
|
|
|
You would read this as, Set z equal to 6.7 times the maximum of d, e, and f. Other names that suggest values rather than actions are SquareRoot, Cube, Factorial, StudentCount, SumOfSquares, and SocialSecurityNum. As you see, they are all nouns or noun phrases. |
|
|
|
|
|
|
|
|
|
(text box continues on next page) |
|
|
|
|
|