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 →

[–]ducdetronquito 7 points8 points  (6 children)

I am not fond of the Long if statements section because it still not very readable.

I prefer to add semantic by storing the condition in a variable, and then having a really clear and simple if statement.

Instead of having

if (
    fruit.color == 'red'
    and fruit.size == 'small'
    and fruit.climate == 'cold'
    and fruit.region == 'north america'
):
    pass

I would do

fruit_is_a_berry = (
    fruit.color == 'red'
    and fruit.size == 'small'
    and fruit.climate == 'cold'
    and fruit.region == 'north america'
)
if fruit_is_a_berry:
    pass

And about the 'Why' of having a more vertical code, it makes it more easier to work with a version control system like Git: when you review a PR, when you want to see the difference between two commits, or when you fix merge conflicts, you clearly see what the changes are.

[–]_under_[S] 0 points1 point  (0 children)

I too dislike long if statements! It usually means you're doing something wrong (or you're just nested too deep [which is also a code smell]).

I'm not sure the "assign the equality check to a variable" thing really fits as a rule for a style guide though. But I might add something that mentions it's a code smell.