I am screaming internally whenever I see "var" in C#
Hugging hell I hate it

  • 2
    Haha it reminds me of vanilla junkscript, I mean JavaScript.
  • 2
    Just came here to say I've released a huge hugging evil...
  • 3
  • 12
    What's the big deal with this? Doesn't C# infer the types even when you do use "var"?
  • 1
    Java 10 has var. Local variable type inference.
  • 5
    If I initialize the variable when defining it I tend to use var for everything that isn't a primitive.
    If you don't initialize it directly you can't even use var so I don't see the problem here :D
  • 19
    I change any explicit declaration I see to var. If you don't like var, you don't understand it. By not writing out a verbose declaration, you will almost always make the code more condensed, less cluttered and thus more readable.
    If you require an explicit declaration to understand the code, then you need to get better at understanding code in general.
    Var does not mean dynamic. Var just means that you already declared the type on the right side of the statement, and doing it again would be redundant.
  • 6
    I understand the OP, at least partially. I mean, it is OK if you can immediately see which type the variable has, like var x = "Some string";, but I absolutely hate it when I see something like var x = RandomFactoryFunction().getUnknownTypeVariable().InstantiateNeverMentionedObject(); where you seriously either have to claw your way through the code of all mentioned classes to determine which type it is, or just guess (which in my case is 99% of time wrong).
    Seriously, I am wasting my time because some other person tried to be lazy by saving some characters while typing.
  • 8
    @TobiSGD You need a better IDE. Visual Studio tells me the type of a var 100% of the time. Using var is not lazy. It is best practice because it avoids redundancy.
  • 4
    @pleuph Maybe I misunderstand, but where does it reduce redundancy?
    All It does is to make the code less readable, and that is, at least for me, a total no-go.
  • 3
    @jibberdev919 I am not talking about a 500-character-string on the right side, but an assignment, where it is not immediately clear which type will be returned, something you see very often when functions are chained together.
    And having to ask the IDE which type will be returned because the code is otherwise unreadable is IMHO quite stupid.
    Especially since there is a simple fix for it: Tell me which type it is by not using var in the first place.
  • 2
    MyClass myClass = new MyClass();
    var myClass = new MyClass();

    In the first statement, the MyClass type name on the left side of the declaration is redundant, since the right side also specifies the type. The second statement is shorter, and thus more readable. This is especially apparent when you have long or more convoluted types, like nested generics etc.
  • 2
    @TobiSGD Not utilizing the IDE or using common sense to figure out what the return type of an assignment is is a lot more stupid than cluttering up the code with redundant declarations.
  • 3
    @pleuph As I said, for simple statements, like your MyClass example, I am on your side.

    It is the "nested types" stuff where it is not immediately obvious which type is returned that I hate.

    As one of my teachers said: There are two audiences for the developer:
    1. The compiler
    2. Other developers reading your code
    While var-statements with some nested stuff on the assignment side may be easy to read for audience 1 (and with that also for the IDE), it is not easy to read for audience 2.
    And if audience 2 has to ask audience 1 first which type is actually returned than you have failed audience 2, you made your code less readable for them.
    And since it doesn't matter for the compiler at all if you use the real type or var it is in my opinion stupid to use var for complicated cases.
  • 1
    @TobiSGD When dealing with long type names, assuming audience 2 knows:
    1. What var means
    2. How to use the IDE or common sense to find the type
    the code is more readable with var. Always.
    If anyone in audience 2 does not know either 1 or 2, they need to be taught this. There is no shame in not knowing something, but there is in knowingly writing more verbose code because of a personal preference.
  • 2
    @pleuph We describe two situations here, which have a common feature: It is not obvious which type a given function returns.
    Situation 1: The developer uses var instead of the real type. Since it is not obvious which type is meant other developers have to either look at that function and its return type, or have to ask the IDE for the return type.
    Situation 2: The developer uses the real type instead of var. This makes it immediately obvious to other developers, which type is used.

    So, in situation 1 an extra step is required, which is not required in situation 2.
    I still can't see how having to add an extra step makes things easier, sorry.
  • 1
    @TobiSGD Edge case. That's a reason to hate shitty code, not a reason to hate the tool used to commit the atrocity.
  • 2
    @nathanchere I can still hate it for making it so easy to write shitty code. ;)
  • 2
    @TobiSGD then you should hate var, the entire .NET Framework, assembly language, modern IDEs, widely accessible personal computers and everything in between :P
  • 1
    @TobiSGD When using always using var, you have these three situations:
    1. The type is obvious from the right hand side of the declaration
    2. The type is obvious from usage or context
    3. The type is found with a very quick lookup (usually a mouse-hover)

    So only in 1 of 3 situations do we need an extra step to know the type. And in that situation, you would most likely need a deeper understanding of the context anyway.

    The cost of not using var, in terms of how much unnecessary extra reading is required by anyone looking at the code, I would say is higher than the cost of looking up the type in those few situations.
  • 0
    @jibberdev919 Yes, I worded it that way specifically because I do not have any statistics to support the claim. I do have 12 years of experience though :-)
  • 0
    @jibberdev919 (and every acknowledged best practice guru's word of course ;-))
  • 1
    Ah, this reminds me of all the arguments in the C++ community over the use and usefulness of the auto keyword, which is basically the same thing as var.
    Same arguments.
  • 1
    😈😈😈 My whole codebase is with var in C#
  • 0
    @jibberdev919 I like the pattern, the more I see of the same, the less I care about redundant information.
  • 0
    @irene I know, was reading through Scott Meyers' books
  • 0
    @jibberdev919 No offence taken, I understood what you were saying :-)
  • 0
    @jibberdev919 so you would have explained this about 3000 times?
  • 0
    If the type is on the RHS of the expression then why add it to the left too? Just use var
  • 0
    Duck, me too!
  • 1
    DYNAMIC obJEcT = new DYNAMIC();
  • 0
    @pleuph I find myself using quick documentation when in doubt.
  • 0

    var milk = new Milk();

    I don’t think you need to explicitly say it’s Milk in this case.
  • 1
    @rEaL-jAsE Like... this ?

    IMilk milk = new SpecialMilk();
  • 0
    @rEaL-jAsE I just don’t understand what you are trying to do.
  • 1
    This thread - TL:DR
    var is fine in C#
    You are wrong

    var x = new Thing();

    I know type of x is Thing, I don't need more info than that.
    Beyond that, use the IDE, that's what it's for.
Add Comment