Friday finking: TDD and EAFP

DL Neil PythonList at DancesWithMice.info
Fri Nov 1 01:40:06 EDT 2019


Is the practice of TDD fundamentally, if not philosophically, somewhat 
contrary to Python's EAFP approach?


TDD = Test-Driven Development
EAFP = it's easier to ask forgiveness than permission
* WebRefs as footnote


The practice of TDD* is that one writes test routines to prove a unit of 
code, eg method or function; BEFORE actually writing said function.

The rationale is that TDD encourages proper identification and 
consideration of the routine's specification, and attempts to ensure 
that exceptions and "edge-cases" are not quietly forgotten.
(in a broad-brush, nut-shell)

However, I quite possibly like yourself, come from a time-before - 
before TDD, and before Python. So, have had to not only learn these 
things, but sometimes, un-learn points and habits (if not vices). 
Accordingly, I came (quite unknowing of the term, or that there might be 
an alternative) from the world of LBYL* (look before you leap).

In other words, before you do anything with some data, check that it is 
what you think it is. Whereas in Python we "try" by assuming everything 
is compos-mentis* and handle the "except" when things are not how we'd like.

That adaptation was not too difficult. After all, aren't programmers an 
optimistic bunch - I mean, I *never* introduce bugs into *my* code! Do you?

Which brings us to TDD. Here we assume the likelihood of bugs, as-if 
(cue: manic giggling); and code a bunch of tests first - in an attempt 
to prove that the code is up-to-spec.

In encouraging my mind to think about testing the code, I find myself 
searching for edge-cases, and attempting to anticipate the unusual. 
Accordingly to the gospel of TDD: so far, so good.

The 'problem' is, that it puts my mind onto LBYL-rails before the 
Python-coding train (of thought) has even left the station. It then 
seems natural to start putting a bunch of if-then-else's up-front and 
before the 'main line' of code.

Does TDD bend your mind in this (apparently) non-Pythonic fashion?
Have you developed an answer?
(other than: "@dn is 'nuts'*", which is not worthy of debate)


WebRefs:
https://duckduckgo.com/?q=TDD&ia=web
https://devblogs.microsoft.com/python/idiomatic-python-eafp-versus-lbyl/
https://docs.python.org/3/glossary.html#term-eafp
but: https://mail.python.org/pipermail/python-dev/2014-March/133118.html
https://docs.python.org/3/glossary.html#term-lbyl
Latin/legal term "compos mentis" 
https://www.thefreedictionary.com/compos+mentis
English slang term "nuts": https://www.thefreedictionary.com/nuts
-- 
Regards,
=dn


More information about the Python-list mailing list