DEV Community

Discussion on: Scare me with your one liner

Collapse
 
codemouse92 profile image
Jason C. McDonald • Edited

The following is the Python form of one of the most insidious anti-patterns in all of programming. The scary one-liner is the third line.

try:
    someImportantFunction()
except: pass

Mike Pirnat aptly calls that the "Diaper Anti-Pattern". It's horrific because it silences all errors, expected and otherwise, leaving your program in an unresolved exceptional state with few, if any, clues as to what's wrong.

Always, always explicitly catch and handle (or at least log) your exceptions. Unhandled exceptions should always crash your program. Period.

From the Zen of Python:

Errors should never pass silently.
Unless explicitly silenced.

If you're using anything resembling the diaper anti-pattern in any language, go fix it now. Seriously. Right now. Don't make me call your mother.


Here's a runner up from Python 2...

True, False = False, True

Thankfully, that one is no longer possible in Python 3.

Collapse
 
daniel13rady profile image
Daniel Brady

The boolean-swapping allowed by Python 2 is a great one πŸ‘ I remember when I first discovered that True and False were just builtin, mutable, global variables 😬