CodeProject
Introduction
This is one of the series of articles “Coding guidelines and Best practices”, and today we will cover Naming conventions.
Contents
What is Naming Convention?
Naming convention is something which answers a question “How to name an element.”
In programming, it can be defined as set of rules which tells how an identifier will be named, like:
- What all characters are permissible?
- How it should be formatted or organized?
And so on...
When it comes to naming, it’s quite a contentious topic.
Why Naming Convention is Required?
Try to read the following word:
It’s challenging to read it in a single glance.
Now try to read this one:
The answer is:
- Respectable programming is that where code is healthy written, which can be easily read and apprehended.
And for readability, naming convention is a must. No one can read my name if I write like “sukeshmarla”, but can if it’s like “Sukesh Marla”.
- When we talk about a project, we have a team. And it will be very difficult when every programmer in a team uses his own way or standard for defining variables. Difficulties in both management and understanding. It becomes even more difficult when a team member resigns and new guys take over.
So why every company defines centralized Naming Conventions and brings consistency.
- Readability and consistency need reduced effort to read and understand the code (especially written by other developers).
- A proper naming convention with modern enhanced Intellisense feature drastically improves the development speed.
What are the Different Types of Naming Conventions?
The first golden rule everyone should follow is “Variable name should be meaningful.”
There are lots of common conventions out there for naming Identifiers. Some of them are:
- Pascal (aka Upper Camel) - First letter of every single word will be capital.
Example:
GradeOfCustomer
- Camel - Begins with lower case and then capitalizes first letter of every following word.
Example:
gradeOfCustomer
- Hungarian - Variables are prefixed with some kind of information like Data Types, Modifiers, etc.
Example:
strCustomerName
, intCustomerAge
How to Decide Conventions?
We already spoke about importance about naming conventions. Choosing convention completely depends on how we think, it’s actually a though process thing. Let me share my thoughts about the same.
- Nowadays, all IDEs (like Visual Studio) provide a great Intellisense, so using Hungarian notation will not be a good idea. It just makes variable more complicated to read.
- Pascal or Camel - we must choose at least one of them. But which one? Well, it’s up to which you are comfortable in. For instance in Java, Camel case is used extensively whereas in .NET, Microsoft preferred Pascal (upper camel case).
I am a .NET developer, so I prefer Pascal casing for class members and Camel for local members
(members defined inside a function or function parameters).
- Other than Pascal and Camel, some people often use one more notation where name starts with underscore followed by camel casing.
For example
_age
, _dateOfBirth
.
Normally, this notation is used for naming private
data members.
I personally feel great about this.
It comes in handy when we have a class with say hundreds of data members (combination of private
and others). And when it comes to accessing any private
member inside member function, developer just presses ‘_
’ and Intellisense will show all private
members at the top.
Conclusion
This post cannot be treated as a final documented Naming convention document. Here, I tried to explain the importance of naming convention and how to choose them.
As I said, choosing a naming convention is completely a thought process. We should remember them by their significance, not just by mugging documents. Pascal, Camel, Hungarian each of them has their own significance, when it comes to readability of code.
Hope all of you enjoyed reading this post.
Keep updated on articles - Facebook, twitter@SukeshMarla.
For technical training related to various topics including ASP.NET, Design Patterns, WCF and MVC contact SukeshMarla[at]Gmail.com or at www.sukesh-marla.com
For more stuff like this click here. Subscribe to article updates or follow at twitter @SukeshMarla
<iframe width="560" height="315" src="http://www.youtube.com/embed/HO_wLFjBabQ" frameborder="0"> </iframe>