Skip to content

Add guidelines on multiple assignments in a single line #80

Open

Description

@raphaelsaavedra

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.

Activity

  1. omus commented on Dec 11, 2020

    @omus
    Contributor

    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.

  2. fchorney commented on Nov 30, 2021

    @fchorney
    Contributor

    I agree with omus.

  3. mcabbott commented on Nov 30, 2021

    @mcabbott
    Member

    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.

  4. oxinabox commented on Nov 30, 2021

    @oxinabox
    Member

    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.

  5. oxinabox commented on Nov 30, 2021

    @oxinabox
    Member

    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" ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentationgood first issueGood for newcomers

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions