Clarity vs. code reuse/generality

Scott David Daniels Scott.Daniels at Acm.Org
Mon Jul 6 08:33:02 EDT 2009


Andre Engels wrote:
> On Mon, Jul 6, 2009 at 9:44 AM, Martin Vilcans<martin at librador.com> wrote:
>> On Fri, Jul 3, 2009 at 4:05 PM, 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.
>> In general, code clarity is more important than reusability.
>> Unfortunately, many novice programmers have the opposite impression. I
>> have seen too much convoluted code written by beginners who try to
>> make the code generic. Writing simple, clear, to-the-point code is
>> hard enough as it is, even when not aiming at making it reusable.
>>
>> If in the future you see an opportunity to reuse the code, then and
>> only then is the time to make it generic.
> 
> Not just that, when you actually get to that point, making simple and
> clear code generic is often easier than making
> complicated-and-supposedly-generic code that little bit more generic
> that you need.

First, a quote which took me a bit to find:
     Thomas William Körner paraphrasing Polya and Svego
     in A Companion to Analysis:
         Recalling that 'once is a trick, twice is a method,
         thrice is a theorem, and four times a theory,' we
         seek to codify this insight.

Let us apply this insight:
     Suppose in writing code, we pretty much go with that.
A method is something you notice, a theorem is a function, and
a theory is a generalized function.

Even though we like DRY ("don't repeat yourself") as a maxim, let
it go the first time and wait until you see the pattern (a possible
function).  I'd go with a function first, a pair of functions, and
only then look to abstracting the function.

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list