blog icon indicating copy to clipboard operation
blog copied to clipboard

To 'var' or not to 'var' comment.

Open adras opened this issue 5 years ago • 1 comments

I just saw your article about the var keyword. You're basically using all the arguments everyone else does.

I've got one question though:

  • How do you want to read code without VS when the types are no longer available? For instance while browsing a github repostory?

See this for example

How about line: 43, 44, 47. Which types are we talking about? Without the usage of the var keyword, you could answer my question instantaneously. But because of var, the only way to answer my question are excuses like: "the variable should be properly named". When it comes to naming, how do you want to choose a name for line 43? settingsEntity - But how do you want to know that it's an entity and not actually a SettingsEntity?

All of this doesn't work properly in my opinion. Take a look at your example:

var productGroups = _productService.GetGroupedProducts();

How do you want to know, that productGroups is a Dictionary? It could be a List or an array as well. If you're able to guess that it's a dictionary (for whatever magical reason), how do you want to know that the Key is a string (could be an integer as well) and that the Value is a Product (It could also be a ProductGroup).

You're heavily depending on the usage of VS and the tooltips it provides. The second VS is not available, for instance when simply browsing a repository on github, you don't know the types anymore, and can't figure them out easily either.

I'm really having a hard time to see where this increases the readability. All I see is a lot of scenarios where one lacks the information needed for the benefit of shorter code which can't be understood anymore.

I don't want to attack you in any way. I'm just wondering, how you want to work with code efficiently, when you have to look up stuff all the time, when the answer could be right in front of your eyes.

adras avatar Jan 28 '20 12:01 adras

Hey @adras Let me write a more detailed blog post as an answer :-) I think this would make more sense. Anyway, many thanks for your comment and your questions. I really appreciate that. :-)

JuergenGutsch avatar Feb 07 '20 15:02 JuergenGutsch