Kasper Peulen
2015-07-09 01:03:14 UTC
PREFER using var without a type annotation for local variables.
Method bodies in modern code tend to be short, and the types of local
variables are almost always trivially inferrable from the initializing
expression, so explicit type annotations are usually just visual noise.
Decent editors can infer the type of local variables and still provide the
auto-complete and tooling support you expect.
Okay here are my reason why I think 1) type annotations are not "usuallyMethod bodies in modern code tend to be short, and the types of local
variables are almost always trivially inferrable from the initializing
expression, so explicit type annotations are usually just visual noise.
Decent editors can infer the type of local variables and still provide the
auto-complete and tooling support you expect.
just visual noise" and why I think that 2) you don't get the "tooling
support you expect".
1. Let's consider the following code from the pub package:
var pubspecs = await source.getVersions(package, package);
var versions = pubspecs.map((pubspec) => pubspec.version).where(constraint.
allows).toList();
If we would add types, we get:
List<Pubspec> pubspecs = await source.getVersions(package, package);
List<Version> versions = pubspecs.map((pubspec) => pubspec.version)
.where(constraint.allows).toList();
I think this improves readability of the code. And it also improves
explorability of the library apis. Now I can just press F1 on the Pubspec
and Version class, to see what the docs say.
I can go to the declaration of the Pubspec and Version class easily to
learn more about those classes.
But also code like this:
var directories = pluralize('Directory', directoryNames.length, plural:
'Directories');
var names = toSentence(directoryNames.map((dir) => '"$dir"'));
var verb = pluralize(singularVerb, directoryNames.length, plural: pluralVerb
);
It is just 2 chars longer to write:
String directories = pluralize('Directory', directoryNames.length, plural:
'Directories');
String names = toSentence(directoryNames.map((dir) => '"$dir"'));
String verb = pluralize(singularVerb, directoryNames.length, plural:
pluralVerb);
Why wouldn't you give those extra hints to your reader ? Now I have to
first read the docs about those methods, to see what it returns.
Maybe if I'm smart I can understand it from the names that are chosen, but
surely not in every case, and it will usually take longer for the reader to
infer the types from the method names then if you just add a friendly type.
2. Then you also have examples like var input = new InputElement(); where
it doesn't really add anything for readability if you would write
InputElement input = new InputElement();
But there is also another argument for adding type annotation, and that is
just that the analyser gets smarter the more type annotations you add. Say
we have a class MathInput that extends the InputElement class, with a
method called mathValue. Now if you write:
var input = new InputElement()
input.mathValue = "\pi = 3.1415...";
You wan't get any errors or warnings for this. See:
https://dartpad.dartlang.org/f2ade932d2aa7a3dfe85 But of course, the
program will fail if you run it. stereotype441 from the analyser team told
me this is the reason that the analyser don't give a warning:
The reason the analyzer doesn't give a hint for the call to foo2() is
because when the user declares a variable using "var" or "dynamic", they're
essentially saying to the toolchain "trust me, I know what I'm doing with
this variable, and I don't want to see warnings for it." We don't want to
give them a spurious hint unless we are really certain that the call is not
going to work.
see: https://github.com/dart-lang/dart-pad/issues/302#issuecomment-91373539essentially saying to the toolchain "trust me, I know what I'm doing with
this variable, and I don't want to see warnings for it." We don't want to
give them a spurious hint unless we are really certain that the call is not
going to work.
My reasoning is, if you know that the variable is a InputElement, and you
know that it will never be any other type, why leave the analyser in doubt
? Why would you give the analyser the signal: "trust me, I know what I'm
doing with this variable, and I don't want to see warnings for it." ?
I won't argue that the style guide should change to "PREFER using types for
local variables", I think it is great in dart that you have the choice to
use the `var` keyword if this makes sense. In the example of the style
guide:
Map<int, List<Person>> peopleByZip = new Map<int, List<Person>>();
Here I see that using types gives extra noise. But I don't see the point of
dogmatically avoiding any types for local variables. Which seems the case
in some dart projects that I open. I would say PREFER using types CONSIDER
using `var` if the type doesn't improve readability and gives extra visual
noise.
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.