Code refactoring involves restructuring existing code without changing its external behavior to improve qualities like readability, maintainability and extensibility. It involves techniques like extracting methods, consolidating conditional logic, simplifying method calls, generalizing classes and hierarchies, and separating concerns. Refactoring helps reduce defects, improve design, and make code easier to understand and modify over time. It should be done iteratively and tests ensured to pass after each small refactoring step.
In this document
Powered by AI
Introduction to the concept of code refactoring, its definition, motivations, and key requirements.
Discussing how to identify problematic code ('code smells') and the tools available for effective refactoring.
Techniques for composing methods, including various strategies like Extract Method and Inline Method to improve code clarity.
Strategies for substituting algorithms in methods to enhance code efficiency and maintainability.
Techniques for moving features between objects, such as Move Method and Extract Class, aimed at better organization.
Strategies for organizing data, including encapsulation, replacing magic numbers, and various data transformations.
Methods for simplifying complex conditional expressions, such as Consolidate Conditional Expressions and using Guard Clauses.
Techniques for simplifying method calls, including method renaming, separating queries from modifiers, and parameter management.Approaches for managing generalization in code, like Pull Up and Push Down methods, Extracting Superclasses, and Interfaces.
Advanced refactoring strategies such as separating domain from presentation and converting procedural designs into object-oriented ones.
The refactoring cycle, ensuring all tests pass, and a focus on best practices for effective code refactoring.
Benefits and advantages of refactoring, enhancing code quality and maintainability, along with best practices for code reviews.
Engagement with the audience for questions and discussions regarding the topics covered in the presentation.
Replace Temp withQuery
Extract the expression into a method. Replace all references to the temp
with the expression. The new method can then be used in other methods.
13.
Introduce Explaining Variable
Extractthe expression into a method. Replace all references to the temp
with the expression. The new method can then be used in other methods.
Replace Method withMethod Object
Turn the method into its own object so that all the local variables become
fields on that object. You can then decompose the method into other
methods on the same object
Move Method
Create anew method with a similar body in the class it uses most. Either
turn the old method into a simple delegation, or remove it altogether