Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On C# naming conventions for member variables

I have seen an advice somewhere here on SO to not name private/public member variables, in a way that they differ only by the case of the very first character. For instance:

private string logFileName;  public string LogFileName {     get     {         return logFilename     .... 

and: private System.Windows.Forms.MainMenu mainMenu;

and: DialogResult dialogResult = this.saveConfigFileDialog.ShowDialog();

and:

public Version Version {     get;     set; } 

and:

    private void CheckPollingType(PollingType pollingType)     { 

So, did I hear wrong? Is there anything wrong with these naming conventions? If yes, then what are better ways of doing it? Links, references are a plus.

Thanks.

like image 705
Hamish Grubijan Avatar asked Jun 04 '10 18:06

Hamish Grubijan


2 Answers

That is definitely a very popular naming convention and I don't see why you should be against it.

I would simply recommend following the Naming conventions for C# provided by MSDN and also General Naming Conventions provided by MSDN.

Specifically they have this to say about properties:

Do name properties using a noun, noun phrase, or an adjective.

Noun phrases or adjectives are appropriate for properties because properties hold data.

Do not use properties that match the names of Get methods.

For example do not name a property EmployeeRecord and also name a method GetEmployeeRecord. Developers will not know which member to use to accomplish their programming task.

Do name Boolean properties with an affirmative phrase (CanSeek instead of CantSeek). Optionally, you can also prefix Boolean properties with Is, Can, or Has, but only where it adds value.

Consider giving a property the same name as its type.

When you have a property that is strongly typed to an enumeration, the name of the property can be the same as the name of the enumeration. For example, if you have an enumeration named CacheLevel, a property that returns one of its values can also be named CacheLevel.

I think if there were a compelling reason to be against what you are suggesting they would have mentioned it in their guidelines.

like image 59
Brian R. Bondy Avatar answered Sep 16 '22 14:09

Brian R. Bondy


Most of the time class level variables are prepended with an underscore. So myVariable is actually _myVariable. A lot of people don't like varrying the name by one character, because it is too easy to make a mistake.

There is nothing wrong with just doing myVariable and MyVariable. It's just a convention, and if everyone follows it then it will probably work just fine.

Personally if at all possible I dispense with the private variable and just use the getters and setters in the property. Most of the time (but not all the time), accessing the private variable was used to to not allow write access in the property.
This can be solved by:

public String MyVariable {    get;    private set; } 
like image 37
kemiller2002 Avatar answered Sep 19 '22 14:09

kemiller2002