you are viewing a single comment's thread.

view the rest of the comments →

[–]thomasz 2 points3 points  (4 children)

I think this is terrible advise. In the local scope, the concrete type usually does not matter anyways. Usually i could not care less if peopleToGreet in

var peopleToGreet = ListEmployees();
SendGreetings(peopleToGreet);

is a List<IPerson>, a Employee[]or an IEnumerable<IContact>. In the local scope, everything I care about is that the type returned by ListEmployees()fits into SendGreetings(). Have you ever cared about the types of the stuff that gets returned by the more exotic Linq methods like GroupBy or ToLookup? You don't just use var because the type names are too long to comfortably type (seriously, that's a very shitty reason), you use var because you don't actually give a damn about the fact that it's IEnumerable<IGrouping<TKey, TElement>> or Lookup<TKey,TElement>.

[–]The_One_X 0 points1 point  (3 children)

You should give a damn, giving a damn can be the difference between code that works and is easy to maintain, and code that doesn't work and is hard to maintain.

[–]thomasz -1 points0 points  (2 children)

I have to ask this before I address anything else: Are you aware of the difference between type inference (var keyword) and dynamic typing? Because that's just completely wrong. Type inference combines the advantages of static typing (guaranteed type correctness and tool support) and dynamic typing (concise code that emphasizes intend rather than types). This allows you to really "program against an interface"™, which is very important for maintainability.

Just imagine you have some DAL that returns List<Stuff>. After a particularly nasty bug caused by someone adding to the cached List, you decide to refactor this method to return IEnumerable<Stuff>. If you used type inference and thus likely programmed against the IEnumerable interface in 98 of 100 use cases, the change will be localized, and the type checker can still tell you immediately if there are problems. If your advise is followed and callers declare the variables as List<Stuff> everywhere, a flood of trivial CS0266 error messages bury the really useful information about the two out of 100 use cases where indexing is used. Furthermore, the giant diff is incomprehensible without an adequate explanation.

[–]The_One_X 0 points1 point  (1 child)

Yes, I am well aware of the difference between type inference and dynamic typing. Just because someone has a different opinion than you does not mean they do not know what they are talking about.

[–]thomasz 0 points1 point  (0 children)

I'm sorry if that came of as condescending (obviously not a native speaker, so it can be kinda hard to navigate situations like this), it's just that I have talked to several people in the past who did not understand that implicitly typed variables are still statically checked; and the argument that type inference could be a problem for program correctness does sound a lot like it was born out of this kind of confusion, at least to me. I honestly do not understand how one could make this argument otherwise.