Clarity vs. code reuse/generality

Albert van der Horst albert at spenarnc.xs4all.nl
Thu Jul 16 08:45:27 EDT 2009


In article <h2l36k$q5l$1 at reader1.panix.com>, kj  <no.email at please.post> 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
>it.
>
<SNIP>
>
>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...
>
<SNIP>
>
>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.

>kj

Groetjes Albert

--
-- 
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert at spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst




More information about the Python-list mailing list