Clarity vs. code reuse/generality

Albert van der Horst albert at
Thu Jul 16 14:45:27 CEST 2009

In article <h2l36k$q5l$1 at>, kj  < at> wrote:
>I'm will be teaching a programming class to novices, and I've run
>into a clear conflict between two of the principles I'd like to
>teach: code clarity vs. code reuse.  I'd love your opinion about
>This seemed straightforward enough, until I realized that, to be
>useful to my students in their homework, this _binary_search function
>had to handle the case in which the passed function was monotonically
>decreasing in the specified interval...
>Here's the rub: the code above is more general (hence more reusable)
>by virtue of this trick with the sense parameter, but it is also
>a bit harder to understand.
>This not an unusual situation.  I find that the processing of
>abstracting out common logic often results in code that is harder
>to read, at least for the uninitiated...

Yes, of course. You're teaching, say, green belt programmers.
Good reusable code requires a fifth dan. You may not be up to it
yourself (no offense intended), let alone your students.

Writing a reusable binary search is a very different assignment
from being able to code/use it in a concrete example.

Their choice must be between *writing* a one-off binary search,
versus *using* a brilliantly code, brilliantly documented
binary search that has been given beforehand.

Even assuming the reusable code is perfect in all sense, reusing may
be more effort than writing from scratch.
Then *you* have to explain them about the benefits of reuse.
(The other benefits, of course.)

>I'd love to know your opinions on this.

You're welcome.


Groetjes Albert

Economic growth -- being exponential -- ultimately falters.
albert at spe&ar& &=n

More information about the Python-list mailing list