Michael,

Thank you very much for commenting on the article.

You’re correct; in many circumstances it is not practical or possible to have a method return a Boolean value and I should have pointed this out in the article. A constructor cannot return a value, for example, and it is unreasonable for a simple member accessor method to do so. The Pragmatic Defense addresses these cases differently; the Word Count example application referenced in the article provides illustrations (but another article would be needed to cover this topic).

In the case of methods that compute a value, returning a bool has many advantages, even for simple computations. Here’s an example that might be what you are looking for.

Let’s consider a commonly used C function to see how returning a Boolean, and applying the Pragmatic Defense, will help you isolate problems.

The sqrt function in the C header file math.h (or cmath) computes and returns the principal square root of its argument; here’s the function signature:

```
double sqrt (double x );
```

There is an implicit constraint on the value of x - it must be a non-negative real number (that can be represented as a double precision floating point value). It is not valid to call sqrt with an argument that is a negative real number or a value such as NaN (a special bit pattern indicating “Not a Number”). The principle square root cannot be computed for such values.

Let’s encapsulate the sqrt call within a C++ function that returns a Boolean; here’s the function signature:

```
bool computePrincipalSquareRoot(double dNonNegativeRealNumber, double & dPrincipalSquareRoot) throw()
```

A Boolean value is returned indicating success or failure. The computed principal square root is returned in the dPrincipalSquareRoot argument (a reference to a double). Within computePrincipalSquareRoot, we can check preconditions such as whether the input argument dNonNegativeRealNumber is indeed non-negative. We can also check if it is set to NaN (and other special, but invalid, values).

Using the Pragmatic Defense, the body of the computePrincipalSquareRoot method is outlined as shown below:

```
bool bSuccess = false;
try {
// Preconditions:
// REQUIRE: dNonNegativeRealNumber is a valid real number (not infinity
// and not NaN)
if(0 != isinf(dNonNegativeRealNumber)) { // throw, log error... }
if(0 != isnan(dNonNegativeRealNumber)) { // throw, log error... }
// REQUIRE: dNonNegativeRealNumber is non-negative
if(dNonNegativeRealNumber < 0) { // throw, log error... }
// ...
// Method Body
dPrincipalSquareRoot = sqrt(dNonNegativeRealNumber);
bSuccess = true;
// ...
```

You could also check postconditions within computePrincipalSquareRoot as well. (For instance, you could check that the square of the computed principal square root is within some epsilon of the value of the input argument dNonNegativeRealNumber.)

If invalid data were passed in to computePrincipalSquareRoot, the Boolean return value would let you know that the computed value, dPrincipalSquareRoot, should not be used. You would avoid using the value in subsequent computations. (If the value were propagated, the error would become much more difficult to track down.) Moreover, if the input argument did not satisfy one of the preconditions, you would know that the failure occurred in the code leading up to the call to computePrincipalSquareRoot. The error message emitted to the log would help you characterize the failure, giving you clues as to where to look for the fault.

Incidentally, testing special cases such as NaN is tricky. The following approach may help:

```
// Assigns the bit pattern for IEEE "Not a Number" to a double precision
// floating point variable.
double dValue;
long long *p_llBitPatternNaN = reinterpret_cast<long long *>(&dValue);
*p_llBitPatternNaN = 0x7ff0000080000001LL;
```

You can then pass dValue in to computePrincipalSquareRoot in your unit tests.

(Also, note that sqrt is overloaded in C++ header file “complex” - careful, this example is using the version in math.h.)

In summary, methods intended to compute a value can return a Boolean indicating success or failure; the computed value may be returned as an argument passed by reference. A Boolean return value may not be suitable in all circumstances, but in many cases, using the Pragmatic Defense - including Boolean return values - will help you track down faults quickly.

Please let me know if the above example is what you are looking for. I’m glad you found the article profitable and I very much appreciate your comments. Thanks once again.