Clarity vs. code reuse/generality

Jean-Michel Pichavant jeanmichel at sequans.com
Fri Jul 3 11:58:48 EDT 2009


kj 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.
>   
[...]
>     sense = cmp(func(hi), func(lo))
>     if sense == 0:
>         return None
My suggestion on how to improve this part for python novices:
# assuming func is monotonous
if func(high) > func(low):
    direction = 1 # aka sign of the func derivative
elif func(low) > func(high):
    direction = -1
else:
    return None

Avoid using cmp, this will prevent the "what the hell is cmp doing ?" , 
unless you want to teach your students  how to search for inline python 
documentation.
Some other list members have suggested to improve the variable naming. I 
couldn't agree more, in your case, I think clarity can be achieved as 
well with the abstraction (these notions does not necessarily collide).

Here's a link on my how-to-name bible :
http://tottinge.blogsome.com/meaningfulnames/

Jean-Michel




More information about the Python-list mailing list