In my opinion, the "best practice" here is, "don't do that." If I run across that in code I'm reviewing, I immediately flag it. Having two variables that differ only by case is a misunderstanding just waiting to happen. It's just too easy for a maintenance programmer to come along months or years later and inadvertently make an assigment to myThing
instead of MyThing
.
Added later:
A commenter asked for my suggestion to replace the upper/lower case naming convention. For that I need a concrete example. Say you have a simple Book
class that has only one property: Title
:
public class Book
{
public string Title { get; private set; }
}
Now you need a constructor. A common convention is to use a lowercase version of the property:
public Book(string title)
{
Title = title;
}
Or, if you want to make sure there's no ambiguity: this.Title = title
.
One can make the argument that this is okay in constructors. And it might be, if all constructors were so simple. But my experience has been that when a constructor goes beyond just a few lines, the distinction between Title
and title
gets lost. The problem becomes worse when you're talking about methods other than constructors. Either way, you need a different convention.
What to use? I've variously used and seen used abbreviations in the parameters: ttl
, for example. Or something like bookTitle
, which is more descriptive when using Intellisense. In my opinion, either is preferable to the convention of using a name that differs only by case.