Apply 'Use explicit type' everywhere, but only for built-in types?

We have a large code base, and frequently 'var' is used instead of 'int' or 'string', which, in my opinion, makes code harder to read -- explicit types are a GoodThing(TM). :-)  Therefore, my goals are:

(1) Eliminate all usages of 'var' where it is used instead of a built-in type (basically, all types that have a designated C# keyword, i.e. 'string', 'int', 'long', 'bool', 'decimal' etc.) and use the explicit type instead of 'var'.  Reason:  Little is gained by using 'var' instead of 'string' for example, but the code is easier to read if you see the exact type of a variable.

(2) For all other usages of 'var' I would like to apply no changes at all to the source code.  Reason:  For longer or more complicated type names, using 'var' can result in code that is easier or harder (!) to read; this depends on a few more things than whether or not 'var' is used, and therefore I'd like to leave those 'var' usages the way they are.

(3) Also, although technically 'int?' is not a built-in type, I would like to use 'int?' (or 'decimal?' etc.) instead of 'var' everywhere.  Reason:  'int?' looks much more like 'int' than it looks like 'IEnumerable<int>'.  'int?' is a special shorter name for 'Nullable<int>', just like 'int' is a special shorter name for 'System.Int32'.  Both are short and readable, and 'var' does not improve code readability here.

Now, I already found some relevant ReSharper settings:  I've set my "Preferred 'var' style" settings to 'Use explicit type' for built-in types, and the corresponding inspection severity to "warning".  I set the inspection severity to "Do not show" for "Simple types" and "Elsewhere".  When I check the "Code Issues in Solution" window, all usages of 'var' instead of 'int' or 'string' for example, are displayed as warnings.

I can achieve goal 1, but only with a lot of "collateral damage"... ;-)  It seems I cannot easily achieve all of my goals, for several reasons:

* It seems impossible to achieve both goals 1 and 2 at the same time.  From the "Code Issues in Solution" window, I cannot directly apply the changes that ReSharper offers me when I navigate to a single source location that triggers one of the warnings.  And if I do that, I can only choose to "Use explicit type everywhere in solution", but that includes, apart from the "built-in types" (goal 1), also "simple types" and "elsewhere" (so goodbye, goal 2).  I would like to apply the selected 'var' style to the "built-in types" code issues *only*.  Can this be done?  Another solution would be an additional option "Do not change" in the "Preferred 'var' style" options -- currently, I can only choose from "Use 'var'", "Use 'var' when evident", and "Use explicit type".  In fact, I think this would be the perfect solution to achieve my goals 1 and 2:  I would like to tell ReSharper to use explicit types for built-in types, and to leave the code as it is and not apply any changes (goal 2) for other 'var' usages.

* I see no way of achieving goal 3 because ReSharper does not see Nullable<T> types as built-in types -- although 'int?' is much shorter than 'Nullable<int>' and "looks like" a built-in type.  As far as I can tell, for the "Use preferred 'var' style" feature, ReSharper puts 'Nullable<int>' (and 'int?') in exactly the same category ("Elsewhere") as, e.g., 'IEnumerable<int>' or 'Dictionary<int, string>'.  It would be great if Nullable<T> types were treated as built-in types here...  after all, they are special: they have special short names ('T?').

So...  is there any way I can achieve my goals without manually looking at hundreds of locations in the source code and applying ReSharper fixes ("Use explicit type") one by one?

1 comment
Comment actions Permalink

I have the same issue.  I'm tempted to apply the 'use explicit type' rule everywhere in the code base, then use a bit of Perl to compare the before and after code line by line, putting it back to 'var' where the new explicit type is not one of the builtin ones or a nullable builtin one.


Please sign in to leave a comment.