Introduction

Some people use underscore with private variables and some peoples use camel case with private variables. Another issue he knows that why var was introduced; but now peoples are using “var” everywhere as a data-type.

Naming Convention for Private Member Variables

Question: Which convention is standard with the underscore or without underscore according to the best practice guidance?

Answer: it depends on the personal preference of the developer; because some peoples don't follow the standard guidelines, they have their own preference.

If everybody follows their own preference then it's difficult to review and maintain for another person.

Investigation to Underscore or not to Underscore

Few programming languages are case-insensitive, for example, say, Visual Basic (old version). In the VB, there is no difference between pascal case (i.e., VariableName) and camel case (i.e., variableName). Therefore, they have to use underscore with the private member variable (i.e, _variableName).

C# is a case-sensitive programming language and it knows the difference between pascal-case (VariableName) and camel-case (variableName).

Therefore,

  • If C# is a case-sensitive programming language, then why are we using underscore (_) prefix before private member variable?
  • What is the extra benefit to using the underscore (_) prefix before private member variable?
  • Am I a doer only? Few peoples are following exceptional convention and that's why, are we using the same convention?
  • Is it the beauty of readability?
  • In the best practice guidelines, it is clearly suggested that use camel-case for the private variable, then what is the problem to follow the general guidelines?
  • If peoples use the underscore with the private variables, then this is an exceptional case. The exception is not an example of the best practice, then why are they arguing with these?

Solution:  Be a THINKER first, then DOER.

Real-life Investigation

  • In C++, underscore was used for private variables.  Hungarian notation (i.e., 'm_') was used as a prefix with member variables for MFC. But current practice states "Don't use underscores at-all".
  • If I have the same name for the private variables and parameters, then I'm using an underscore to avoid the problem. This is not a good reason to use underscore in C#. What happens if peoples start to use underscore with the parameters?
  • Only use underscore if you are writing unit testing methods. In BDD naming convention, underscore is used with test methods to make it more readable.
  • For example:

    Given_Zero_Values_As_Params_When_GetSum_IsCalled_Then_It_Should_Throw_Invalid_Argument_Exception.

  • If I use any intelligence tool and if it doesn't have best practice as a default (say default is underscored with private variable), then there is an option to change the default settings.
  • I know programming very well to solve any problem quickly. I have implemented hundreds of applications. But if I used to write code according to my personal preference, then it doesn't mean that I know all of the best practices.

Remember, tomorrow doesn't come; but tomorrow NEVER dies.

Underscore vs. this

  • If I use the same name for the private variables and parameters, then I have to use 'this' with a private variable.
  • This is good practice if you use 'this' for all references to the private variables.

Underscore is Thorn for Private Variable

  • The doctor sometimes uses drugs as a treatment to the patient; but now if general people start to use the same drug for their pleasure, then this is illegal; because it is harmful to the human body.
  • If I'm in the Amazon rain-forest as a survivor game changer, then it okay to eat some raw food. Because, there is no alternative option. It doesn't mean that - in my real life, every time I will eat that raw food.
  • Similarly, I'm writing code using VB (old) or C++. So, in VB, there is no difference between Pascal case (i.e., VariableName) and camel case (i.e., variableName). Therefore, I am using underscore with the private member variable (i.e., _variableName). It doesn't mean that this is the standard for all of the languages.

Therefore the worst excuse

  • I'm used to using underscore with the private variable
  • I personally believe that it is the best.
  • I have self-explanation that it is easy to find out the private member variables or easy to read
  • I don't need to write 'this' with private variables.
  • I don't need to use 'this' when both private variables and parameters have the same name or blah- blah reasons.

Moral Points

Some programming languages use underscore because they have some explanation for that.

  • Now I want to fit the underscore everywhere. Even, I know that C# doesn't have any limitation that it needs to use underscore with private variables. But I want to fit it by hook or by crook.
  • This is my personal preference and I'm writing it only for me; in future, nobody needs to maintain it.
  • I don't need to worry about the other team members or the best practice guidance.
  • Even I am leading a team and I am advising the team members to do the worst practice like me.

These kind of immature thoughts are one kind of anti-pattern such as GOLDEN HAMMER.

I am not writing the code only for the machine or myself. If so, then it doesn't need modern languages, in general, it just knows only 0 and 1.

Remember, I am writing code for human; so that, different peoples can maintain it. If I write code for me only, then I don't need to follow any best practices. In these case, my personal preference is enough.

Golden Hammer - Anti-Pattern

"If all you have is a hammer, everything looks like a nail."

I'm using a particular technology, tools, methodology or architecture to solve all kinds of problems; although I know that there are alternative and better solutions to solve that problem. Only because, I'm familiar and used to it.

Personal Preference vs. Best Practice

Definition of best practices:

"A procedure or set of procedures that is preferred or considered standard within an organization, industry, etc."

Think, why you need international language? Why aren't you communicating using your local or personal preferred languages to the world? We all know the answer, I'm avoiding it to make it short.

If you follow the best practice guidelines, then there is no question at-all.  Although, there are some exceptions and they are used to it. They don't care about the best practice. But exception is not the best example to follow. If you follow that, then sometimes be ready to face "okay, but ...

Best Practice

  • Don't use Hungarian notation
  • Don't use an Underscore prefix for private member variables.
  • Do use camel casing for private member variables
  • Do use 'this' for all references to the private variables. (For VB, use 'me')

Keep personal preferences aside, pick the best technique to solve the problem.

General naming convention from Microsoft - "Don't use underscore and Hungarian notation".

References

First Reference

Second Reference

Improper Use of 'VAR' Everywhere

Have Questions

  • In the above example, are these the beauty of readability?
  • Why am I using var for a simple declaration?
  • Why, I can't say that these kind of improper use, are an anti-pattern?

Investigation of var or not to var

'var' should not be used in simple declarations, shown as below:

When must 'var' be used

  • Should be used in LINQ
  • Should be used with anonymous types.

When Can't It Be Used

There is a restriction, declaring local variables within the method or property, including iteration variable in 'for' or 'for each' statements.

  • As a type of field
  • As a type of parameter
  • As a return type of method or property
  • As a type parameter in generic type or method

Solution: First, be a thinker, then doer.

Anti-Pattern using VAR Everywhere

Why was var introduced? It was introduced for LINQ, anonymous types, etc.