Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Add new lint: unnecessary_final. #1827

Merged
merged 1 commit into from
Nov 12, 2019

Conversation

davidmorgan
Copy link
Contributor

This lint is more or less the opposite of prefer_final_locals plus prefer_final_in_foreach, plus checking parameters. (There isn't a prefer_final_parameters).

What do you think, please? This seems much closer to common usage. I'm not sure we'll ever get around to enforcing it, but it's nice to have a lint that matches what people do, so that people aren't just citing the existence of those lints as an argument :)

@davidmorgan davidmorgan requested a review from pq November 11, 2019 16:30
@a14n
Copy link
Contributor

a14n commented Nov 12, 2019

final allows to avoid accidentaly reassign a local variable and prevents bugs.

Personnally I would never enable unnecessary_final in one of my project.

See also https://stackoverflow.com/a/316787/634863

@davidmorgan davidmorgan merged commit c366cda into dart-lang:master Nov 12, 2019
@davidmorgan davidmorgan deleted the add-unnecessary-final branch November 12, 2019 11:08
@davidmorgan
Copy link
Contributor Author

Thanks @a14n .

We have fans of both approaches--as on that stackoverflow answer. In google3 it looks like >50% prefers var to final, although it's not overwhelming.

With this lint, the linter can now enforce whichever of the two you like, or neither :)

My personal interest is to try to make the tools good enough that nobody wants final for locals. The IDE could highlight local variable reassignments for you; there's no need for you to do it by hand.

@pq
Copy link
Member

pq commented Nov 12, 2019

My personal interest is to try to make the tools good enough that nobody wants final for locals.

I find this idea confusing. IMO the value of final is in communicating intent (that the tools can then use to help keep you on the rails). I don't understand why we'd ever want to do away with that.

@a14n
Copy link
Contributor

a14n commented Nov 12, 2019

Unlike Java final can be used to introduce variable in Dart.

It looks like the benefits of using final are rarely challenged. The main concern on the Java side is that it adds verbosity:

// in java
String s = "";
// with java>=11
final var s = "";
//--- with final
final String s = "";
// with java>=11
final var s = "";

In Dart the difference is smaller (so the main drawback is lowered):

// in java
var s = '';
// with java>=11
final s = '';

@davidmorgan
Copy link
Contributor Author

We've had this discussion a lot of times internally :)

Originally I was on the side of final. I'd like it to be the default in the language, but since it isn't, we could at least add it manually everywhere. So, I argued for everyone to use it everywhere.

People didn't want to :) ... they argued that the extra typing (just two characters, but in lots of places) doesn't add enough value. Eventually, after raising this topic repeatedly over a number of years, I figured it's never going to happen: we can't make final the standard since Dart was created with var.

So I changed my own code to use var. e.g. here was the change for built_value: google/built_value.dart@7ea8681

And my personal experience is that it makes no difference whatsoever. I can't think of a single case where using var instead of final caused me problems. And it is, indeed, two characters shorter, which is nice.

I hope that one day either enforcement of final or enforcement of not using final we be added to package:pedantic, resolving the issue one way or the other. However, since I have to get a consensus for either change, and the current split is pretty close to fifty fifty, this may unfortunately be an uphill struggle :)

@davidmorgan
Copy link
Contributor Author

One more thought. Enforcing this lint has the nice property that it makes final just for fields, and var just for locals. That cuts down on confusion about what final does/means, and aids readability overall.

Thanks.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants