|
Are you one of Those People who use var instead of int? If so, why the hell?
|
|
|
|
|
harold aptroot wrote: Are you one of Those People who use var instead of int?
no. I did say "where applicable".
We use var on list returns and some lamda/linq stuff, but not everywhere.
Edit: I have never run into an issue with "var". Visual Studio lets me know what the type is, if I need to know it.
|
|
|
|
|
Ok good.
Slacker007 wrote: "where applicable" Well that's the thing. There's a school of thought that it is applicable any time it is allowed.
|
|
|
|
|
harold aptroot wrote: There's a school of thought that it is applicable any time it is allowed.
I use ReSharper a lot, and it will ask you everytime it can to use implicit type declaration. So, I can see why some devs out there may think that it is good for everything.
However, I have debugged code files that have had "var" all over the place, and it has not been an issue. If for some reason, I cannot tell the type, then hovering over "var" will tell you.
|
|
|
|
|
Try doing a code review in Crucible where someone's abused var and then see how you feel about that.
|
|
|
|
|
Interesting discussion.
I found var a bit difficult to cope with at first as it does hide the actual type. Fortunately many IDEs will, as has been pointed out elsewhere, give you the type if you hover over the 'var '. This, however, doesn't help when dealing with printed/quoted code snippets or when working in an environment that doesn't reveal the type.
So, my definition of 'appropriate' is where the type is obvious (usually because the assignment includes the type as part of a 'new ' or a typecast of some kind) or because the type is so complicated that it obfuscates itself (e.g. nested generics with multiple parameters). In the latter case I either rely on VS for an explanation or, where possible, simplify the type by creating a new wrapper class /struct with XML help to explain what the class is about.
The basic rule is "Think about other people when writing your code." Always ask yourself 'Can this be understood without having to look through reams of code or relying on an IDE to find variable/type definitions?' If not can it be simplified or explained?" If all else fails use a comment to explain (and thereby starts another argument ).
|
|
|
|
|
Couldn't agree more with you. I would also like to add that even if you have Visual Studio it interrupts reading and interpreting the code. Making it harder to understand for someone that is reading the code for the first time.
I blame ReSharper for the fever of using var everywhere. That's the argument of many: "Oh, but Resharper says to use var everywhere".
I wish I knew who at ReSharper dev team decided it was a good thing.
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
|
|
|
|
|
Fabio Franco wrote: I blame ReSharper for the fever of using var everywhere.
Indeed! That's why I quickly felt pressured into using var . However, as with most things ReSharper, you can turn off the 'hint' in the ReSharper settings if the nagging gets too much and your employer's required coding style doesn't prohibit it.
|
|
|
|
|
Paul Benson wrote: ou can turn off the 'hint' in the ReSharper settings
Yeah, but by that time, ReSharper already damaged the new devs.
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
|
|
|
|
|
No. I will not rely on some IDE to tell me what my code means. Often enough the IDE is too busy showing me hysterical error messages or the parser got lost a little further up in the code and simply is not able to provide any useful information anymore.
Second, especially when I have to work with complex baseclasses, I tend to use some good oldschool printouts of those baseclasses as reference. I tried using intellisense on a printout, but the result always was a classic 'Funny, no response!'.
Third. I don't hover, not even when flying a helicopter.
The language is JavaScript. that of Mordor, which I will not utter here
This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a f***ing golf cart.
"I don't know, extraterrestrial?"
"You mean like from space?"
"No, from Canada."
If software development were a circus, we would all be the clowns.
|
|
|
|
|
Interesting view points.
|
|
|
|
|
Actually I have nothing against hovering with a helicopter. It's just because I gave a friend the nickname 'Hoverfly' because he keeps practicing hovering and did not want to carefully start flying around. Last weekend he did and crashed his helicopter and has been repairing since then.
Now I can't just say that hovering is ok, or he will never try again.
The language is JavaScript. that of Mordor, which I will not utter here
This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a f***ing golf cart.
"I don't know, extraterrestrial?"
"You mean like from space?"
"No, from Canada."
If software development were a circus, we would all be the clowns.
|
|
|
|
|
CDP1802 wrote: or the parser got lost a little further up in the code and simply is not able to provide any useful information anymore.
after saying that you really think you are in position to say that var is a problem? you just said you write code that the machine that will compile and run it can't understand.
|
|
|
|
|
No. When the parser is a little upset and confused, the code will not compile. Nor will it be able to provide me with some of the information I may need to correct this. In this situation I prefer not having to rely on intellisense.
The language is JavaScript. that of Mordor, which I will not utter here
This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a f***ing golf cart.
"I don't know, extraterrestrial?"
"You mean like from space?"
"No, from Canada."
If software development were a circus, we would all be the clowns.
|
|
|
|
|
Slacker007 wrote: f for some reason, I cannot tell the type, then hovering over "var" will tell you.
Yes, it will - but that wastes time and breaks the "flow" when reading the code.
An explicit declaration is always clearer - particularly when it's to hold a method return value:
var myVar = GetSomething();
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Slacker007 wrote: Visual Studio lets me know what the type is, if I need to know it.
Irrelevent. Try reading newbie code posted in QA.
|
|
|
|
|
To obfuscate variable type
|
|
|
|
|
I am. Why the hell not? It's more aesthetically pleasing and in most cases it does not reduce readability (for me, for you - don't care).
I haven't use it much in C# before C++11 assigned new meaning to auto . Then I started replacing crap like this std::unordered_map<std::string, boring_class>::const_iterator with auto and then moved the habit to C#.
|
|
|
|
|
|
How often do you have to declare stuff like in C++ std::unordered_map<std::string, boring_class="">::const_iterator?
It's hardly the rule. If you're declaring
Tuple<Dictionary<int, string>, KeyValuePair<int,string>>
or something like that, maybe it makes sense.
But more often than not, it doesnt.
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
|
|
|
|
|
Well ok, if it's some sh*tty long name it makes sense.
If it's int or similar, it does not.
|
|
|
|
|
Hardly the rule and only applicable for complex names.
Now, people use it on method returns and everywhere. I happen to be in a position that I have to read a lot of other people's code and it's a pain to read them when var is used everywhere. On generic types it's even more disrupting.
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
|
|
|
|
|
PIEBALDconsult wrote: Same thing I see with Linq/Lambdas ; it's still just the Cult of Fewer Keystrokes .
I really like linq/lambdas. They unify the way many various things are accessed, and in my opinion make the code more readable.
Just my opinion. Your mileage may vary.
Once you lose your pride the rest is easy.
In the end, only three things matter: how much you loved, how gently you lived, and how gracefully you let go of things not meant for you. – Buddha
Simply Elegant Designs JimmyRopes Designs
|
|
|
|
|
JimmyRopes wrote: They unify the way many various things are accessed
To the lowest common, and therefore the least efficient, method possible.
And it's unreadable.
|
|
|
|
|
PIEBALDconsult wrote: And it's unreadable.
When done correctly, it is very readable and strongly encouraged in all of our software teams. However, I have seen it done poorly, which validates your comment, IMO.
|
|
|
|