Refactoring – Introduction and Its Techniques
Last Updated :
03 Apr, 2025
Refactoring or Code Refactoring is defined as systematic process of improving existing computer code, without adding new functionality or changing external behaviour of the code. It is intended to change the implementation, definition, structure of code without changing functionality of software. It improves extensibility, maintainability, and readability of software without changing what it actually does. Why should we refactor our code when it works fine? The goal of refactoring is not to add new functionality or remove an existing one. The main goal of refactoring is to make code easier to maintain in future and to fight technical debt. We do refactor because we understand that getting design right in first time is hard and also you get the following benefits from refactoring:
- Code size is often reduced
- Confusing code is restructured into simpler code
Both of the above benefits greatly improve maintainability which is required because requirements always keep changing. When do we refactor?
- Before you add new features, make sure your design and current code is “good” this will help the new code be easier to write.
- When you need to fix a bug
- When you do a peer review
- During a code review
How to identify code to refactor? Martin Fowler proposed using “code smells” to identify when and where to refactor. Code smells are bad things done in code, just like bad patterns in the code. Refactoring and Code smells are a few techniques that help us identify problems in design and implementation. It also helps us in applying known solutions to these problems. Refactoring Techniques : There are more than 70 refactoring techniques that exist. But we will discuss only a few, more common ones.
- Extract Method – When we have a code that can be grouped together. Example:
def student():
getgrades()
# details
name = input()
class = input()
- This could be refactored as:
def student():
getgrades()
getdetails()
def getdetails():
name = input()
class = input()
- Replace Temp with Query – When we are using a temporary variable to hold the result of an expression. Example :
SI = P * R * T / 100
if(SI > 100):
return SI
else:
return SI * 1.5
- This could be refactored as:
def calculate_SI(P, R, T):
return P * R * T / 100
SI = calculate_SI(P, R, T)
if SI > 100:
return SI
else:
return SI * 1.5
- Encapsulate Field – It involves providing methods that is used to read/write data rather than accessing it directly. Example :
class A:
variable
This could be refactored as:
class A:
self.variable
getvariable()
setvariable()
class A:
self.variable
getvariable()
setvariable()
- Inline Method – When we have a method body which is more obvious than the method itself. Example :
class PizzaDelivery:
def getgrades(self):
return 'A' if self.moretheneight() else B
def ismorethaneight(self):
return self.number > 8
- This could be refactored as:
class PizzaDelivery:
def getgrades(self):
return self.number > 8 ? A : B
- Move Method – When a function class is used by other class more than the class in which it exists.
Class A:
#...
abc()
Class B:
#...
- This could be refactored as:
Class A:
#...
Class B:
#...
abc()
- Replace Conditional with Polymorphism – When we have a conditional that performs various actions depending on object type or properties. Example :
=class Bird:
# ...
def getSpeed(self):
if self.type == EUROPEAN:
return self.getBaseSpeed()
elif self.type == AFRICAN:
return self.getBaseSpeed() - self.getLoadFactor() * self.numberOfCoconuts
elif self.type == NORWEGIAN_BLUE:
return 0 if self.isNailed else self.getBaseSpeed(self.voltage)
else:
raise Exception("Should be unreachable")
- This could be refactored as
class Bird:
# ...
def getSpeed(self):
pass
class European(Bird):
def getSpeed(self):
return self.getBaseSpeed()
class African(Bird):
def getSpeed(self):
return self.getBaseSpeed() - self.getLoadFactor() * self.numberOfCoconuts
class NorwegianBlue(Bird):
def getSpeed(self):
return 0 if self.isNailed else self.getBaseSpeed(self.voltage)
# Somewhere in client code
speed = bird.getSpeed()
Note :
- Refactoring improves the design of software.
- Refactoring makes software easier to understand.
- Refactoring helps us finding bugs in the program.
- Refactoring helps us program faster.
Similar Reads
7 Code Refactoring Techniques in Software Engineering
Being a developer how do you start working on a new project�? Firstly you gather some basic requirements and then based on the requirement you start implementing the feature one by one. As you progress with your project and learn more about it, you keep adding and changing the code in your codebase
9 min read
Defect Prevention Methods and Techniques
Defect Prevention is basically defined as a measure to ensure that defects being detected so far, should not appear or occur again. For facilitating communication simply among members of team, planning and devising defect prevention guidelines, etc., Coordinator is mainly responsible. Coordinator is
4 min read
Fault Reduction Techniques in Software Engineering
Fault reduction techniques are methods used in software engineering to reduce the number of errors or faults in a software system. Some common techniques include: Code Reviews: Code review is a process where code is evaluated by peers to identify potential problems and suggest improvements.Unit Test
4 min read
Techniques for reducing need of Software Maintenance
Software Maintenance is modification of a software product after delivery to correct faults, to improve performance or other attributes, or to adapt the product to a modified environment. Following are the three techniques the need of software maintenance as depicted in the following figure :There a
5 min read
Introduction to Software Engineering
Software is a program or set of programs containing instructions that provide the desired functionality. Engineering is the process of designing and building something that serves a particular purpose and finds a cost-effective solution to problems. Table of Content What is Software Engineering?Key
11 min read
Techniques to Identify Defects
Defects are one of the major causes of a decrease in improvement and quality of products. Therefore, identification of defects is very important to control and minimize its impact on system. Identification or detection of defects is not an easy process. Testers need to be very focused. Removing all
4 min read
Object Modeling Technique (OMT) - Software Engineering
Object Modeling Technique (OMT) is a real-world modeling approach that is easy to draw and use. This article focuses on discussing OMT in detail. What is the Object Modelling Technique?Object Modeling Technique (OMT) is a real-world-based modeling approach for software modeling and designing. It was
3 min read
Refactoring in Agile
Refactoring is the practice of continuously improving the design of existing code, without changing the fundamental behavior. In Agile, teams maintain and enhance their code on an incremental basis from Sprint to Sprint. If code is not refactored in an Agile project, it will result in poor code qual
12 min read
An Introduction to Software Development Design Principles
This introductory article talks about the things a software developer must keep in mind while developing any software. It also talks about what are some of key principles that one should consider while writing code on a daily basis. Overview: Letâs begin topic with a question - In context of softwar
10 min read
Introduction of Software Design Process - Set 2
Software Design is the process of transforming user requirements into a suitable form, which helps the programmer in software coding and implementation. During the software design phase, the design document is produced, based on the customer requirements as documented in the SRS document. Hence, thi
6 min read