How to refactor code in Java?

ows and othA few weeks ago, my colleagues and I were extending the older code in Java with additional functionality. It seem 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 requir for modifications and the add value that the changes will bring us. More detail 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 inspir by the teams we refactor.

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

Problem The 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 parameteriz method for the code, in case of duplication of code in relat 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
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-prepar templates that we can modify (Eclipse). Unification of naming.
See point no. 1.
Constants defin directly in the code Replacement of constants defin in the code with constants defin in the class or replacement with enumeration.
See point no. 2.
In the following points, I would show the most common refactoring methods that help us make the 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 avoid when initially writing the code.

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

 

The mixing of different language variable names, inappropriate initialization of variables, visibility of variables outside the class, direct access to variables outside of get/set methods and many others are visible.
When it comes to the language, it is indonesia phone number data good to agree in advance which one will be us throughout the project. In this case it was Slovak.

phone number data

Similar rules are us for method names as for attributes and variables. The name should be short, concise (more details can be found in the description of the method), use a uniform language and naming methodology. In seo practices to wnhance your customer experience English, these are, for example, the prefixes get/set/is/has. If the method name is too long, it is possible that the method covers different functionality and is an indicator of the division of the method into several parts.
When expanding the Attachment object, we replac the column names in the database with global constants and similarly defin the attachment versions.
The version labels could also be defin by the bz lists enumerator, but since they were only us within this class, we left them as constants. Another reason was that there were only two versions of the attachment, namely 0 and 1.

 

In further iterations of class modifications, changes were made to PrepareStatements, SQL code generation, and much more. The file type has been convert to enumeration:

 

3. Using an interface instead of a specific implementation
In the previous example implementation of the getAttachments method, the return value is of type ArrayList. Defining a specific type of return value prevents the code from being easily chang and modifi.
In the future, if we ne to change to a different type of list, for example synchroniz, or a completely different implementation of the list, we must then change the type throughout our code.
The solution is to define the type of the return value by its interface. In our case List-om.

 

 

Scroll to Top