This is one of the series of articles “Coding guidelines and Best practices.”, and today we will cover Naming conventions.
- What is Naming Convention?
- Why Naming Convention is required?
- What are different types of Naming conventions?
- How to decide Conventions?
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 a 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 a quite 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 apprehend.
And for readability naming convention is 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 a very difficult when every programmer in a team use 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 handover
So why every company defines centralized Naming Conventions and brings consistency.
- Readability and consistency reduced effort needed 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 different types of Naming conventions?
The first golden rule everyone should follow is “Variable name should be meaning full.”
There are lots of common conventions are 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 ensuing 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 how we think, it’s actually a though process thing. Let me share my thought about the same.
- Nowadays all IDE’s (like visual studio) provides a great Intellisense, so using Hungarian notation will not be a good idea. It’s 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 in which you are comfortable. For instance in Java, Camel case is used extensively where as in .net Microsoft preferred Pascal (upper camel case).
I am a d.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 start 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 to handy when we have a class with say hundreds of data members (combination of private and other). And when it’s comes accessing any private member inside member function, developer just presses ‘_’ and Intellisense will show all private members at top.
This article 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 have their own significance, when comes to readability of code.
Hope all of you enjoyed reading this article. For technical training related to .net, design patterns, jQuery , WCF and more contact SukeshMarla@Gmail.com
Keep updated about articles - Facebook, twitter@SukeshMarla.