|
What is the love affair with var? I see sample code where they do something like
var url = "http://someapi";
This feels lazy to me but I am open minded and am curious if I am missing something. Is there some benefit to declaring most things var instead of what they actually are? In the case above, a string.
Social Media - A platform that makes it easier for the crazies to find each other.
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
The only benefit is that if you don't understand types then there is a chance your code will work using var even though you don't know why. Also I've found some common style tools dictate you should use var. I don't mind it for
var name = "Blah";
or
var people = new List<Person>();
but I hate it when people use it for things like
var data = SomeFunction();
|
|
|
|
|
In C++
auto i = small integer
auto v = some complex STL type
auto l = some big elephanting lambda
"If we don't change direction, we'll end up where we're going"
|
|
|
|
|
I use auto , the C++ equivalent, whenever it will deduce the correct type.
- It forces you to initialize the variable.
- If the variable is initialized by calling a function, and that function's return type changes, the code might not even be affected.
- Some type names are long or complicated, and I hate spilling lines.
Some argue that it makes the code harder to understand because the reader has to figure out the type. My counterargument is that the reader doesn't understand how the code fits into the system if this is the case.
|
|
|
|
|
You are going on a dangerous path. It leads you to the days when an insignificant dot was significant:
auto i = 2;
auto j = 3;
auto k = i/j;
auto f = 2./j;
Besides "auto" has four letters and "int" has three; it's not energy efficient
Mircea
|
|
|
|
|
Not very dangerous unless one ignores compiler warnings. Then again, I don't use floating point very often.
The main one I have to watch for is
for(auto i = 0...
which makes i an int when what might actually be called for is a size_t .
|
|
|
|
|
The advantage of var is if "http://someapi" was a variable or class instance instead of a string literal and you changed its type, you would not then need to refactor the code elsewhere.
In your example it's just a case of habit probably, however it's obvious from what is on the right that it's a string.
It can sometimes make it harder to debug issues as jumping to the class definition might not always be supported in the IDE you use to debug the var url value with or have to wait for the IDE to resolve the type that the var is.
That's my take on var ...
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
|
|
|
|
|
GuyThiebaut wrote: The advantage of var is if "<a href="http://someapi">http://someapi</a>" was a variable or class instance instead of a string literal and you changed its type, you would not then need to refactor the code elsewhere.
Absolutely not true. Changing a data type and then attempting to use it elsewhere is guaranteed to cause you to have to modify code everywhere it's used. Fortunately the VS IDE is smart enough to identify those spots for you.
|
|
|
|
|
obermd wrote: Absolutely not true. Changing a data type and then attempting to use it elsewhere is guaranteed to cause you to have to modify code everywhere it's used. Even when changing from a short to a long ?
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
|
|
|
|
|
GuyThiebaut wrote: The advantage of var is if "http://someapi" was a variable or class instance instead of a string literal and you changed its type, you would not then need to refactor the code elsewhere. "O, that way madness lies; let me shun that!" (King Lear)
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
Tell me about it, I have been coding in Kotlin for an Android application and in Kotlin pretty much every type is inferred.
So the code is peppered with it when referring to a property and it can be pretty much anything.
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
|
|
|
|
|
ZurdoDev wrote: This feels lazy to me but I am open minded and am curious if I am missing something. Is there some benefit to declaring most things var instead of what they actually are? In the case above, a string.
I felt the same way initially, and I agree, for something simple like a native type, I rarely use var.
However:
var complicatedDictionary = new Dictionary<SomeKey, List<KeyValue>>();
and worse, I like definitely like it.
|
|
|
|
|
Marc Clifton wrote: var complicatedDictionary = new Dictionary<SomeKey, List<KeyValue>>();
and worse, I like definitely like it. I can see that.
Social Media - A platform that makes it easier for the crazies to find each other.
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
I'd actually prefer it is you could do this:
Dictionary<SomeKey, List<KeyValue>> complicatedDictionary = new *();
To me, that would be a more natural way of showing what the type of complicatedDictionary actually is.
As it is, I find var is mainly over used by the lazy-and-don't-care script kiddies ...
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
This is one place where VB has a better syntax:
dim complicatedDictionary as New Dictionary(of SomeKey, List(KeyValue))
I'd prefer to see the < and > symbols used around the type information as this would make it clear what is type information vs. New parameters/arguments.
|
|
|
|
|
|
Hah! I just spent half an hour trying to persuade Google to let me know how you make suggestions for the C# spec ...
That language change I will use!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I kind of find it easier to read with var first.
But this is more secure, since you have to know what you are going to need and the you create the new instance in the lazy mode.
Best of both options, I guess
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
OriginalGriff wrote: that would be a more natural way of showing what the type
Yup. And as I replied on the Insider News, what I really want is:
var foo = new();
In most cases, the compiler should be able to figure out what foo is by inspecting its usage in the code!
|
|
|
|
|
Marc Clifton wrote: In most cases, the compiler should be able to figure out what foo is by inspecting its usage in the code! If you turn the compiler into a psychic, be prepared for bad news
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
Yes I agree
"We can't stop here - this is bat country" - Hunter S Thompson - RIP
|
|
|
|
|
C# 9.0 will have it: Welcome to C# 9.0 | .NET Blog
To alcohol! The cause of, and solution to, all of life's problems - Homer Simpson
Our heads are round so our thoughts can change direction - Francis Picabia
|
|
|
|
|
Dictionary<SomeKey, List<KeyValue>> complicatedDictionary = new ();
Is on the planned list for C# 9.0.
|
|
|
|
|
+1
For native types, I don't see the point in using var. I also subscribe to the idea of using:
var x = new SomeMoreComplexType();
I don't mind:
var x = SomeFunction();
...if I don't particularly case about the type returned, for example, if I only use it to forward to some other function and I'm not looking at any of its members myself.
|
|
|
|
|
it is good for a lot of applications but can be missused very easily.
it's main purpose is code readability.
MyMostExcellentBaseClassOfAllClassesInTheWholeWorld myClass = new MyMostExcellentBaseClassOfAllClassesInTheWholeWorld();
or
var myClass = new MyMostExcellentBaseClassOfAllClassesInTheWholeWorld();
It is really good when working with Linq, etc. as well.
|
|
|
|