On 07/09/2019 18:59:49, Chris Angelico wrote:
On Sat, Sep 7, 2019 at 11:27 PM Rob Cliffe via Python-ideas
<[email protected]> wrote:
A chance for me to bang the drum on one of my pet themes:
Sometimes the readability of code is improved by breaking the sacred
taboo of 1 statement per line, if it allows similar constructs to be
vertically aligned:
if max_results > 0 : query['max_results'] = max_results
if active is not None : query['active'] = active
if deleted is not None: query['deleted'] = deleted
If this comes out ragged in your browser, the colons and equal signs are meant
to be vertically aligned.
I'm not bothered by putting an if and a simple statement together, but
I am definitely bothered by the need to vertically align them, and
especially by the triple repeated name. Not a fan of this style -
looks like a bug magnet and maintenance burden.
ChrisA
The OP's code:
if max_results > 0:
query['max_results'] = max_results
if active is not None:
query['active'] = active
if deleted is not None:
query['deleted'] = deleted
is perfectly reasonable and has the same triple repeated name. Would you
rewrite it differently, and if so, how?
I'm just saying that aligning similar elements and bringing them
together, without intervening lines, makes it
easier to see the code structure and spot typos. Not to mention using
less vertical screen space.
I don't understand why you think it is a bug magnet and maintenance
burden. I think it is *easier* to maintain:
in which of the following is it easier to spot the mistake?
# Version 1:
if max_results > 0 : querydata['max_results'] = max_results
if active is not None : querydata['active'] = active
if deleted is not None: quervdata['deleted'] = deleted
# Version 2:
if max_results > 0:
querydata['max_results'] = max_results
if active is not None:
querydata['active'] = active
if deleted is not None:
quervdata['deleted'] = deleted
Data point: I have used this style repeatedly in my code (only where
appropriate, of course) and find it helpful.
Rob Cliffe
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/B6LEICJFFJNBO5EWWNUFJBGMSF6R5DE2/
Code of Conduct: http://python.org/psf/codeofconduct/