language icon indicating copy to clipboard operation
language copied to clipboard

Should parallel assignment be treated as multiple assignment for lints?

Open lrhn opened this issue 3 years ago • 4 comments

We have a lint, avoid_multiple_declarations_per_line which warns about

var x = something, y = other;

declarations which declare more than one variable. (It's a lint, and AFAICS not included in the official core/recommended lints, so it's something people have chosen to opt into.)

Should an assignment like var (i, length) = (0, list.length); count towards that lint?

(And is this really a linter question? The lint documentation has no motivation, so it's unclear what the goal of the lint is, other than to avoid declarations of that form, and therefore it's unclear whether a pattern parallel assignment should be covered too.)

lrhn avatar Jun 06 '22 10:06 lrhn

it could be allowed as it's pretty clear and concise with the parenthesis, as opposed to var x = something, y = other; which you'll basically miss when reading code fast. Destructuring is more obvious, so I don't think a lint rule would serve a good purpose here.

If that's a thing I hope it's a different lint rule, because I want one but not the other.

cedvdb avatar Jun 06 '22 23:06 cedvdb

You can obviously destructure a tuple, but if you are destructuring a tuple literal, then you are effectively just doing parallel initialization, and could just as well use two separate declarations instead.

With two different ways to do the same thing, a lint is a way to enforce one or the other. Since we have a lint for preferring separate declarations over multiple declarations on the same line, would we want one for preferring separate declarations over multiple variable declarations using a tuple literal too?

lrhn avatar Jun 07 '22 08:06 lrhn

Sorry, I might misuse some semantics here and there: I was talking about destructing a tupple literal ( var (a, b) = tupple; ). Even if that fits the rule in plain English / logically speaking, that rule will be detrimental for people that find it perfectly fine but do not find chaining okay; as it is demonstrably less readable.

So if you want to add tupple literal destructuring to that rule because it fits the lint rule title, then an additional more fine grained rule would be useful. avoid_chaining which would only disallow chaining like this var a = 3, b = 6; but would allow var (a, b) = tupple;.

In typescript for example I believe there is a rule only for chaining and as far as I can tell that's what is the most used because destructuring an object on the same line is not an hindrance to readability.

cedvdb avatar Jun 23 '22 23:06 cedvdb

I think the lint is useful because it warns users away from a syntax that is confusing. Consider:

var a = 1, b = 's';

What is the inferred type of those variables? Does it infer a separate type for each one, or the same type for both? (The answer is the former, but that's odd because there's no way to write a separate type for each. So inference can express something that you can't annotate.)

Or:

var a, b = 1;

What is the value of a? Are they both initialized with 1? Just the latter? If just the latter, what value does a have? What is its type? (The answer is a has value null and type dynamic because... ¯\_(ツ)_/¯.)

The syntax is just bad and should be avoided. Destructuring from tuples has none of these problems so I don't see any reason we'd lint against it.

munificent avatar Jun 28 '22 17:06 munificent

Closing this. I think the answer is "no", we shouldn't lint destructuring a tuple literal in a variable initializer.

munificent avatar Aug 18 '22 23:08 munificent