Skip to main content

Code conventions: Underscore on private fields

Why would you put underscore on private fields in a class?

  • Because it is easier to determine in a method if the variable being used is a private field or not.

Why would you not put underscore on private fields in a class?

  • Because you already state in the declaration of the field that it is private. The underscore is duplicating that purpose. It would be the same (in a horrifying and extreme kind of way) as

    private static MySingleton privateStaticFieldInstance;
  • There already is a solution to the problem. If you put this-keyword when you use a private field, it will become just as clear as the underscore. This is a built in language feature and should be used before any made up convention.

    return this.instance;
  • Using this is also much better out of readability. One does not have to learn the convention of underscore. This is a part of C# and therefor well documented for the reader to look up.

Now, let's dance!

comments powered by Disqus