Anyone can write code that machine can understand. But you should write code that humans can understand. I know most the of the times we would give an temporary solution for a problem faced and we say that we can refactor it later. But it’s not gonna happen, if you are going to refactor it, do it now.
Writing clean code is hard & difficult. But once you focus and do it, it’ll be very useful.
“So if you want to go fast, if you want to get done quickly, if you want your code to be easy to write, make it easy to read.”
— ― Robert C. Martin — —
In this blog, we will see some of the basic parameters to write clean code irrespective of the programming language you use.
How to write Clean Code?
Naming Conventions
Naming is one of the important aspect of coding. Choosing good names may take some time but it will improve the readability of your code. Give proper names to your variables, functions, classes and objects such that it says, what it is actually doing. The names should tell you why it exists and how it is used.
int d; => d is describing number of fruits
You can instead declare it as
int numberOfFruits;
A name should be generic and it should be understandable. Declaring it as d, doesn’t give an proper idea what the variable is doing.
Class Names – A class name or object name should be in noun or noun phrases such as Customer, User, Account etc. And also a class name should not be a verb, such as Writer, Processor etc.,
Method Names – A method name should be in a verb or verb phrases such that it should describe the action of it. For eg: DeleteCustomer, AddUser, ChecIfCustomerExists and GetAllUsers.

If there are any actions that you are going to do with your API’s prefix your functions with get, update, delete and the function will give you more idea on it.
Functions
Whenever you write some functions, make sure it’s small and it does only a focused job. And also it should be reusable. The number of arguments passing to a function should not extend more than three parameters. If it’s extending you can wrap them up in a class and send that as an single parameter.
It you’re repeating the same logic several times, you can define a function for it and you can use that function wherever you want.
public string GetAllUsers(){
return $"We have around {NumberOfUsers()} in our project!";
}
public string DeleteAllUsers(){
return $" Are you sure you want to delete {NumberOfUsers()} users?";
}
public int NumberOfUsers(){
return 56;
}
In the above example, we have reused NumberOfUsers() function in two places. If you are working on an large enterprise application, you can create an helperClass and define all these common functions under those respective file such that you can use it all over your project.
Comments
Comments give you some understanding of the respective function or classes. It will improve your readability and understandability of your code. But sometimes it will go on a wrong way. Describing comments for each and every line of your code makes it messy.
The comments should be present only if, when your partner finds it difficult what you are exactly doing it here. But Always remember,
Clear Code with few comments is far better than cluttered and complex code with lot of comments
You cannot compromise your complex code with comments. You can compromise some of your clean code with your comments.
Format the Code
Formatting the code will improve the readability experience. The team should decide whether you go for tabs or instead to use spaces. A formatted code will give a better first impression on your code base. So we want people to be impressed by our orderliness, attention to detail and clarity of it.
“Code formatting is about communication, and communication is the professional developer’s first order of business.”
— ― ROBERT C. MARTIN — —
Don’t think just getting it working is the first order of business. Remember the logic which you write, may evolve and change, over the years but your readability of your code will never change. As the days pass by, most of the people will remember for your code style rather than what you code.
Error Handling
Error handling should be done properly in whatever the language you code. There are various chances of receiving an incorrect input, runtime exceptions etc. The proper way of handling these exceptions would be using try-catch blocks which is available in most of the programming languages.
And also we have to be sure that we should not be using more number of nested try-catch blocks. It should be simple and neat of using one try-catch which will handle most of the exceptions.

But one of the problem when the user tries to implement error handling, sometime the code become messy and hard to read. Basically the code should be clean and robust and the error should be handled in grace and style. That is a sign of great software craftsmen.
Conclusion
These are some of the parameters that you can take into consideration for writing clean code. I know it’s hard, but hereafter if you try to do that you will see an massive improvement to your code base. According to Robert Martin, writing clean code is called code-sense.
Some of us are born with it, but some should try to acquire it with their patience and perseverance. But once you acquire it, you would have the ability to transform the bad code into a good code at ease.
“It is not the language that makes programs appear simple. It is the programmer that make the language appear simple!”
Happy Coding!
Cheers! 🙂
Leave a Reply