Agile Software Development
Topic: Code Refactoring
Dr. Nikhil Govil
Associate Professor, Program Coordinator – B.Tech. (CSE), Dept. of
Topics to be covered
• Code Refactoring
• Why to refactor a code?
• Process of code refactoring
• Benefits of code refactoring
• Code refactoring techniques
Code Refactoring
• Code refactoring is the process of restructuring existing computer code
without changing its external behavior.
• While refactoring new code is not developed rather improvement of the
existing code is focussed.
• Refactoring improves non functional attributes of the software.
• Advantages include improved code readability and reduced complexity
to improve source code maintainability, and create a more expressive
internal architecture or object model to improve extensibility.
Why to Refactor a Code?
There may be some stereo-typical situations where program code
should be improved. Such situations are known as Code Smells.
Some of them are:
• Duplicate Code
• Long Methods
• Switch Case Statements
• Data Clumping &
• Speculative Generality.
Process of Code Refactoring
Benefits of Code Refactoring
1. Maintainability
It is easier to fix bugs because the source code is easy to read and
the intent of its author is easy to grasp.
2. Extensibility
It is easier to extend the capabilities of the application if it uses
recognizable design patterns, and it provides some flexibility
where none before may have existed.
Code Refactoring Techniques
Techniques that allow for more abstraction
• Encapsulate Field: force code to access the field with getter and setter
methods
• Generalize Type: create more general types to allow for more code
sharing
• Replace type checking code: with State/Strategy
• Replace conditional with polymorphism
Code Refactoring Techniques
Techniques for breaking code apart into more logical pieces
• Componentization breaks code down into reusable semantic units that
present clear, well defined, simple to use interfaces.
• Extract Class moves part of the code from an existing class into a new
class.
• Extract Method, to turn part of a larger method into a new method. By
breaking down code in smaller pieces, it is more easily
understandable.
Code Refactoring Techniques
Techniques for improving names and location of code
• Move Method or Move Field move to a more appropriate Class or
source file.
• Rename Method or Rename Field changing the name into a new one that
better reveals its purpose.
• Pull Up in OOP, move to a super class.
• Push Down in OOP, move to a sub class.
References
• Ken Schawber & Mike Beedle, Agile Software Development
with Scrum, Pearson, 2008.
• Robert C. Martin, Agile Software Development, Principles,
Patterns and Practices, Prentice Hall,2002.
• Lisa Crispin & Janet Gregory, Agile Testing: A Practical Guide
for Testers and Agile Teams, Addison Wesley, 2008.
• Alistair Cockburn, Agile Software Development: The
Cooperative Game, Addison Wesley, 2006.