Most programming languages these days have implicit types—this is where we can use var
, let
, or some other keyword in place of a type like string
or int
. This feature can come in handy when dealing with more verbose type names—like Dictionary
or HashMap
types particularly. But exactly when should a developer use these implicit types?
My opinion is:
int
, it is probably better to still use var
or let
as changing the type to an Int64
(or whatever) will not require you to change the var
, but it will require you to change an int
.Basically, var
and let
cut down on how much code people (or AIs as the case may be today) have to read. And when you refactor a type to have a different name, references to it that are using var
or let
don't need to be changed, reducing your work load.
I think, in languages like C#, var
is nearly always better. In Rust and Swift we have let
and everything is implicitly-typed by default. In Go we can use var
, or just the special operator that assigns a new variable. In C++ (not covered on this site) we have auto
which has the same effect.