James Millar

View Original

C# To var or not to var

So I’ve been in yet another ‘disagreement’ today over the use of var in C# today. Seems some people have some pretty strong opinions on its usage. As a team we’ve decided that we need to make a decision one way or another so I came up with this as a way of understanding the problem. Hopefully its of use to someone:

The "var" keyword in C# is a type inference feature that allows the compiler to automatically infer the data type of a variable based on its initial value. The use of "var" can bring several benefits to developers when compared to explicitly declaring the data type of a variable.

See this content in the original post
  1. Improved code readability: By using "var", developers can avoid having to explicitly specify the data type of a variable, which can make the code more readable and easier to understand. This is especially useful when working with complex data types or generic collections.

  2. Reduced code verbosity: When working with long or complex data types, the use of "var" can help reduce the amount of code needed to declare a variable. This can make the code more concise and easier to maintain.

  3. Better support for refactoring: When the data type of a variable is inferred using "var", it becomes more resilient to changes in the codebase. For example, if the data type of a variable is changed, the compiler will automatically update all the references to that variable, reducing the risk of breaking the code.

  4. Improved performance: In some cases, the use of "var" can lead to improved performance, as the compiler can generate more efficient code when the data type of a variable is inferred.

  5. Consistency with other modern languages: Many modern programming languages such as Python, JavaScript, and Go already use type inference, and it's becoming a common practice among developers. Using "var" in C# can help developers to have a more consistent experience across different languages.

As with everything, there are also disadvantages or at least potential negative impacts to using var such as:

  1. Reduced code clarity: By using "var", the data type of a variable is not explicitly specified, which can make it more difficult to understand the code at a glance. This can be especially confusing for developers who are new to the codebase or are working on a large codebase.

  2. Reduced code predictability: When working with "var", the data type of a variable is not explicitly specified, which can make it more difficult to predict how the code will behave. This can lead to unexpected behavior and bugs that are difficult to debug.

  3. Reduced code maintainability: Because the data type of a variable is not explicitly specified, it can be more difficult to maintain the codebase. For example, if the data type of a variable is changed, the compiler will not catch all the references to that variable, which can lead to bugs.

  4. Reduced code portability: When using "var", the code may become less portable, as it becomes dependent on the specific behavior of the C# compiler. This can make it more difficult to move the code to a different platform or to a different version of C#.

  5. Reduced code safety: By using "var" the data type of a variable is not explicitly specified, which can make it more difficult to ensure that the code is safe. For example, if a variable is declared as "var" instead of a specific data type, it can lead to unexpected behavior when working with null values.

So as you can see, the use of the "var" keyword can bring many benefits to you as a developer, but it's important to note that it's not always the best option, and developers should use it judiciously, and only when it improves the readability and maintainability of the code.

Now of course the ‘readability and maintainability’ of the code is always going to be subjective so I’m sure the disagreements will continue.

You can read more about these declaration statements by heading over to Microsoft Learn: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/declarations