How To Write Clean Code? Follow These Best Practices
How To Write Clean Code? Follow These Best Practices
This is your last free member-only story this month. Sign up for Medium and get an extra one
Vikram Gupta
Sep 9, 2020 · 6 min read
Hey programmers, this article is not about some java related feature or concept but
https://medium.com/javarevisited/how-to-write-clean-code-follow-these-best-practices-for-writing-clean-code-a17ccecbac9d 1/9
14/02/2021 How to Write Clean Code? Follow These Best Practices | by Vikram Gupta | Javarevisited | Medium
more than that. Basically, I’m going to explain what all mistakes a java developer does
while writing the code and hence how you can minimize it and you don’t have to
refactor your code. So this article is about the BEST PRACTICES a developer should
always follow.
here Employee is the class name and other developers can easily understand that this
class deals with employee-related stuff.
3.Always start method name with lower case and do not include And and Or
words in method name .Eg.
https://medium.com/javarevisited/how-to-write-clean-code-follow-these-best-practices-for-writing-clean-code-a17ccecbac9d 2/9
14/02/2021 How to Write Clean Code? Follow These Best Practices | by Vikram Gupta | Javarevisited | Medium
4. Write constants in upper case and separate them with underscore .Eg.
Note : do not use special symbols while writing variables ,methods , constants or classes
names .
{
if(employee.isCtr){
return true;
else {
return false;
https://medium.com/javarevisited/how-to-write-clean-code-follow-these-best-practices-for-writing-clean-code-a17ccecbac9d 3/9
14/02/2021 How to Write Clean Code? Follow These Best Practices | by Vikram Gupta | Javarevisited | Medium
}
}
{
return employee.isCtr;
Now you can see, we reduced the code to a single line instead of six.
https://medium.com/javarevisited/how-to-write-clean-code-follow-these-best-practices-for-writing-clean-code-a17ccecbac9d 4/9
14/02/2021 How to Write Clean Code? Follow These Best Practices | by Vikram Gupta | Javarevisited | Medium
If there is a method that has similar code as you want to write then write a single
function that will perform the task of both functions, one which already there in the
codebase and the other which you wanted to write. This is the best practice to remove
duplicate code.
Now you can include test cases for all these identified scenarios and check whether
your code passes these test cases or not. While doing coding if you had missed some
corner cases then try to include them.
https://medium.com/javarevisited/how-to-write-clean-code-follow-these-best-practices-for-writing-clean-code-a17ccecbac9d 5/9
14/02/2021 How to Write Clean Code? Follow These Best Practices | by Vikram Gupta | Javarevisited | Medium
Never do this mistake. You can write a function that will fetch the IP address and then
you can use it in your code.
Code without logs or improper logs do not help other developers to understand the
code easily and the developer himself has to put extra effort to debug the issue.
Also, note that the developer should not log each and every statement results as
excessive logging may reduce the performance of the application.
Eg.
The above code can be reduced by passing the User object to function as a parameter.
}
//where user will be a class
class User{
//here we can have constructors and getters and setters for above
instance variables
}
You can see, how easy it became to read the argument passed to function.
13.END NOTE
I have written many best practices that every developer should follow while writing
code in any language. These best practices improve your code writing skills and
https://medium.com/javarevisited/how-to-write-clean-code-follow-these-best-practices-for-writing-clean-code-a17ccecbac9d 7/9
14/02/2021 How to Write Clean Code? Follow These Best Practices | by Vikram Gupta | Javarevisited | Medium
definitely will help in the near future. I hope you have taken note of these important
points and you will implement these in your code writing practices.
You can use of SonarLint plugin for Eclipse and IntelliJ IDE for improving your code
quality .This plug in suggest you what are best practices while writing code .
If you find this article useful then you may Follow me for such useful articles.
https://medium.com/javarevisited/how-to-write-clean-code-follow-these-best-practices-for-writing-clean-code-a17ccecbac9d 9/9