This article promotes the fail-fast approach, something I very much dislike (against popular opinion it seems).<p>I'm very much in favor of the opposite approach, defensive coding. Often when I read opinion pieces about how bad defensive coding is, they almost always seem to forget that defensive coding without proper logging, error-handling and monitoring is <i>NOT</i> defensive coding.
It is extremely dangerous to just detect error conditions without any feedback: you have no idea what is going on in your system!<p>IMHO properly applied defensive coding, works as follows:<p>* Detect inconsistent situations (e.g. in a method, expected an object as input argument, but got a null)<p>* Log this as an error and provides feedback to the caller of the method that the operation failed (e.g. through an error callback).<p>* The caller can then do anything to recover, (e.g. reset a state, or move to some sort of error state, close a file or connection, etc.).<p>* The caller should then also provide feedback to its caller, etc. etc.<p>This programming methodology gives the following advantages:<p>* You are made to think about the different problems that can occur and how you should recover them (or not)<p>* Highly semantic feedback about what is going wrong when an issue occurs; this makes it very easy to pinpoint issues and fix them<p>* Server application keeps on running to handle other requests, or can be gracefully shut down.<p>* Client side application UIs don’t break, user is kept in the loop about what is happening<p>Of course you will need to keep a safety net to catch uncaught exceptions, properly logging and monitoring them (and restart your application if relevant)<p>The fail-fast approach, as I have seen it applied, doesn’t do any checking or mitigation, with the effect that:<p>- you are thrown out of you normal execution path, losing a lot of context to do any mitigation (close a file, close a connection, tell a caller something went wrong)<p>- you only get a stack trace from which it can be hard to figure out what went wrong<p>- there can be big impact on user experience : UIs can stop working, servers that stop responding (for <i>all</i> users).<p>I have very good experiences with using the defensive coding paradigm, but it takes more work to do it right; for many, especially in the
communities that use dynamic typing, such as the JS community, this seems to be a too big a hurdle to take. This is unfortunate
because it IMO it could greatly improve software quality.<p>Any feedback is welcome!<p>(Edit: formatting to improve readability)
(Edit: clarified defensive coding as an opposite approach to fail-fast)