Friday finking: TDD and EAFP
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)
Latin/legal term "compos mentis"
English slang term "nuts": https://www.thefreedictionary.com/nuts