Click here to Skip to main content
15,881,757 members
Articles / Programming Languages / C#

Naming Conventions

Rate me:
Please Sign up or sign in to vote.
4.46/5 (7 votes)
27 Jul 2013CPOL4 min read 20.9K   10   5
Naming conventions

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:

  • gradeofamericancustomer

It’s challenging to read it in a single glance.

Now try to read this one:

  • GradeOfAmericanCustomer

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.

    _Image

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>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Just Compile
India India
Learning is fun but teaching is awesome.

Who I am? Trainer + consultant + Developer/Architect + Director of Just Compile

My Company - Just Compile

I can be seen in, @sukeshmarla or Facebook

Comments and Discussions

 
SuggestionLeft out a few Pin
Chad3F1-Aug-13 19:51
Chad3F1-Aug-13 19:51 
GeneralRe: My vote of 5 Pin
Marla Sukesh29-Jul-13 9:03
professional Marla Sukesh29-Jul-13 9:03 
GeneralMy vote of 4 Pin
Amir Mohammad Nasrollahi27-Jul-13 20:27
professionalAmir Mohammad Nasrollahi27-Jul-13 20:27 
GeneralRe: My vote of 4 Pin
Marla Sukesh27-Jul-13 20:44
professional Marla Sukesh27-Jul-13 20:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.