code refactoring: short guide
February 4, 2020

Software Development

Code Refactoring: What It Is and When You Need It

By Olga Matuts

Code refactoring is the process when developers edit and clear code without changing its functions. Code refactoring makes code more productive and maintainable, increases readability, improves testing and QA. This process helps to avoid bugs in the future and reduce costs by editing code in advance.

While experienced developers pay attention to the clarity of code, they often have to upgrade or deal with a functioning system built by another or even several other teams. In this case, refactoring is very useful. It helps to prevent the code rot and programming discrepancies, resulting from wrong classifications, code duplication, different styles of code writing by several developers.

When do developers need to use code refactoring?

As a rule, refactoring is used before updating or editing new features into the code. This process of cleaning code increases the quality of the product and facilitates its writing.

Also, it is a good practice to apply refactoring shortly after the product rollout. It means that developers have time to review their code without delays and then they get down to a new task.

Code refactoring basic types 

The best way of refactoring is to do it piece by piece before modifications. There are a lot of refactoring code approaches described on the refactoring.com. They differ in size, required time and the number of team members, so developers can choose exactly what they need. Let’s consider some of them below.

Red-Green-Refactoring

This is one of the most well-known methods, an example of Agile testing development, and it includes three parts:

Red: define the goal of development (write failing tests);

Green: develop the product to pass at least primary testing (write the minimum required code);

Refactoring: make enhancements (optimize code without adding new functions).

Abstraction-aware Refactoring

It is used when developers require to do a large part of refactoring. Abstraction is aimed at removing duplicates in coding; it consists of class inheritances, hierarchy, and extraction. An example of class inheritances is the Pull-Up/Push-Down method. It includes two opposite types of refactoring: Pull-Up brings parts of code into the superclass, and after that Push-Down takes them to subclasses. This process helps to avoid code duplication.

Composing technique

This method is about reducing the amount of code duplication by its optimizing. It is reached by various processes as extraction and inline methods.  

Extraction allows developers to divide the code into fragments and take the needed part into a distinct method. Next, this fragment is replaced with a call to this method. In this way, the extracted part doesn’t affect the work of the code. Just like method, extraction can be about class, interface, and local variables.

Inline refactoring is the way to simplify code by reducing useless methods. For this, developers find calls to the method and replaced them with the content from this method. After that, the method is removed.

Simplifying Methods

With time, code becomes more complicated and modified, so it requires simplification. Developers can make it by, first, using the consolidation of conditional fragments and expressions and then by replacing the conditional ones with polymorphism. Also, simplification includes such aspects as adding, removing, and introducing new parameters along with replacing parameters with explicit methods and method calls.

Moving Features Between Objects

This approach allows for creating new classes and spreading functionality between the old and the new ones. For example, if there is a lot of burden in one class, developers can transfer a part of functionality into another class. The other way, if the class doesn’t have enough work, developers can put their functions into a different one and remove it.

Preparatory refactoring

Developers use this kind of refactoring when they add a new function. In this case, this process is probably a part of software updating rather than refactoring; developers making changes as soon as they notice a need for it. The book “Refactoring: Improving the Design of Existing Code” by Martin Fowler is dedicated to this topic. Also, there is an interesting analogy of Jessica Kerr, software developer and blogger. She compares preparatory refactoring with making the shortcut: when people show you the way, you should check the map because the shortest way is not always obvious.

When is code refactoring unnecessary?

In several cases, the usage of code refactoring is excessive. When the whole web application requires updating, developers should rewrite all code; it is not about refactoring, cleaning, and simplifying.

Developers can also defer code refactoring when there are time limitations, and the product is at the launch stage. At this moment, refactoring can slow down the launch with additional testing and coding.  

What can simplify code refactoring?

There are several tips that can make code refactoring more convenient for developers. For example, teams can follow the Agile methodology and use testing facilitates. The quality of an application depends on how well developers establish the refactoring process, single out parts of work, and test the code on time.

Let’s see what practices can help developers to work with code refactoring.

Refactoring before adding features

Following this rule helps the developers’ team to reduce future technical debt even if it takes more time than expected. This way, developers should check a part of code before they add something new. Technical debt is the scope of problems such as code updating and using another approach to programming. 

Planning refactoring

Developers should schedule refactoring and its goals for reaching good results. Timelines depend on tasks: changes in variable names, code optimization, readability enhancement or a full clean-up. If refactoring is a scheduled process, there can be no hurry or missed deadlines.

Frequent testing

Testing code during refactoring is an essential condition for success. Developers should prepare relevant tests before the process begins and track all parts of code refactoring. It helps to prevent problems with functionality, code, and bugs.

Collaboration with QA team

Any changes in coding can affect the testing results, even if it is a simple clean-up process. They can break the results of an older test and cause bugs. So the QA team should create new tests and check the results of refactoring every time something changes to prevent performance issues. They can use both in-depth and regressive testing for tracking this process.

Process prevalence

It is important to understand that code refactoring is an ongoing process, and it can’t be finished at all. The code eventually would become irrelevant and the system might need refactoring again and again. This process can be compared with weekly cleaning, something permanent and required.

Automating code refactoring

Developers can simplify the process of code refactoring with automatization. There are a lot of tools for refactoring automatization, for example, Eclipse, Intellij IDEA or Microsoft Visual Studio, some of the most popular integrated development environments for Java and C# and this list is constantly updated.

Best books on refactoring

We prepared the top 5 books that will help you to improve your knowledge about refactoring.

#1. Refactoring: Improving the Design of the Existing code by Martin Fowler (2nd Edition, 2018)

This book consists of actual JavaScript code and functional examples to demonstrate refactoring and answers what refactoring is, why developers should refactor, and what is the best way to do it.

#2. Refactoring Application A Complete Guide by Gerardus Blokdyk (2019) This detailed guide is grounded on the real successful projects and tells about useful tools for self-assessment and productive refactoring.

#3. Refactoring JavaScript: Turning Bad Code Into Good Code by Evan Burchard (2017) This practical guide will be useful to write better JavaScript code and effectively apply principles of object-oriented and functional programming during refactoring.

#4. Clean Code in Python: Refactor your legacy code base by Mariano Anaya (2018) The book will help you to become proficient in code refactoring and designing clean and readable Python code based on SOLID principles with the usage of the most efficient refactoring techniques.

#5. Monolith to Microservices: Refactoring Approaches compared: Transforming Applications to could-ready Software Architectures by Jonas Fritzsch (2018) In this book, the reader can find a comparison of the 10 most popular code refactoring approaches and get the answer to the questions: How to find the right service granularity to leverage the advantages of the microservice pattern? And how to choose the correct refactoring technique?

Perception of refactoring as an integral part of coding results in good-working software, increases the efficiency and quality of the product, and reduces technical debt.

We use cookies to personalize our service and to improve your experience on the website and its subdomains. We also use this information for analytics.

Got it!