Be a Thinker First, Then a Doer

Coverage Topic

Let's Drill Down the Basic Concept

The Problem

Adam Rusch is my colleague and he is working as "Dot-Net C# developer" in a team. Code-review is the part of his job responsibility. So, during code review, he was confused about the coding standard for the private member variables. He means, some people use underscore with private variables and some peoples use camel case with private variables. Another issue he knows about is why var was introduced; but now people are using “var” everywhere as a data-type. He is considering C# as a programing languages and .NET as a development platform.

Naming Convention for Private Member Variables

Question

Adam asked me, which convention is standard with underscore or without underscore according to the best practice guidance?

Answer

I said, it depends on the personal preference of the developer; because some people don't follow the standard guidelines, they have their own preference.

But Adam was not satisfied with the answer. Because, if everybody follows his/her own preference then it's difficult to review and maintain for another person.

Investigation to Underscore or not to Underscore

Few programing 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 programing language and it knows the difference between pascal-case (VariableName) and camel-case (variableName).

Therefore,

Solution

Be a THINKER first, then a DOER.

Real-life Investigation

For example

Underscore vs. this

Underscore is a Thorn for Private Variable

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 most worst excuse,

Moral Points

I know, some programing languages use underscore. Because, they have some explanation for that.

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 humans; so that different people 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 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 do 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

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

Investigation to var or not to var

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

When Must 'var' be Used

When Can't It Be Used

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

Solution

First, be a thinker, then a doer.

Anti-Pattern using VAR Everywhere

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

Now if I use it everywhere, then it is one kind of anti-pattern such as Golden Hammer. So, avoid the anti-pattern. Because, it is like a thorn for the software development. It is better to avoid type guessing.

Please note:

I apologize, if I hurt your feelings. This is mainly a focus on the best practices in C# language. Few people who are used to their personal preference, this article is not pointing at them. This is general guidance for new developers.