Friday finking: TDD and EAFP

Peter J. Holzer hjp-python at hjp.at
Sun Nov 3 16:04:20 EST 2019


On 2019-11-01 18:40:06 +1300, DL Neil via Python-list wrote:
> 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.

I think you refer here to the practice of (for example) just opening a
file and handling possible error cases (e.g. file doesn't exist, you
don't have permission, etc.) in an exception handler instead of checking
for possible problems first. Which is quite common in languages with
exceptions (like Python or Java), but not exclusive to them (for
example, this is also almost always what you should do in C).

You feel that TDD discourages this style and pushes you to writting
lots of checks up-front, correct?

> Does TDD bend your mind in this (apparently) non-Pythonic fashion?

No.

I don't feel my mind bent in this fashion. TDD treats (at least
conceptually, if not always in practice) the code as a black box.
So it shouldn't matter whether (for example) I check some condition
and raise an error, or rely on open() to raise the error for me.

I do feel OTOH that TDD nudges me toward sloppyness in cases where I
cannot easily write a test. I might then skip the test case ("It's
unlikely to happen anyway") and - since the test suite passes - forget
to implement it properly.

        hp

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp at hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20191103/24cfeaff/attachment.sig>


More information about the Python-list mailing list