[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

style: single and multiple lines

On 02/10/17 17:00, Stefan Ram wrote:
>    My copy of pep 8 (from 2016) says:
> Yes:
> def f(x): return 2*x
>    . So this single-line style should not be that bad.
>    However, I remember someone saying that the multiline
>    style is more phytonic?
>    So, is this better:
> def f(x):
>      return 2*x

Most of the time, yes.  The whitespace on the left-hand side is a good 
visual cue that something content-like is happening, in this case the 
body of a function.  The fact that it has shape makes it easier to 
comprehend at a glance.

>    ? And is
> def f(x):
>      y = x*2
>      return y
>    better than
> def f(x):
>      y = x*2; return y

Hell yes.  One thought per line, please.

Something I keep repeating to clients is that whitespace is not the 
enemy.  Not even in C.  Judicious use of spacing can make code *much* 
easier to comprehend.  Densely-written code makes you work hard to break 
it down into manageable chunks; something as simple as the odd blank 
line to "paragraph" your code can make that a lot easier.

Experience also suggests a correlation between code that's hard to read 
and code that's rather crap.

Rhodri James *-* Kynesim Ltd