Hacker News new | ask | show | jobs
by Cshelton 1904 days ago
I hate hate hate var.

Sure, in your own IDE, and reading the code you just wrote, no problem. But when doing code reviews and jumping all over, I want to see the type right there. Nothing more frustrating than reviewing a PR with var's all over. This isn't even up for debate anymore... No more var!

It is frustrating to still see var as the recommended way by Microsoft... You can even put in a rule to format the document and add explicit types as well on save. So complicated type signatures for linq aren't a problem!

9 comments

This is completely the opposite of my own experience. Using “var” makes the code easier to write AND easier to read. The code becomes decluttered from all the mess of types everywhere and you can read what it is actually doing.

Anyone who names their variables properly should have no problem telling what kind of types are being worked with anyway. And in the rare case you absolutely need specifics, they’re just a tooltip away.

I always find these arguments against “var” somewhat absurd because those same programmers work with fields and properties and methods from other classes all the time, and none of those requires you to write the type.

For an example, if you were to access “someExternalThing.bigRedTrain”, the type of “bigRedTrain” would be written nowhere in the current class. I fail to see how using “var bigRedTrain” is any different.

Can't disagree more. var improves readability so much that I can't imagine reviewing code anymore with unnecessary type declarations everywhere.
Every time you write something like Foo.Bar, the type of the value returned by Foo is implicit - i.e. it's exactly like var. Do you never dot-chain properties and method calls? If you do, why should it be any different when some intermediate step in the chain gets a name?

  MyFluffyGenericType<WowThisIsFun<MeToo>, MoreStuff> x = myVendorsStupidContract.SomeInsaneType;
vs

  var x = myVendorsStupidContract.SomeInsaneType;
> Nothing more frustrating than reviewing a PR with var's all over.

ooh you gotta try reviewing your PRs within visual studio [1] (and be sure to use VS internal diff tool as well) - var is no longer annoying with intellisense

[1] https://github.com/github/VisualStudio/blob/master/docs/usin...

There's a reasonable convention of specifying full type for a variable that is returned from a method where it's not very clear what the method returns without looking up method signature. This usually means that the method name can be is not descriptive enough, but there are case where you can't really fix that. Other than that, I think var definitely is a huge benefit for readability and overall code design.
Even then, you only see the type signature when you are initializing the variable, not every time you use it.

If you really want to see type signatures everywhere, you should use something like Hungarian notation which embed type information in the name. And you should disallow method chaining and having methods or properties as parts of expressions.

But I would recommend just using an IDE instead.

It’s definitely up for debate and since “use var everywhere” is the default ReSharper setting I suspect your preference is the minority position.
This kind of thing is really quite common in languages though. You have to understand that this is quite subjective.