0% found this document useful (0 votes)
78 views

How To Write Clean Code? Follow These Best Practices

The document provides 12 best practices for writing clean code, including giving meaningful names to variables and methods, writing smaller and focused methods, reducing duplicate code, adding comments for complex code, using object-oriented principles like single responsibility and avoiding hard coding values. It emphasizes practices like writing test cases, logging for debugging, reducing method parameters and reusing existing code where possible to improve code quality and readability.

Uploaded by

kishorenayark
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views

How To Write Clean Code? Follow These Best Practices

The document provides 12 best practices for writing clean code, including giving meaningful names to variables and methods, writing smaller and focused methods, reducing duplicate code, adding comments for complex code, using object-oriented principles like single responsibility and avoiding hard coding values. It emphasizes practices like writing test cases, logging for debugging, reducing method parameters and reusing existing code where possible to improve code quality and readability.

Uploaded by

kishorenayark
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

14/02/2021 How to Write Clean Code?

Follow These Best Practices | by Vikram Gupta | Javarevisited | Medium

This is your last free member-only story this month. Sign up for Medium and get an extra one

How to Write Clean Code? Follow These Best


Practices
Know the best practices for writing clean code.

Vikram Gupta
Sep 9, 2020 · 6 min read

Photo by Safar Safarov on Unsplash

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.

1. What’s there in the name :


A developer should always try to give a meaningful name to the variables, methods,
and classes. It becomes too easy to understand what the method or class or variable is
about when someone reads your code while reviewing it or debugging it. Giving name
like a,b,c to the variable do not intent any meaning and becomes less relevant while
debugging the code.

1.Always start the name of a class with upper case .Eg.

public class Employee {

here Employee is the class name and other developers can easily understand that this
class deals with employee-related stuff.

2. Always start a variable name with lower case .Eg.

private int salary;

here the salary tells about the salary of the employee.

3.Always start method name with lower case and do not include And and Or
words in method name .Eg.

public int calculateSalary(int noOfDaysWorked , int baseSalary)

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.

public final int RETIREMENT_AGE = 58;

Note : do not use special symbols while writing variables ,methods , constants or classes
names .

2. Smaller the methods better to manage and debug:


Writing many things into a single function leads to bad design implementation. Also,
it’s not advised to write too big methods as it’s hard to debug if so many things are done
in a single function. Hence write your function between 10–15 lines focusing onto a
single thing that the method does.

3. Be smart to reduce lines of code :


Always try to think before even you start writing any piece of code. Also, think about if
there is any possibility to reduce no of lines which you want to write. Generally,
developers check a boolean condition in if block and then return the value. This isn’t
the correct way to write the code. Let me explain with the piece of code for better
understanding.

boolean isContractor(Employee employee)

{
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

}
}

the above code can be reduced to :

boolean isContractor(Employee employee)

{
return employee.isCtr;

Now you can see, we reduced the code to a single line instead of six.

4. Are comments necessary?


Yeah, comments do add meaning to complex methods or algorithms but they are not
necessary for your code. If you write a piece of code and you think that any developer
can understand by just reading the code then there is no need of adding comments to
self-explanatory code. But do remember to add comments where you think you have
explained or written complex logic or algorithm so that later any developer can
understand while debugging the code.

5. Single task in one method


Adding many tasks/things into a single method is not a good design. So developers
should always try to maintain different functions for the different tasks to perform. Eg.

public void addUser(String userName , String password)

//perform user addition operation to DB

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

6.Structure your code


Always write functions in the order they are called. Also, write all variables to the top of
all methods, so it becomes easier to locate. If some variables are used only in a single
function then try to make those variables locale variables to that function.

7.Reviewers don’t like duplicate code


Whenever you start writing code, you can give a look at related functions and classes in
the codebase to check whether if similar functionality already exists or not. If there is
some method which does the same task as you want then reuse it.

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.

8.Check your code functionality by writing Test cases


Once you are done with writing your methods and related classes then go ahead and
identify test scenarios and corner cases for your code functionality.

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.

9.Dododon't hard code


Hard coding is a bad idea which I generally see many developers do while coding. If
you are hard coding some strings then it becomes difficult to test the functionality

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

implemented by you. It may introduce bugs to your code if something is changed


related to your code.

private static String serverIpAddress =”1.2.3.4”;

Never do this mistake. You can write a function that will fetch the IP address and then
you can use it in your code.

10. Logging makes code debugging easier


While writing code every developer should try to log necessary points about the
functionality to get the idea about the flow or important info. It makes it easier for
another developer to read the logs and check the code flow and debug and fix the
issue.

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.

11.Reduce method parameters


Passing many parameters to a single function is not good practice. Instead, you should
try to create an object and then pass that object as a parameter to function. This
improves code readability and hence improves code quality.

Eg.

public void addUser(String name , String gender , String age ,String


userName , String password)

//perform user addition operation to DB


https://medium.com/javarevisited/how-to-write-clean-code-follow-these-best-practices-for-writing-clean-code-a17ccecbac9d 6/9
14/02/2021 How to Write Clean Code? Follow These Best Practices | by Vikram Gupta | Javarevisited | Medium

The above code can be reduced by passing the User object to function as a parameter.

public void addUser(User user)

//perform user addition operation to DB

}
//where user will be a class
class User{

private String name ;


private String gender ;
private int age ;
private String userName;
private String password;

//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.

12. Code reuse


I have already talked about duplicate code which you should avoid. Basically, you
should always look for your functionality before you write them into code base or
related code area and if you find them use it, and if not then go ahead with your own
method and write them.

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.

You can check my other articles on Java programming language…

nal, nally and nalize in Java


Hey, I’m going to explain the three most frequently used java
keywords in this article. These are final, finally and…
medium.com

What is Inheritance and Composition in Java? How are


they di erent?
I’m going to cover the two most widely used object-oriented
programming features of Java. One is Inheritance and the other…
medium.com

Confused with Java Singleton design pattern? Have a look


at this article.
Hey, I know many developers many times get confused with
Singleton design pattern. Actually it’s not their issue as…
medium.com

Best Practices Code Review Java Programming Coding

About Help Legal


https://medium.com/javarevisited/how-to-write-clean-code-follow-these-best-practices-for-writing-clean-code-a17ccecbac9d 8/9
14/02/2021 How to Write Clean Code? Follow These Best Practices | by Vikram Gupta | Javarevisited | Medium

Get the Medium app

https://medium.com/javarevisited/how-to-write-clean-code-follow-these-best-practices-for-writing-clean-code-a17ccecbac9d 9/9

You might also like