On a mail list I am on there has recently been a huge debate raging on "defensive programming" and the fact that it is a bad thing in these days of test-driven programming. An (arguably trivial) example of one of the many issues:
In a for loop people are taught to write things like: for ( int i = 0 ; i < N ; ++i ) { . . . } instead of: for ( int i = 0 ; i != N ; ++i ) { . . . } The arguments go roughly along the lines of: i < N is an act of "defensive programming" and this smothers potential errors in the code. In particular a large class of errors will lead to faults elsewhere in the code and so make debugging harder. i != N maximizes the chance of an error in the code being detected by a more catastrophic failure associated with the code itself and therefore easier to debug. With a modern approach to programming, i.e. massively unit test oriented even if not test-driven development, there is no need for defensive techniques and indeed they are detrimental to good and efficient code development and maintenance. or The for loop is a counted iteration not a bounded iteration so whilst i < N is a reasonable condition in a while loop (which represents bounded iteration), i != N is a more sensible expression in a for loop as it minimizes the termination condition and so maxmizes the discovery of error locally to the loop. Error is detected as part of a proper unit test so a defensive approach to code is unnecessary and inappropriate. Anecdotally some teachers and trainers are admitting that "i < N" does cause some students some real problems. Currently, the debate is entirely presented from personal experience and theoretical argumentation which is fun but not really conclusive. I was wondering if there had been any research on the area of conceptualization of code structures especially control structure like for, "defensive programming" and errors that could provide some scientific input into the debate. Thanks. -- Russel. ==================================================== Dr Russel Winder +44 20 7585 2200 41 Buckmaster Road +44 7770 465 077 London SW11 1EN, UK [EMAIL PROTECTED]
signature.asc
Description: This is a digitally signed message part