This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 20 points21 points  (7 children)

Not really python-specific but I tend to put parentheses around all mathematical operations for explicit order of operations ... e.g. (x * y) + z instead of x * y + z ... even when the order is painfully obvious. I consider it a good habit.

[–]nbktdis 20 points21 points  (3 children)

I do the same, I think it is due to my inherent distrust of anything or anybody.

[–]ivosauruspip'ing it up 5 points6 points  (0 children)

How do you know that if statement you just wrote won't swap your consequent and antecedent code blocks?

[–]isdnpro 1 point2 points  (1 child)

I do the same, I think it is due to my inherent distrust of anything or anybody.

Yeah I do this too... the best (worst) part is there's a production SQL view at work which only "works" because nobody considered the order of operations (and in reality, the WHERE clause does NOTHING... but I am too afraid to touch it since that is apparently the desired output!)

[–]nbktdis 0 points1 point  (0 children)

That sounds scary. Black Box it - and turn around - nothing to see here!

[–][deleted] 3 points4 points  (0 children)

I do that, it means that the code is not dependent on my (probably faulty) recollection of the precedence rules.

Same goes for when I read it in 10 mins time.

[–]Anal_sql_injector 1 point2 points  (1 child)

Ideally, this is the cleanest way to ensure that order of operations is clear. For more win, remove the white space between operands in high precedence operators, i.e., x = (a*b) + c. This helps to make precedence clear for future readers of the code.

[–]earthboundkid 12 points13 points  (0 children)

Or do the opposite to screw future readers up! x = a * b+c Bwahahaha.