2

They say one should not go to sea with two chronometers, or wear two watches. You should either use one that's reliable, or three (or more) to let the "majority rule."

So, should I install another code refactoring helper, or uninstall one of them? I have installed two, and they are bickering back and forth over whether this line of code:

using (StreamReader file = new StreamReader(ChemicalMakeupOfEveryDropOfWaterInTheMississippi))

...should be this instead:

using (var file = new StreamReader(ChemicalMakeupOfEveryDropOfWaterInTheBigMuddy))

If I specify the type explicitly, the tool with a light bulb gutter icon tells me:

"Use implicitly typed local variable declaration | use 'var'"

If I acquiesce and allow it to convert the explicit to the implicit, another tool (the one with a pencil icon in the gutter) pipes up and says, "Specify type explicitly" and changes the 'var' back to 'StreamReader'

I am stuck in an endless loop changing explicit to implicit and back again. My take on it is it doesn't really matter, but I throw this conundrum out to the wisdom of the StackOverflow crowd.

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 2
    Except where explicit typing is impossible, the choice is entirely a personal preference. Just pick one and stick with it so your code is consistant. – Michael Edenfield Jun 18 '12 at 23:07
  • This is an inappropriate question for StackOverflow, It calls for discussion, speculation, and opinion. Voting to close as "not constructive". The [FAQ](http://stackoverflow.com/faq) is pretty clear about these sorts of questions not being a good fit for the design here. – Ken White Jun 18 '12 at 23:08
  • 1
    This discussion has been covered quite thoroughly here. No need to go over it again, especially considering these are matters of opinion. – Adam Robinson Jun 18 '12 at 23:08
  • Anyway, see [here](http://stackoverflow.com/questions/41479/use-of-var-keyword-in-c-sharp/2866064#2866064) – Adam Robinson Jun 18 '12 at 23:09
  • Right, don't go to sea with two chronometers. Get rid of (at least) one of the refactoring helpers. If you keep one, configure it to make reasonable refactorings, and just stick to it. For the pattern `xxx v = new xxx()` either works fine as long as the type is reasonably short. Personally I would prefer the specific type unless it's too long, and if it *is* too long, consider adding a `using`. – Guffa Jun 18 '12 at 23:32

4 Answers4

4

I prefer explicitly typed variables are they allow a reader to more quickly get a grasp on what the code is doing. They also help enforce stricter type safety (from a person reading the code's standpoint) when coding which help prevent a lot of errors during development due to not know exactly what type is being used.

The only time in my opinion that implicit typed variables should be used is during linq queries when various anonymous types are being returned.

secretformula
  • 6,414
  • 3
  • 33
  • 56
  • 3
    explicit and implicit typing have exactly identical type safety. The *compiler* knows what type your variables are at all times, even if you don't. – Michael Edenfield Jun 18 '12 at 23:09
  • For the code writer, and for performance sake, there really is no difference. But for the maintainer (most likely the writer, one year after he wrote it), explicit makes for easier and smoother maintaining. For example, var x = GetData().... what's x? Well, now we need to go into GetData() , see the type, and remember it. – Edgar Mar 26 '21 at 00:40
1

There's no difference. You can use both versions.

Darj
  • 1,403
  • 1
  • 17
  • 47
1

Unless the declaration would take unreasonable amounts of space, is unduly complicated, or you're not exactly sure what the type is, you should explicitly type. It generally improves readability, and makes logic easier to follow.

If however, you encounter any of the mentioned exceptions, use implicitly typed, because it will improve readability.

Var is also good for sorting through collections, when you have intermediate results.

3Pi
  • 1,814
  • 3
  • 19
  • 30
  • 1
    I find the exact opposite; explicit typing often wastes eyeball cycles with redundant or unneeded information; I implicitly type everything. – Michael Edenfield Jun 18 '12 at 23:08
1

Uninstall or reconfigure one of them, which, is much of a muchness. I prefer explicit where I can, but that's just me....

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39