Saturday, 13 February 2010

CodeQuality.html

Code Quality
-->

Code Quality

Code Quality

I'm beginning to feel beleaguered. I'm surrounded by code that was ostensibly written in order to deliver a product quickly but the result is that it is so bug ridden that it cannot be used. Here are some of the reasons why so much code takes so long to write:

  • No Specification
  • No Contract
  • Defensive Programming
  • No Unit Testing

If there is no spec' how do you know when you are finished?

If there is no spec' then of course there can be no contract. With no contract the programmer of the low level code will be tempted to defend his code against faulty input by trying to outguess the client and by writing code that copes with faulty input. He thinks that he will at least be able to say that it wasn't his code that blew up the Space Shuttle or downed an Airbus. In fact, of course, the defensive sub-routine conceals bugs in both the routine and the caller and in the absence of unit testing allows bugs to lie dormant in the caller for months or years.

Defensive programming is such a pernicious practise that it should be banned by law. Every time you see a Visual Basic subroutine that begins by comparing an object to Nothing in order to do nothing if it is you should suspect defensive programming. Everytime you see an error trap that simply sets the return value to an empty string, Nothing, null or a zero suspect the same thing.

For a much calmer and very much more thorough exposition of Design By Contract see Todd Plessel's paper at http://www.elj.com/eiffel/dbc/.

Here is a quote from the Abstract:

Meyer's Design By Contract is presented as a high-yield technique with a low-effort and low-trade-off requirement. It is argued that Design By Contract not only improves the effectiveness of other techniques aimed a verifying correctness, but is, in fact, a

necessary condition for correctness.

Further words ought to be superfluous, read the paper to find out why they are not.

More Design By Contract links

Design by Contract: The Lessons of Ariane
Don't let it happen to you!
Critique of "Put it in the contract: The lessons of Ariane"
Of course not everyone agrees.
Framework for C#
Might be worth a try.

No comments:

Post a Comment

Blog Archive

Followers