Code refactoring is about improving existing code. It is a software transformation that improves the internal software structure while preserving the external software behavior and existing functionality. The purpose is to improve internal quality attributes of the software: to improve code readability, to simplify code structure, to change code to adhere to a given programming paradigm, to improve maintainability, to improve performance, and to improve extensibility.
It reduces software decay (aging), software complexity and software maintenance costs. It increases software understandability (for instance, by introducing design patterns) and software productivity.
The following are just a few examples of typical code refactoring:
Problem: Duplicate code
Solution: Extract method; pull up variable; form template method; substitute algorithm
Problem: Long Method (The longer the method the harder it is to see what it’s doing.)
Solution: Extract method (split a method into two); replace temp with query; introduce parameter object; preserve whole object; replace method with method object
Problem: Large Class (The larger the class the harder it is to see what it’s doing.)
Solution: Extract class (split a class into two); extract subclass
Problem: Lazy Class (Each class you create costs money to maintain and understand. A class that isn't doing enough to pay for itself should be eliminated.)
Solution: Collapse hierarchy (when you have subclasses that aren’t doing enough); inline class (when you have a class that does not do very much, move all its features into another class and delete it)
Problem: Feature Envy (Often a Method that seems more interested in a class other than the one it's actually in.)
Solution: Move Method
We can refactor code written in a wide range of languages.
|