2011-08-22

Goodbye _, Hello R# Color Identifiers

I've been prefixing my C# class members with _ (underscore) since the first version of C#. I really do like to be able to distinguish between instance and function scope.

I've also obeyed the larger part of the classic IDesign Coding Standard for who knows how long.

Therein, rule #67 states: "Do not use the this reference unless invoking another constructor from within a constructor." Usually, you'll use the same name for your fields and constructor arguments, why a this reference is needed if the fields aren't prefixed.

While re-reading the great book Clean Code authored by Uncle Bob today, I came across the following statement on page 24: "You […] don't need to prefix member variables […] anymore. […] you should be using an editing environment that highlights or colorizes members to make them distinct.".

The argument appealed to me, but how should I follow that advise? As a C# programmer, you're more or less forced to use Visual Studio. I knew, despite using ReSharper (R#) since its first version, that I've never come across field/member highlightning in the Fonts and Colors section.

After some googling, I finally found what I was looking for in R#; you need to enable Color Identifiers. Once I'd done that, the "ReSharper Field Identifier" (and more) show up among the display items in the aforementioned Fonts and Colors section.

Goodbye _, hello color identifiers!

Now I need to write a refactoring regex that refactors old, prefixed code…


UPDATE 2011-08-24: I've now refactored two C# solutions where I got rid of the prefixes by using VS' find and replace with regex and capture groups like so:
find what: private {.*} _{.*}
replace with: private \1 \2
and similar expressions.

1 comment:

  1. I completely agree!! _ is for dinosaur developers! _ reads ugly, and requires unnecesary typing when coding. Worse, this extra typing requires you to use the shift key too.

    ReplyDelete