This document discusses refactoring code to improve its structure and design without changing its external behavior. Refactoring includes techniques like extracting methods, moving methods between classes, simplifying conditional logic, and improving data structures. The goal of refactoring is to make code more readable, reusable, and maintainable by removing duplications and optimizing class relationships and communication. The document provides examples of code smells that indicate needs for refactoring and explains various refactoring techniques at the method, class, and architecture level.
2. Refactoring Code
Making code better, may not be faster
Better structured, better built
More readable, more understandable
Easy to work with
Easy to add new features
Easy to spot and fix bugs
Keeping code in control
Improving existing code
3. Contents
Introduction to Refactoring
Method-Level Refactoring
Class and Condition Focused Refactoring
Data Focused Refactoring
Communication and High Level Refactoring
Conclusion
4. What REFACTORING is?
Techniques of improving code that already exists
No rules that anyone can blindly follow to improve
code
Techniques of recognize issues as per consciousness
Applied without thinking will be dangerous
5. What REFACTORING is NOT?
Not debugging
Not adding feature
Not changing observable behavior of code
Not performance improving
7. When to use?
Write Scan Refactor at the end : INCORRECT
Write Scan Refactor regularly : CORRECT
Code reviews
How to use?
8. Code smell
Duplicated code
Long methods
Comments
Doesnt tell what to do but tells us what to notice
9. Method Level Refactoring
Preparing to refactor
Grouping refactorings together
Using the Extract Method refactoring
Extract Method with parameters and variables
Common code smells for Extract Method
Using IDEs for the Extract Method refactoring
The Inline Method refactoring
Refactorings that remove temps
Refactorings that add temps
11. Grouping refactorings together
Individual Method(name? parameters? Temp
variables? Duplicate? Too much in method?)
Context of method in class (right class? similarity with
other methods class)
Communication between classes(inheritance)
12. Extract Method refactoring
Smaller and specific is better
Provides reusability
Ease of overriding while using inheritance
13. Extract Method with parameters
Parameters should be related to the method
Extract Method with variables
Temporary variables can be extracted
14. Code smells for using Extract
Method
Long method
Comments(having comments vs need comments)
21. Class and Condition Focused
Refactoring
Move Method
Extract Class and Inline Class
Making conditions easier to read
Replacing conditions with polymorphism
Replacing type code with subclasses
22. Move Method
Move method from one class to another
When a method interact with multiple classes then
move it to the class with which it interacts more
Code smell: feature envy, inappropriate intimacy,
shotgun surgery
27. Data Focused Refactoring
Moving and encapsulating fields
Working with data clumps
Simplifying method calls and parameter use
Pulling and pushing methods and fields
29. Working with data clumps
Preserve whole object
(instead of parameters pass object)
Introduce parameter object
(minimize parameters list by creating a class for set of
parameters)
30. Simplifying method calls and
parameter use
Rename method
Remove parameter
Add parameter
Parameterize method
(same behavior multiple methods)
De-parameterize method
(same type of parameter but different behavior depending
on parameter)
Separate Query from Modifier
(split method with individual focus on functioning)
31. Pulling and pushing methods and
fields
Pull up method
Pull up field
Push down method
Push down field
32. Communication and High-Level
Refactoring
Refining hierarchies
Extract superclass, Extract subclass, collapse hierarchy
Communication refactoring
Code smell: message chain, middle man
Larger scale refactoring
Convert procedural to OO
Code smell: large classes, god classes, dumb data