To be dogmatic or not to be dogmatic approach to VAR

Jason Heine on December 5, 2008 in C#

Oh boy, there have been a ton of discussions and articles about the usage of the var in c# development.

 

My perspective on this is that developers are being too dogmatic in their views when it comes to the usage of the var.

 

Okay, so what do I mean by this? Let’s play devils advocate on both sides. I am only going to give a few examples of the pro’s and con’s of the var usage. There are many articles out there if you would like to see an in depth usage of the pro’s and con’s. The point of this article is to help you be less dogmatic about the usage of var.

 

First, let’s take the “I do not support the var” approach.

 

Point 1: I think it makes the code much more difficult to read.

Point 2: The developer’s intent is not immediately obvious.

Point 3: It can be overused hence causing confusion in code.

 

Okay, now we have seen lots of points in regards to the do not support perspective, let us know look at the “I do support the var” approach.

 

Point 1: Var removes code noise.

Point 2: Var forces initializing to not null values.

Point 3: Var introduces flexibility inside the method scope.

 

 

If you really dig deep in these ideas, from both sides, you realize that both sides can be valid. Well, why don’t we take a less dogmatic approach and come up with the following points.

 

Point 1:

 

Use var when necessary. There are many scenarios where you may want to use var.

            Example:

            Don’t use var like this:

            var amount = engine.GetAmount();

            var amount = 10;

            var name = “Jason”

           

You get the picture. These examples show the un-necessity usage of var. For one, the first example shows that you have to clue what the type is unless you go into the method GetAmount(). The 2nd two are pointless because you know that name is going to be a string, you might as well just make it a string. The amount = 10, this could be an int, decimal, float or whatever. Hence, you need to have a type that indicates what that number really is.

 

            Example:

            Use var like this:

            var myList = new List<SomeObject>;

            foreach (var foo in barCollection)

           

Here you can see that you are declaring a list object, when you have a bunch of those (I think have had 10 or 12 in a row before) your code then does become unreadable. If you use var, you are giving an explicit definition to your variable, so you don’t care that you are using var. Also, why mess up your foreach with large variable names when you can declare the looping variable with a var making it much easier to read.

 

Some people will argue that putting a var in the looping variable in a foreach is not something you want to do. This is up to your personal discretion. Use it wisely.

 

 

So, back to the original point of this blog about being too dogmatic about the usage of var. We all need to sit back and look at the big picture. We need to use var when it is needed, and not use it when it does not make sense. If you can be less dogmatic about your approach to the usage of var, then you will for one, become a better programmer, for two, you will get to use a powerful feature of c#.

 

 

Leave a Reply