A good answer might be:Any function that returns any type of value can be used in a boolean expression, so side-effects are a concern. |
Danger with any MethodFor example, say that a method computeMaximum() computes the maximum value of some variables, stores the result in maximum and returns that value: There is a problem here because the second method has a side effect (setting maximum) that will not happen when sum is 100 or more. In this case you should arrange the expression like this:int maximum; . . . if ( sum < 100 && computeMaximum() < 500 ) . . . result = 2 * maximum ; // maximum might not have been computed. With this arrangement the side effect will always happen. The twoint maximum; . . . if ( computeMaximum() < 500 && sum < 100 ) . . . result = 2 * maximum ; // maximum will be computed. if statements look almost identical;
however, the first one is a bug.
Bugs like this can be hard to find.
An even better solution is to write |
QUESTION 4:Would it be useful to have a non-short-circuit AND operator? Click Here after you have answered the question |