Clarity vs. code reuse/generality

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Fri Jul 3 11:19:34 EDT 2009

kj a écrit :
> 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 - others already commented on this code)

> 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.

Perhaps better naming (s/sense/direction/g ?) and a small comment could 
help ?

> 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...

IOW : the notion of "clarity" depends on the context (including the 
reader). Anyway, there are algorithms (or implementations of...) that 
are definitly and inherently on the 'hard to read' side - well, 
complexity is something you have to learn to live with, period. The key 
is to try and make it as simple and readable *as possible*.

Also, factoring out common code - or even slightly complex code - often 
makes _client_ code (much) more readable.

More information about the Python-list mailing list