JuliaDiff / BlueStyle

A Julia style guide that lives in a blue world
Creative Commons Zero v1.0 Universal
487 stars 35 forks source link

A word about short-circuit evaluation #56

Open paveloom opened 4 years ago

paveloom commented 4 years ago

A little about the stylistic usage of this functionality is mentioned in the official documentation, but I would like to see this guide's position on what and when is preferable:

if ! <cond> <statement> end

or

<cond> || <statement>
omus commented 4 years ago

As a first pass I think:

<cond> || <statement>

is fine if it's within the line length and the statement isn't a compound statement (e.g. (...; ...; ...)). In all other scenarios:

if !<cond>
    <statement>
end

should be preferred.

oxinabox commented 4 years ago

Sometimes I think it is also fine for multiline compound statements if wraping them in an if would lead to long indent.

Mostly I am thinking of testsets. Writing something like this testset with a giant if block around it would not be easier to read, it would just double the arond of indentation needed

omus commented 4 years ago

That's a good counter example. There are always exceptions to rules and that would be one here. Generally though I think the first pass I outlined is what we should promote as that's generally what should be used.