mstefan wrote:
 |
BHpaddock wrote:
And what if the pointer is NULL? What do you do then? Nothing? Return a failure and hope the caller can cope with it? |
If you code defensively, then the caller must be able to handle a failure. I disagree with the notion that it's acceptable, or worse, desirable for a program to simply fault out. While that can be useful for developers, it only detracts from the user experience. Is a cryptic "unhandled exception" dialog -- immediately followed by all of their work being dumped down the bit bucket -- the best that we can do when we make a mistake in our code?
What's better? The application gets into an unpredicted state and things just stop working, and eventually crashes in some unrelated piece of code, spins indefinitely, etc.
Or the program crashes, is caught by Watson, and the user is presented with a "Please install update XYZ to fix the problem" dialog.
Then again, that should never happen, because your methods/interfaces should be annotated properly and nobody will be calling you with NULL if you don't accept that input. If they try to, they'll get a PREfix error before they can even check the code in.
As we said, if an exposed API doesn't check its inputs, as is likely happened here, then it's a bug. Of course, the situation can be more complex, too. The bug could be that the caller is calling function A with a pointer, function A is null'ing it out unexpectedly, and then the caller passes the same pointer to function B.
Having function B notice the null input and return an error probably doesn't buy you anything, since the caller didn't realize he could ever be passing a null value there. Maybe somebody changed Function A and introduced that behavior, maybe intentionally or maybe by accident. Perhaps function A was supposed to initialize the pointer, and the caller just checks if (SUCCEEDED(hr)), but then someone came along and added an S_FALSE return that leaves the out param blank. In any case like that, covering it up it just makes it harder to find and debug.
Of course, in an ideal world that would never happen, and software wouldn't have bugs. Sadly, that's not the world we live in.