the further adventures of

Mike Pirnat

a leaf on the wind

« Previous Post Next Post »

The Diaper Pattern Stinks

I mentioned the "Diaper Pattern" in a recent post and got some comments asking what the term meant. I had hoped to just link to an explanation, thinking it was a well-known antipattern, but several minutes of frustrated Googling have left me convinced that there's actually a tiny knowledge gap to be filled here.

"Diaper Pattern" is a term that I picked up from working with Matt Wilson and David Stanek. It's an anti-pattern about naively over-broad exception catching. A very basic example in Python might be:

except Exception:

If anything at all goes wrong in do_something, the except will catch and silence it--but we know that errors should never pass silently (unless explicitly silenced). It's called a Diaper because it catches all the shit.

In practice, unless you have a good reason to catch everything, it's a lot better to only catch the specific exceptions that need special handling, so that you'll know right away about any runtime surprises and where they came from:

except IOError, e:
    # handle just IOErrors; let everything else make noise

Maybe it's just a coincidence, but I've heard the term used more by developers who have young children than by those without kids.

blog comments powered by Disqus

« Previous Post Next Post »