Discussion:
[dart-misc] Clarification for `final` vs `var` in local scope.
jtknox via Dart Misc
2018-11-29 22:32:21 UTC
Permalink
(If there's an existing group for style related questions, in particularly
clarifications of Effective Dart, please let me know and I'll post there
instead).

I am aware of the preference to fields-and-top-level-variables-final
<https://www.dartlang.org/guides/language/effective-dart/design#prefer-making-fields-and-top-level-variables-final>,
but what about local variables. Consider something like:

set property(String value) {
var oldProperty = _property;
_property = value;
notifyChange(oldProperty, _property);
}

I *could* make `oldProperty` final, but that feels overly verbose. Is there
a consensus on using `var` vs `final` in these cases?
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/b31bba18-6b15-4ff7-92ec-28cbd9144036%40dartlang.org.
Randal L. Schwartz
2018-11-29 23:04:45 UTC
Permalink
jtknox> I *could* make `oldProperty` final, but that feels overly verbose. Is there
jtknox> a consensus on using `var` vs `final` in these cases?

If it were a longer routine, it might be a nice assertion that nobody
should update the value later.
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<***@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/Dart consulting, Technical writing, Comedy, etc. etc.
Still trying to think of something clever for the fourth line of this .sig
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/861s73mqnm.fsf%40red.stonehenge.com.
'Lasse R.H. Nielsen' via Dart Misc
2018-11-30 06:39:09 UTC
Permalink
Post by jtknox via Dart Misc
(If there's an existing group for style related questions, in particularly
clarifications of Effective Dart, please let me know and I'll post there
instead).
I am aware of the preference to fields-and-top-level-variables-final
<https://www.dartlang.org/guides/language/effective-dart/design#prefer-making-fields-and-top-level-variables-final>,
set property(String value) {
var oldProperty = _property;
_property = value;
notifyChange(oldProperty, _property);
}
I *could* make `oldProperty` final, but that feels overly verbose. Is
there a consensus on using `var` vs `final` in these cases?
There is no consensus, and the style guide leaves it up to you (it has the
"be consisitent with the surrounding code" rule, though).

Some people think you should make local variables final if you can. Others
think it's unnecessary noised.

Since the only code that will ever access that variable is code in the same
function, it provides not *compiler* benefit to mark a variable as final.
The compiler is quite capable of detecting that there are no assignments to
the variable.
If the function is short, there is probably also no human reader benefit.
If the function is long, maybe you should make shorter functions :)

/L
Post by jtknox via Dart Misc
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups
"Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an
To view this discussion on the web visit
https://groups.google.com/a/dartlang.org/d/msgid/misc/b31bba18-6b15-4ff7-92ec-28cbd9144036%40dartlang.org
<https://groups.google.com/a/dartlang.org/d/msgid/misc/b31bba18-6b15-4ff7-92ec-28cbd9144036%40dartlang.org?utm_medium=email&utm_source=footer>
.
--
Lasse R.H. Nielsen - ***@google.com
'Faith without judgement merely degrades the spirit divine'
Google Denmark ApS - Frederiksborggade 20B, 1 sal - 1360 KÞbenhavn K
- Denmark - CVR nr. 28 86 69 84
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/CA%2BeWuVDbBHF0b1ku5k_v0JjKmmNN19zOYetHANZ0hnLN592GFw%40mail.gmail.com.
Loading...