The path of failure

hsoi blog, talk 0 Comments

It’s important to consider failure.

Why? Because it happens. Not “if” it happens, but “when”… because it will. Because we’re human, we make mistakes. Because things are finite, and eventually you run out. Failure will happen.

The question is, have you prepared to handle it gracefully?

While this applies to life, here I want to talk about it in code and programming.

See, most of the time we consider code paths where things work out and everything goes smoothly. Certainly you have to focus on that, but if you know there’s a chance for failure, you need to handle it. If a function returns a boolean to say if it succeeds or not, that’s a clue it might not succeed and you need to handle that failure case. If a function can return an error code/object, that’s a clue that something — in fact, probably many things — could go wrong and you need to handle those failures; you may be able to say any failure we blanket handle, or you may need to special-case out some things and handle them differently.

But whatever the specifics are, you need to handle them.

You need to make sure whatever you did, you can recover. You need to make sure you clean up after yourself as well. Maybe you’re writing out a file and there’s failure. Part of the handling of that failure would be to clean up the partly written file to make sure no garbage remains. This is especially important on embedded devices — you know, like smartphones — where every resource, like disk space and memory, are truly at a premium.

And when you fail, not only should you handle it gracefully, but you should consider the user. Sometimes a failure doesn’t matter to the user; maybe you just note it and try again later. But that’s still handling it. Maybe this failure is low-level, and it doesn’t matter until things bubble up to a layer more relevant to the user; but still, the error gets bubbled up instead of swallowed and forgotten. When the user finally gets the error, make sure the error is useful and meaningful to them. Be informative about the problem, and if you can and if it’s appropriate, let the user know what they can do next because for sure they’re going to stare at the screen wondering that very thing.

While failure can be tough to program for, can be tough to test for, can be tough to deal with, you have to deal with it. Don’t just think about the path to success; consider the path to failure and be prepared for it. Handling your failures gracefully and solidly makes all the difference in the world.


Leave a Reply