C# Coding Standards – Parameter Names vs. Type Names

coding-standardsmethodsparameters

I see questions similar to this with regards to parameter names that match properties on the class, but I can't find anything regarding using a parameter name that is the same as the parameter type name except for casing in C#. It doesn't seem to be a violation that I can find, but is it considered bad practice? For example, I have the following method

public Range PadRange(Range range) {}

This method takes a range, and returns a new range that has had some padding applied. So, given the generic context, I can't think of a more descriptive name for the parameter. However, I'm reminded of a a tip I picked up when reading Code Complete about "psychological distance". It says

Psychological distance can be defined as the ease in which two items can be differentiated…As you debug, be ready for the problems caused by insufficient psychological distance between similar variable names and between similar routine names. As you construct code, choose names with large differences so that you can avoid the problem.

My method signature has a lot of "Range" going on, so it feels like it may be an issue with regards to this psychological distance. Now, I see many developers do the following

public Range PadRange(Range myRange) {}

I personally have a strong distaste for this convention. Adding a "my" prefix to variable names provides no additional context.

I also see the following

public Range PadRange(Range rangeToPad) {}

I like this better than the "my" prefixing, but still don't care for it overall. It just feels overly verbose to me, and reads awkwardly as a variable name. To me, it's understood that range will be padded because of the method name.

So with all this laid out, my gut is to go with the first signature. To me, it's clean. No need to force context when it's not needed. But am I doing myself or future developers a disservice with this convention? Am I violating a best practice?

Best Answer

Don't overthink this, Range range is fine. I use such kind of naming for more than 15 years in C#, and probably much longer in C++, and have never experienced any real drawbacks from it, quite the opposite.

Of course, when you have different local variables in the same scope, all of the same type, it will probably help to invest some mental effort to distinguish them properly.