Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming convention for private fields

First, I know this question has been asked several times before and that in the end, it is mostly a matter of personal preference, but reading all the threads about the subject, some things are not clear to me.

Basically, something that most people agree with at least is that public member should be PascalCased while private members should be lowerCamelCased.

The matter that usually brings debate is whether or not to prefix the private members by an underscore, or anything else. Prefixing violates several StyleCop rules (which can obviously be turned off though)

The rationale to not prefixing is that you should use this. to prefix instead.

The problem I have is that I don't understand how it is making a difference? I mean, it is not like you can't use this on a public member anyway inside a class.

Let's imagine a class Customer, looking like this:

class Customer {     private int age;      public int Age     {         get { return this.age; }         set { this.age = value; }     } } 

(Obviously, in such a simple case, I could use an autoproperty, but that's just an example).

If I added a second property inside this class, nothing would prevent me to refer to it using this.Age (the public property) rather than this.age (the private field). Sometimes, it could even be wishable, if some validation or formatting was applied at the getter level.

Also, if some other properties of my class needed to modify the customer's Age, it would make sense to use the property rather than the backing field directly as the setter could also implement some business rules validations, right?

In other words, I really don't see how the this keyword avoids the confusion between private backing members and public properties as this can be used on both and IntelliSense shows both?

Thanks.

like image 933
Kharlos Dominguez Avatar asked Dec 27 '10 16:12

Kharlos Dominguez


People also ask

How do you name a private variable?

We have to start a variable name with a double underscore to represent it as a private variable (not really). Example:- one, two, etc..,. As we already said the variables whose names start with a double underscore are not private.

How do you name a private field in C#?

Use camel casing ("camelCasing") when naming private or internal fields, and prefix them with _ . When editing C# code that follows these naming conventions in an IDE that supports statement completion, typing _ will show all of the object-scoped members.

What should be the naming convention for methods?

Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized. Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter. Internal words start with capital letters.

What is Pascal naming convention?

Pascal case -- or PascalCase -- is a programming naming convention where the first letter of each compound word in a variable is capitalized. The use of descriptive variable names is a software development best practice. However, modern programming languages do not allow variables names to include blank spaces.


1 Answers

I strongly prefer the leading "_" convention for private fields, even though it does not follow MS conventions:

  1. It eliminates conflicts with camel cased parameter names - no need to use "this"

  2. It's a visual indicator that the internal persistent state of the object is being read, or - more importantly - being written. It's a flag saying "this has side effects outside of the particular method I happen to be looking at", which is very important to know when looking at unfamiliar code.

like image 89
Tom Bushell Avatar answered Sep 22 '22 16:09

Tom Bushell