Coding Best Practice #2 : Meaningful Names

Recently I came across following quote while reading the coding bible Clean Code :

Master programmers think of systems as stories to be told rather than programs to be written.

In simple words, it has changed my way of looking at a piece of code and going to influence every single bit of code I will be writing now onwards.

One of the must haves of a clean, beautiful, expressive code is meaningful names. There is a whole chapter in the book discussing what good and bad names are and how to come up with meaningful names which you should read. However, if you are looking for a quick summary, please read through the list below:

– Use intention revealing names. Ex, instead of using variable string day, that represents day of month declare a variable string dayOfMonth.

– Do not use acronyms, or pun intending names. Do not try to be cute (ex: naming moq instead of mock).

– Using nomenclature of the solution domain rather than problem domain is mostly a good idea. Unless the code deals specifically with the problem domain.

– Avoid using synonyms in the code. Decide upon one word to convey a terminology, a state or an action and stick to the same.

– While coming up for the names of classes always think of the one thing it represents or encapsulates.

– While coming up for the name of methods, think of the one action it shall do.

I strongly suggest to come up with proper naming conventions for your team. Document them in the team wiki and keep them in mind when performing a code review, training a new team member or writing a new code yourself.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s