Writing clean code is an art, it takes lot of discipline to write code that can be read by other developers. The most comprehensive book on this topic is by Robert C. Martin (Uncle Bob) and if you have not read his Clean Code book, then that is definitely the next book you should read!
Initially I thought of writing a single post and cover some of the things that Robert Martin mentioned in his legendary book, but then I felt that one post on this topic will not be enough. So I have decided to cover first step for writing clean code in this post, Names!
What is it about names that is so important? Well let me ask you something, how much time you took to name each of your child? Why didn’t you just named them XYZ 🙂 The answer is very obvious, our children are very precious for us, we love them and want to give them the best name possible, and we want their names to depict some of the characteristics and qualities that we want them to possess.
What we need to learn as professional developers is to take the same amount of care and interest while naming our code components.
Use Intention Revealing Names
Whenever you are naming any component of your code, make sure it reveals the intention behind its creation. Look at some of the examples given below and see which ones reveal their intentions correctly.
// Reveal your intentions bool status; bool IsConnectedToDatabase; int d; int elapsedTimeInDays; int fDuration; int fileAgeInDays;
Avoid Misleading and confusing names
What’s worse than a poorly named variable or function? Misleading or confusing name off course! Let’s look at some examples
// CustomerList is not a list at all, its an array Customer CustomerList; Customer Customers; // what exactly is status? bool status; bool IsValidSubmission; // Information can be anything void GetInfo(); void GetRegisteredUsers(); // method code is registering users and also sending them welcome email void RegisterUser();
I have highlighted the problem with each declaration in code above with a comment and then tried to fix it in the right hand side. If you read both names, you will see that the ones on the right are clearer and much more precise. Finally in the last example, I have highlighted a very common problem that I see very frequently in code, the method is doing much more than what it’s name says.
It is extremely important to come up with a naming convention and then stick to it. It does not matter if you want to call your method Get or Fetch but once you have established your naming convention, you must stick to it, the last thing you want to see in a code base is every developer coming up with their own naming convention.
Class names should be Noun or Noun Phrases
Classes are objects, objects that represent things we want them to model, so it makes perfect sense to use nouns or noun phrases when naming your classes. Naming your class Account, Transaction, AccountHolder, WireTransfer etc makes perfect sense and it also makes them searchable.
Method names should be Verb or Verb Phrases
Methods represent behavior therefore their names should ideally be a verb or verb phrase like Save(), SendMessage(), RedirectToHomePage() etc. what Robert Martin also preaches is to make your methods as small as possible, there is no such thing as a too small function. Once you have written a method, strive to make it smaller, once done, strive to make it even smaller 🙂
Naming for me is by far the most important step in making your code cleaner and easier to understand, it is the first rule for writing clean code. Next time you are writing code, try to come up with better names for your code segments, I am sure this will prove to be a giant step towards writing clean code.