BlueStyle icon indicating copy to clipboard operation
BlueStyle copied to clipboard

Add guidelines on multiple assignments in a single line

Open raphaelsaavedra opened this issue 5 years ago • 5 comments

Sometimes one might want to do multiple assignments in a single line to reduce number of lines of code, e.g.

Pmin, Pmax = get_pmin(fnm.system), get_pmax(fnm.system)

If this is considered bad style by us, we should probably add a comment about this in the docs.

raphaelsaavedra avatar Nov 17 '20 15:11 raphaelsaavedra

I think multiple assignments from a single call is okay (unpacking):

a, b, c = f()

but multiple assignments from multiple calls should probably be avoided.

omus avatar Dec 11 '20 19:12 omus

I agree with omus.

fchorney avatar Nov 30 '21 20:11 fchorney

What about the following pattern:

a, b, c = if cond(x)
    alpha, beta, gamma
else
    nothing, f(beta), g(gamma)
end

IIRC there are cases where one assignment outside an if statement infers better than separate assignments inside it.

mcabbott avatar Nov 30 '21 21:11 mcabbott

That case seems beyond the scope of this issue. Since this is about multiple assignments on a single line." I agree that for blocks it can be occasionally useful.

oxinabox avatar Nov 30 '21 21:11 oxinabox

the reason i don't like it is for: a, b = c(), d() the reading order I end up having is either: a, c, b, d or c, a, d, b. Because I want to know what is assigned to what. both of which are easier with

a = c()
b = d()

I guess another way to describe this rule would be "Do not create tuples just to unpack them on the same line" ?

oxinabox avatar Nov 30 '21 21:11 oxinabox