| Store | Cart

Python style guidelines

From: MetalOne <j...@iteris.com>
12 Mar 2004 23:08:19 -0800
I do not like style guidelines either.  If one method can be factually
proven better than another method, then it would make sense to
encourage the better method.  However, when this can't be done, then
the guideline is simply based upon taste or gut instinct or personal
preference.  These all vary from person to person and who is to say
what is correct.

During my schooling I was taught to always place statements on their
own line.
x = 0
y = 0
z = 0

I accepted this completely without thinking about it much.
20 years later I have finally come to realize that if (x,y,z)
represents a point that
x = y = z = 0    or
(x,y,z) = (0,0,0)
is not always so bad.  It conserves lines of code, and being able to
see more lines of code at one time is beneficial.

Some guidelines recommend functions with only a single return point. 
I don't understand why this insanity got started.  At one point it was
recognized that multiple entry points into a function was confusing,
but why did it have to get extrapolated to multiple exit points.  Upon
entry to a function, I sometimes like to verify that everything is ok
before proceeding and if it is not just get out.  Parameters can
validated, resources acquired, or whatever.

At my work we currently have a guideline to always use brackets in
"if" statements.
    single statement
    single statement

Simply on the basis that somebody might forget the braces when adding
another statement to the block.  Has this ever actually happened? 
What about actually testing the code?  It is not too onerous of a
guideline, but it does increase lines of code, which affects the
ability to easily see more code, which affects overall readability in
some cases.

Even the usual guideline of placing statements on separate lines I
think is sometimes a mistake.
if cond: statement
elif cond: statement
else: statement

does not always seem too bad to me where you always just have a single

Somebody once convinced me that I should always use descriptive
unabreviated variable names.  At first this advice seemed a little
strange to me.  Most code that I had ever encountered abreviated
variable names.  Vowels tend to be left out.  I think it might be a
FORTRAN hold over where variables had to be 6 characters or less or
was it 8.  After a while, I realized people do things like
cnt, instead of count. sz instead of size. tmr instead of timer.  I
finally agree that long unabbreviated names are better.  Years later,
I realize that expressions can be hard to read with long variable
names.  It occurs to me that mathematics always use one character
variables like x,y,z or the greek symbols.  Expressions are just
easier to read.  You can simply comment what 'x' means.  So variable
length is not always so simple to decide upon.

And so it goes.  I guess I am a believer in TIMTOWTDI.

The other thing about guidelines is that once somebody publishes a
book with guidelines, lookout.  Your employer may soon have you
following them all whether you agree with them or not.  Rational Rose
and UML are great examples of this, but that is another story.

Recent Messages in this Thread
beli...@aol.com Mar 10, 2004 07:12 pm
Skip Montanaro Mar 10, 2004 10:09 pm
Will Berry Mar 11, 2004 10:39 pm
Richie Hindle Mar 12, 2004 09:36 am
Jorgen Grahn Mar 12, 2004 07:29 pm
MetalOne Mar 13, 2004 07:08 am
Heather Coppersmith Mar 13, 2004 01:22 pm
MetalOne Mar 13, 2004 09:28 pm
Andy Salnikov Mar 15, 2004 09:34 pm
Rocco Moretti Mar 17, 2004 07:22 pm
wmwd...@sneakemail.com ) (Greg Ewing (using news.cis.dfn.de) Mar 15, 2004 12:39 am
Josef Meile Mar 11, 2004 09:48 am
Peter Otten Mar 11, 2004 11:18 am
Heather Coppersmith Mar 11, 2004 11:34 am
Josef Meile Mar 11, 2004 03:39 pm
Peter Hansen Mar 11, 2004 03:41 pm
Donn Cave Mar 11, 2004 04:49 pm
John Roth Mar 11, 2004 05:16 pm
wmwd...@sneakemail.com ) (Greg Ewing (using news.cis.dfn.de) Mar 12, 2004 03:23 am
Mel Wilson Mar 12, 2004 03:57 pm
Aahz Mar 11, 2004 04:27 pm
beli...@aol.com Mar 11, 2004 12:52 pm
Michael Hudson Mar 11, 2004 01:41 pm
Messages in this thread