C# : var and Code Writability – Readability Issues

Recently got a subscription of ReSharper in my company. I am using Visual Studio since 6.0 and its the best IDE ever, probably the best Microsoft product also and after using VS without a tool like ReSharper than all of the sudden installing it and restarting VS, I felt like, WTH delay to open this IDE. My first impression of ReSharper is “They should have made it light weight”.

That is ReSharper’s story and one thing I noticed is the ‘HEAVY’ usage of var in ReSharper. ReSharper does this since he/she/it (hereafter I will call it as he, coz a good friend these days) obviously not a “data aware system” and he really don’t know why we are going to use that data.

So whenever ReSharper say var is that like, we must use, var number = 1990; instead of int number =1992;. For me obviously NO and we shouldn’t do it.

Code Writability
When it comes to code writability, if you think in terms of ‘Extreme Productivity’ and ‘Saving nano seconds’ you could go with var IsReSharperGood = true; instead of bool IsReSharperGood = true; so you can save nano seconds when you code. But its generally good to go with var numbers = new List(){1,2,3,4,5}; rather than List<int> numbers = new List<int>(){1,2,3,4,5};.because you save sometime by not typing 6 characters. other than built-in data types, var is really useful when it comes to other data types. But is this what we really need?

Code Readability
Is software all about writing code? NO. People define a best programmer on how reads and understand the code. In this video Bronwen Grimes says “You don’t have to be a genius to code, Do you have to be a genius to read?”. I don’t know if she mean “code reading” but if it is, my answer, probably is “yes, some kind of”. Because code is totally a different language like “Greek” or “Arabic” which is totally different from the grammar and literature of your language. So you have to be extra careful when reading it. This is why there is a job in the world called “Code Reviewers” exist and thats the main motivation to create something like GitHub.

Imagine you are reading a 14000 LOC class which has a not initiated & instantiated class variable defined as var animals; and when you scroll down to 11235th line you found animals.Add(cow); and in the 11236th line you need to get the count of animals by calling var numberOfAnimals = animals.Count by assuming animals is a List<T> and hit F5… DING..!!! ERROR. Because animals was not really a List<T>. It was a user defined type named Mammals which has a Add() method. You simply misunderstood this since it looked like a collection right? This is a very simple example and this is the place where we need to avoid var. Anther good example is var t = SomeMethod();, now you have to go to the definition of SomeMethod() to see what is that. You should always remember var has no backward compatibility, so you can not migrate your code back to C# 2.0 and below (but is there a need for it in a production environment?)

Update : I found a good research article on this topic 3 years after writing this post.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s