Code Refactoring and its Techniques in Agile

Code Refactoring and its Techniques in Agile

Definition of Code Refactoring

Refactoring simply means restructuring a piece of code to improve the operation without altering its functionality. But, what is refactoring in Agile? In the context of Agile, refactoring means missing or not missing an iteration deadline. 

So what is code refactoring in Agile? It is a practice where the software’s source code is altered without its inherent behavior. In simpler terms, it is a practice that solely focuses on getting the code into a more desirable state for the development team. It is important to note that refactoring code is not:

  1. Fixing any bugs
  2. Improve or change the aspects of source code such as the software’s interface
  3. Rewriting code

One might ask, isn’t it better just to go ahead with the code without any refactoring which takes extra effort and time. To answer it simply, no. One can argue that it is the single most important practice for having success with Agile adoption. Every time there is a change in the code without refactoring, the code got frustrated and costs a lot of time. 

As the project furthers, the code gets more complicated, harder to maintain, harder to understand, and harder to make changes. Refactoring the code helps you to avoid all these problems. As the software grows in size and complexity and evolves, refactoring allows one to modify it constantly as an answer to the inevitable change.

Before jumping on to the refactoring methods in Agile, let’s see the  uses of the refactoring process:

  1. Code refactoring is mainly used to “uncomplicated” huge codes and it is always advisable to carry out the code refactoring in a series of small changes. Each small change should make the existing code slightly better leaving the function of the program or software in working condition.
  2. When you are refactoring, there is a potential to introduce some bugs into the program if you do not run some kind of tests. Therefore, it is a must to use TDD and CI to run tests after every small step of refactoring the code.
  3. When refactoring, the code must become cleaner. It’s a basic tip but very effective.
  4. Refactoring can often be confused with adding new functionality. The processes look similar but, it is of utmost importance that creating a new function and refactoring is separated. Adding new functions can be done at the development stage but, the refactoring stage must be only about cleaning the codes without changing any inherent properties or behavior of the program.


There are a lot of benefits of code refactoring and some of them are listed below.

  1. You get to see the whole picture
    It is better to break a single method into several less complex, easier steps. Code refactoring helps you do this exactly. How does it help? When there are several smaller methods, it’s easy to have an eye on everything that’s going on and it becomes easier to manage the program.
  2. Make it easy for your team
    Think in the long term and make the refactored code such that your co-workers and peers can easily read the codes.
  3. Maintaining the program becomes easier
    Making changes, and update is a continuous process and there is no escaping it and it should be welcomed. When the codes of the program are not maintained, made easier, and not built on a proper foundation, developers are not so keen on making changes. With the help of code refactoring, it becomes easier to clean up the code, organize it and make it easier for the developers. Codes built like this are more likely to adapt to changes in the future.
  4. Efficiency
    Refactoring might be seen as a waste of money and time but, it’s all worth it. There is a significant reduction in problems, waste-of-time at the development stage and every process thereafter becomes highly efficient.
  5. Reduces complexity
    Reducing the complexity of the program’s code is the main goal of code refactoring. Refactoring is perfect for this since using it makes the program cleaner and easy to understand by peers and the development team.


Code Refactoring Techniques

Several refactoring techniques in Agile helps in adopting the correct way of using code refactoring. The following techniques are the most common and useful ones:

1. Red-green refactoring

This technique is the most popular one and forms the basis of all the other techniques. This technique is widely used in the Agile software development process and it is based on Test Driven Development (TDD). It follows a “test-first” approach for design and implementation. This technique incorporates refactoring into a test-driven development cycle. The process starts with a failing “red” test than writing the simplest code possible to pass the test “green”. Finally, the code is improved and enhanced keeping the code test “green”.

The technique is easy to understand and refactoring blends seamlessly into the overall development process. This is the reason, why this technique is so popular and works towards making the overall source code cleaner and easier to manage.

There are two separate parts to this: writing the code to add a new function to the program and improving the code of this function. It is important to understand that both of these parts shouldn’t be done at the same time.


2. Preparatory refactoring

A developer can make few changes to the code base so that building the next set of features becomes a little easier. This technique of code refactoring is called preparatory refactoring as explained by author Mark Fowler in his book Refactoring: Improving the Design of Existing Code

This is done when a developer notices that there is a need for refactoring in the code of the program. Although this sounds exactly like the Red-green technique, it is slightly different in the execution. In this technique, the refactoring process is a part of the overall software update but, not a separate refactoring process.

By noticing that the code needs to be updated, the developer reduces the problem or technical difficulties that might arise later in the project.

If explained via illustration, Jessica Kerr explains this technique. 

“It’s like I want to go 100 miles east but instead of just traipsing through the woods, I’m going to drive 20 miles north to the highway, and then I’m going to go 100 miles east at three times the speed I could have if I just went straight there. When people are pushing you to just go straight there, sometimes you need to say, ‘Wait, I need to check the map and find the quickest route.’ The preparatory refactoring does that for me.”


3. Refactoring by Abstraction

Refactoring by abstraction is usually done when the amount of codes to be refactored is huge. The abstraction process involves hierarchy, class inheritances, and extraction. The main goal of the abstraction is to reduce duplicated software codes.

Pull-Up/Push-down is one example of the abstraction process. These are two opposite forms of refactoring involving classes. The Pull-up method pulls the code parts into a superclass to eliminate code duplication. The Push-down method pushes down the code parts from the superclass to the subclasses.

Few other examples include extract subclass, extract interface, extract superclass, collapse hierarchy, replace delegation with inheritance, replace inheritance with delegation, etc. In this technique, the basic idea is to build an abstraction layer that wraps the part of the program that has to be refactored and the counterpart that will eventually replace it.


4. Composing method of Refactoring

The goal of this technique is very similar to the abstraction method where it involves simplifying the code to reduce code duplications. This can be done through multiple processes, such as extraction and inline methods.

Extraction involves breaking down the code into smaller bits to find and “extract” the fragmentation. Then, the fragmented code is moved to a separate method. Extraction can have class, interface, and local variables too.

The inline method is a way to reduce the number of unnecessary methods by simplifying the code with refactoring.


5. Simplifying method Refactoring

The code and conditionals in the code tend to get more complicated in their logic over a while. It makes sense to go in and simplify a lot of this. This can be done in a variety of methods such as consolidation of conditional fragments and expressions and replacing the conditional with polymorphism. The simplifying method involves tweaking the interaction between classes.

There are multiple methods you can use for preparatory refactoring. The one that works best will depend on the solution, time frame and the number of people there are to assist the team. One can find many more refactoring techniques in Agile in the book Refactoring: Improving the Design of Existing Code by Mark Fowler.

Code refactoring is just like cleaning and organizing the kitchen so that everything is easy to find and the environment is stress-free and makes the whole process a lot more efficient.