Python Module: nift

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Mon Feb 9 08:26:33 CET 2009


On Sun, 08 Feb 2009 13:34:50 -0800, bearophileHUGS wrote:

> Gabriel Genellina:
>> bearophile:
>> > - Generally I suggest to learn to code in the small, using as little
>> > code as possible.
>>
>> I completely agree with all the above suggestions, except this last
>> one. Ok, I think I know what you mean, but a beginner might understand
>> this completely wrong. Readability is important, and trying to compress
>> too much code into one single line should not be the ultimate goal.
> 
> What I meant is to use less code as possible, keeping readability and
> the original flexibility.

I think a better description is, use as little code as you need, and no 
extra, but no less. As Einstein said about physics theories: "As simple 
as possible, but no simpler."


> I can't stress enough the importance of writing less code. Every new
> programmer has to learn how much important it is. Less code means less
> bugs, less lines to edit and debug, etc. 

[pedant]
There's less code, fewer bugs and lines. Less applies to continuous 
quantities, like weight and "amount of code", and fewer applies to 
countable quantities.

Don't worry, I frequently get it wrong too, and I'm a native English 
speaker :)


> Less code is usually more
> elegant, and often more readable (even if you take 3 times to read each
> line it may be good anyway if you have 1/10 of the lines of code.
> Recently in the "Java to Python" thread someone has posted some Java
> class that is scary, you can probably write the same thing in 1/10-1/20
> of the lines of code).

Within reason, naturally. There's a reason why we write:

for button in buttons:
    move(button, delta_x, delta_y)

instead of

for b in bs: mv(b, x, y)

even though the second is significantly shorter. Keyword arguments versus 
positional arguments is another example where more verbose code is easier 
to read and maintain than shorter code.


Also, I don't think there's much advantage in trying to push new 
programmers to reduce the amount of code. Until you're really comfortable 
with the language, you need training wheels and scaffolding in the form 
of verbose code. Sometimes you really don't appreciate Python's labour 
saving features until you've experienced how much labour there is to be 
saved.


-- 
Steven



More information about the Python-list mailing list