How to refactor code in Java?

g things are mentio
A few weeks ago, my colleagues and I were extending the older code in Java with additional functionality. It seemed like a simple task…

But after the initial viewing, it was clear that further expansion without refactoring, improving readability and optimizing the code would not be possible. Fortunately, there were unit tests, so we could start streamlining the existing code.

When refactoring, it is always important to maintain a balance between the time required for modifications and the added value that the changes will bring us. More detailed adjustments and improvements may come in handy for major changes. The modifications will improve our debugging capabilities, help shorten the implementation time, and simplify the modifications of unit tests. Sample codes are inspired by the teams we refactored.

When to refactor code?
Indicators that the code needs to be refactored are situations when we stop orienting ourselves in the code. I present the most frequent problems and their solutions in the table.

Problem Solution

Large number of rows in the class (more than 1000). Suspicion that the class merges functionality that shouldn’t even be together. For example, merging DAO objects and DTO objects.
Large number of lines in method (more than 150) Dividing the method into smaller parts/multiple methods.
Public class attributes Change to private and create get/set methods with attributes.
Code duplication Creation of a separate parameterized method for the code, in case of duplication of code in related classes, move the common code to a higher class (ancestor of the given classes) or to their abstract class.
See point no. 4.
Programming against specific classes, the return type is not a class interface Programming against interfaces with generic type checking. See point no. 3.
Exception handling (using printStackTrace, System.out.println, Throwable). Use of standard login e.g. Log4J.
Use concrete exceptions instead of Throwable.
Exceptions and errors should be handled separately.
Generally, a large number of “if” and “switch” statements Using inheritance and class specialization.
Absence/insufficient description of methods and classes, many comments in the code Completion and automatic generation of descriptions.
Reduce comments in the code and leave them only for complex business processes.
Too many comments can signal a bad or complicated implementation.
See point no. 5.
Java warnings – warns of possible problem areas Fix problem spots.
Different code formats, naming of constants, variables,… Unify the code format according to pre-prepared templates that we can modify (Eclipse). Unification of naming.
See point no. 1.
Constants defined directly in the taiwan phone number data code Replacement of constants defined in the code with constants defined

The class or replacement with enumeration

phone number data
See point no. 2.
In the following points, I would show the most common refactoring methods that helped us make the what should I post on my website? existing code clearer and more efficient.

1. Naming of variables, constants, methods and classes
In the following example I will show inappropriate defining and naming. These errors should be avoided when bw lists initially writing the code.

After looking at the code, it is obvious that quick and easy modifications are needed for further.

Scroll to Top