1

I was watching a youtube video on how to write cleaner code. The creator showed an example where instead of writing: SqlCommand command = new SqlCommand(query, connection); it is much cleaner to write: var command = new SqlCommand(query, connection);

Now I'm curious: is there any performance differences with using var? Is this a good way to write clean code (should I start using var?)

  • 2
    No performance difference, the compiler will convert it to the right type for you. – DavidG Mar 17 '17 at 10:51
  • 1
    Use `var` if you prefer it visually, its only in the code it matters. Personally I prefer using the Type itself cause I find it a lot easier to read, but that's just taste. – Jite Mar 17 '17 at 10:52
  • There is not any performace difference. I prefer not to use `var` except in some LINQ results, because i like to see what type a variable is at a first sight – Pikoh Mar 17 '17 at 10:52
  • 1
    Don't start using it everywhere, though. `var i = 5` doesn't make the code more readable. But `var map = new Dictionary>()` does. – Dennis_E Mar 17 '17 at 10:54
  • Showing the type twice is redundant, like saying the "Department of Redundancy Department." That does not improve readability for me. – Danny Ellis Jr. Mar 17 '17 at 10:56

2 Answers2

8

No, there are no performance differences at runtime. var is changed at compile time to the appropriate type.

As for how good of a practice it is: I usually teach my students to use it when the type of the left hand side is clear from the right hand side. Such as:

List<int> myList=new List<int>();

Here it is clear from the right side, what the left side is. Same goes for factory-like methods. What I don't like is when you have a method like this:

private static int DoSomethingAwesome() {...}

And then it is called like

var x = DoSomethingAwesome();

See how you cannot tell the type of the left side just by reading the code (the return type is not indicated any way whatsoever)? But I guess this might be a little up to taste or opinion.

Akos Nagy
  • 4,201
  • 1
  • 20
  • 37
  • 3
    This question is clearly a duplicate of others, you should not answer hoping for reputation points, instead you should flag it as a duplicate and overcome. – Massimiliano Kraus Mar 17 '17 at 10:58
  • 1
    If your variable wasn't named 'x' it might matter less than in your example. – Danny Ellis Jr. Mar 17 '17 at 10:58
  • 1
    @DannyEllisJr. that's a good point. Still cannot actually tell the type though. – Akos Nagy Mar 17 '17 at 11:02
  • 2
    @MassimilianoKraus I mostly answer hoping that it might help others, not for the reputation (of course it would be hypocritical to say that I don't like gaining rep). I also assume that whoever asks a question has done their due diligence searching for an answer before asking, and they have not found what they were looking for, so I try to answer to the best of my knowledge. If it is indeed a duplicate and somebody flags it, that's fine of course. – Akos Nagy Mar 17 '17 at 11:10
  • @AkosNagy a simple Google search would have revealed to OP (and to you) that there were already hundreds of discussions (on SO and not) about this topic, in the use of "var", its usefulness, its performance implications, etc. Repeating answers has the only result of increasing the general entropy. – Massimiliano Kraus Mar 17 '17 at 11:19
  • 2
    @Massimiliano Kraus The problem with the whole duplicate question thing is that you are forced to assume that the previous question's answers are actually correct or "best." Many accepted answers are actually wrong or previous questions do not contain enough information. If you don't trust the previous answer, what are you to do but ask again? – Danny Ellis Jr. Mar 17 '17 at 16:05
  • If you teach CS, you may be interested in the new [CS Teacher's Stack Exchange](http://cseducators.stackexchange.com) (though since it's still in private beta, it's easiest to enter [through here](https://area51.stackexchange.com/proposals/92460/computer-science-educators)) – Ben I. Jun 02 '17 at 19:07
  • To anyone reading this now note you can now do List myList = new(); allowing the left side to be explicit without being redundant on the right side – user4779 Jan 29 '23 at 10:56
0

No difference in performance, but I personally think it improves readability. Also, if you have to change the type you only have to change it at one end of the line (very minor thing, obviously but it happens to me all the time.)

Danny Ellis Jr.
  • 1,674
  • 2
  • 23
  • 38
  • 2
    This question is clearly a duplicate of others, you should not answer hoping for reputation points, instead you should flag it as a duplicate and overcome. – Massimiliano Kraus Mar 17 '17 at 10:58