Pros And Cons Of Code Refactoring


Code Refactoring is a way of restructuring and optimizing existing code without changing its behavior. It is a way to improve the code quality. According to Martin Fowler's definition - "Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior."
Code Refactoring should not be done just any time. As per Martin Fowler - "You shouldn't refactor if a deadline is near. That time in project is better suited to flush out bugs instead of improving design(Refactoring). Do the refactoring omitted this time directly after deadline is over."

When Code Should be Refactored?

Code should be refactored on the below points,
  • Chances of Enhancement are high
    If modules have chances to add new features or functionalities then make sure design and current code is good and following Open Close Principle
  • Code Smell is Detected
    Sometimes bad patterns like tight coupling, duplicate code, long methods, large classes, etc. are detected in the code;  the code should be refactored in this case.
  • Bug Fixing
    Codes are written badly in some cases, and so many bugs are raised. In this case, fixing of bugs take too much effort. So, the root cause of bugs can be code smell. So, before fixing bugs code should be refactored.
  • Peer Review
    Peer review is an important part of code refactoring. If the peer-reviewer finds some code smell then code should be refactored during peer review.

When Code Should not be Refactored?

Code should not be refactored on below cases,
  • Delivery Deadline is near and new development is planned.
  • The cost of refactoring is higher than rewriting the code from scratch.
  • Don't refactor the code if you don't have the time to test the refactored code before release. It can introduce bugs. 
  • Don't do delayed refactoring because it contains a big mess and makes it very difficult to refactor. Do early refactoring.
  • Stable code should not be refactored.

What are the Goals of Code Refactoring?

Code Refactoring has the below goals,
  • Maintainability
    The main goal of code refactoring is to make it easy to enhance and maintain in the future. It should not violate Open Close Principle.
  • Removing Bad Smell
    Bad code smell motivates the refactoring of code. It prevents many future defects. Code Size is reduced. Confused coding is properly restructured.

What are the Chances of Danger in Code Refactoring?

Below dangers can happen after or during the code refactoring,
  • It is expensive and risky in the view of management.
  • It may introduce bugs.
  • Delivery schedule is very tight.
  • Management doesn't care about maintainability and extension of code base.

What are the Some Common Code Smells?

Below listed points are common code smells and must be refactored:
  • Duplicate Code
  • Long Method/Large Class
  • Long Parameter list in method
  • Refused Bequest
  • Lack of Design
  • Data Clump
  • Primitive Obsession
  • Feature Envy 
  • Divergent Change

What are the Roles of Design in Code Refactoring?

Design has the below roles in Code Refactoring,
  • Refactoring improves the Design of Existing Code - Martin Fowler.
  • In Traditional Engineering, design puts 15% of total effort but design puts 90% effort into Software Engineering.
  • Design helps to make the code good and maintainable.
  • Design makes code easily understandable because code is loosely coupled, restructured, and duplicate code is eliminated.


Code Refactoring is an important exercise to remove code smell. It helps to find bugs, makes programs run faster, it's easier to understand the code, improves the design of software, etc. Code smell slows down the development and is prone to more defects. An adequate set of unit tests and a supportive environment should be there for code refactoring.