Correct code/unit testing (was Re: Efficient python programming...)

Peter Hansen peter at engcorp.com
Sun Jun 9 02:52:43 EDT 2002


Kragen Sitaker wrote:
> 
> Peter Hansen <peter at engcorp.com> writes:
> > You forgot the even more important first thing for a beginner:
> > get it correct!  The only good way to do this is to write unit
> > tests that prove it.  If you don't have tests, you don't have
> > working code (or how can you prove it?).
> 
> I agree that correctness is important, although I think it is
> sometimes overemphasized; buggy programs can still be useful.  (I hate
> them, though.)  For example, in the ICFP programming contests, any
> entry found to contain a bug, however small, is automatically
> disqualified.
> 
> I also agree that unit tests are very useful for reducing bugginess.
> 
> But I do not think that they are either necessary or sufficient for
> writing correct code.  

Without tests, how much confidence can you have in the correctness
of your code?  It's been said "you don't know it works if you 
don't test it".

> I think (and extensive experimentation has
> shown) that careful reasoning and code reviewing is necessary and
> sufficient to write correct code.

Whoa!  "extensive experimentation has shown"??  I would suggest that
what extensive experience (I change the word deliberately) has shown
many of us is that careful reasoning and code reviewing, no matter
how extensive, will *always* leave bugs in the code.  Have you never
spent a large amount of time carefully designing and coding a wonder
little program, and checked it over very carefully, even with a peer,
and later found a bug in it?  Maybe you're just a better programmer
than I, and many others, but I would say this inevitable outcome is 
the source of the term "egoless programming" -- if you have a big
ego, you'll be devastated by the repeated proof that you will never,
ever get it right the first time, or even after lots of reviewing.

> It is certainly possible to err too far in the direction of believing
> your code correct simply because it passes its test suite, and this is
> a common beginner's error.  I think it is also possible to err too far
> in the direction of writing and running tests instead of desk-checking
> code.

I can only suggest that until you have tried test-first development,
in which the code is written in very, very small steps only to make
small failing tests pass, and discovered the possibilities of 
emergent design, and other aspects of this approach, then we will not
find much common ground in this discussion.  I simply disagree with
your last sentence above, and I believe beginners _never_ write
unit tests anywhere that I've seen, so I disagree with the first
one too. :)

-Peter



More information about the Python-list mailing list