Wednesday, June 27, 2012

To var or not to var?

I have recently started using Resharper in Visual Studio 2010.  I enjoy using the tool and mostly agree with the suggestions it makes and rules it has for syntax and styling.  However, I find the rules around the var keyword to be going against my sense of code style.  Take the following code snippet:

int x = 12;

Resharper would have me use the var keyword here, per the rule "use 'var' keyword whenever possible".  So according to the resharper rule, this line should be written as:

var x = 12;

To me, this is less readable than the previous version.  Did I mean for x to be an int?  Maybe I needed an int16, or possibly a uint64?  Possibly I was mistaken altogether and needed a float.  For me, I would like the opposite rule, "use 'var' keyword only when required".

This is a big debate in the C# world.  More dicussion can be found at:

http://www.infoq.com/news/2008/05/CSharp-var

Personally, I come down on the side of minimal var usage.  When I want to use var everywhere and implicit type inference, I will go write some JavaScript.

No comments:

Post a Comment