Refactoring
Refactoring is the systematic modification of a software system's internal structure without changing its external behaviour, aimed at improving nonfunctional attributes of the software.
Goal
To enhance the maintainability, readability, and performance of the codebase, facilitating easier updates, faster bug fixes, and more efficient feature development.
Context
Code tends towards complexity. As we layer on new features, fix bugs, and make changes, the codebase can become harder to understand and maintain. Refactoring is a critical practice to streamline the design of the codebase and keep the codebase clean, organised, and efficient over time.
Techniques
Technique | Description | Methods |
---|---|---|
Composing Methods | Techniques for simplifying methods, making them smaller, and more focused. | Extract Method, Inline Method, Inline Temp, Replace Temp with Query, Introduce Explaining Variable |
Moving Features between Objects | Approaches for reassigning responsibilities between classes to improve coherence or reduce dependencies. | Move Method, Move Field, Extract Class, Inline Class, Hide Delegate |
organising Data | Refactoring that improves how data is managed and accessed within an application. | Encapsulate Field, Encapsulate Collection, Replace Data Value with Object, Change Value to Reference |
Simplifying Conditional Expressions | Techniques to make conditional logic clearer and easier to manage. | Decompose Conditional, Consolidate Conditional Expression, Replace Nested Conditional with Guard Clauses |
Making Method Calls Simpler | Methods aimed at simplifying how methods and functions are called and used. | Rename Method, Add Parameter, Remove Parameter, Separate Query from Modifier |
Dealing with Generalisation | Techniques for refining class hierarchies and object inheritance to make them more logical and easier to understand. | Pull Up Method, Push Down Method, Extract Subclass, Extract Superclass, Replace Inheritance with Delegation |
Inputs
Artifact | Description |
---|---|
Codebase | The existing source code that is subject to refactoring. |
Outputs
Artifact | Description | Benefits |
---|---|---|
Refactored Code | The updated codebase after refactoring has been applied. | Improves code quality and reduces the risk of bugs or errors. |
Anti-patterns
- Random Refactoring: Refactoring without clear goals or understanding of the existing codebase, leading to unnecessary changes.
- Perfectionism: Excessive refactoring that focuses on perfectionism rather than delivering value.
- Lack of Testing: Neglecting to test refactored code thoroughly, risking the introduction of new bugs.