0% found this document useful (0 votes)
20 views47 pages

008 Code Refractoring 20250217

The document outlines various refactoring patterns aimed at improving code structure and readability. It categorizes techniques into groups such as composing methods, moving features between objects, and organizing data, detailing specific methods and their solutions to common coding problems. Each technique addresses specific code smells and provides strategies for enhancing code quality and maintainability.

Uploaded by

MANI 96 FF
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views47 pages

008 Code Refractoring 20250217

The document outlines various refactoring patterns aimed at improving code structure and readability. It categorizes techniques into groups such as composing methods, moving features between objects, and organizing data, detailing specific methods and their solutions to common coding problems. Each technique addresses specific code smells and provides strategies for enhancing code quality and maintainability.

Uploaded by

MANI 96 FF
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

+

Refactoring Patterns

Malik Nabeel Ahmed Awan, PhD

1
+ 2

The Catalog of Refactoring


Patterns
Composing Method
Refactoring Patterns

Moving features
between Objects

Organizing Data

Simplifying Conditional
Expression

Simplifying Method
Calls

Dealing in
Generalization
+ 3

The Catalog of Refactoring


Patterns
◼ Composing Methods: The refactoring techniques in this
group streamline methods, remove code duplication, and
pave the way for future improvements.
◼ Extract Method
◼ Inline Method
◼ Extract Variable


Inline Temp
Replace with Query
Split Temporary Variables
5
◼ Remove Assignment to Parameters
◼ Replace Method with Method Object
◼ Substitute Algorithm
+ 4

Extract Method – Problem

◼ You have a code fragment that can be grouped together


+ 5

Extract Method – Solution

◼ Move this code to a separate new method (or function) and


replace the old code with a call to the method.
+ 6

Extract Method – Code Smells


Eliminated
◼ Duplicate Code

◼ Long Method

◼ Feature Envy

◼ Switch Statements

◼ Message Chains

◼ Comments

◼ Data Class
+ 7

Extract Variable – Problem

◼ You have an expression that’s hard to understand.


+ 8

Extract Variable – Solution

◼ Place the result of the expression or its parts in separate


variables that are self-explanatory.
+ 9

Extract Variable – Code Smells


Eliminated
◼ Comments
+ 10

Inline Temp – Problem

◼ A temporary variable that’s assigned the result of a simple


expression and nothing more.
+ 11

Inline Temp – Solution

◼ Replace calls to the method with the method’s content and


delete the method itself.
+ 12

Replace Temp with Query -


Problem
◼ The result of an expression is placed in a local variable for
later use in your code.
+ 13

Replace Temp with Query -


Solution
◼ Move the entire expression to a separate method and return
the result from it.

◼ Query the method instead of using a variable. Incorporate


the new method in other methods, if necessary.
+ 14

Replace Temp with Query – Code


Smells Eliminated
◼ Long Method

◼ Duplicate Code
+ 15

Split Temporary Variable - Problem

◼ You have a local variable that’s used to store various


intermediate values inside a method (except for cycle
variables).
+ 16

Split Temporary Variable – Solution

◼ Use different variables for different values. Each variable


should be responsible for only one thing.
+ 17

Split Temporary Variable – Code


Smells Eliminated
◼ Extract Method
+ 18

The Catalog of Refactoring


Patterns
◼ Moving Features Between Objects: These refactoring
techniques in this show how to safely move functionality
between classes, create new classes, and hide
implementation details from public access.
◼ Move Methods
◼ Move Fields

3
◼ Extract Class
◼ Inline Class
◼ Hide Delegate
◼ Remove Middleman
◼ Introduce Foreign Methods
◼ Include Local Extensions
+ 19

Move Method – Problem

◼ A method is used more in another class than in its own class.


+ 20

Move Method – Solution

◼ Create a new method in the class that uses the method the
most, then move code from the old method to there. Turn the
code of the original method into a reference to the new
method in the other class or else remove it entirely.
+ 21

Move Method – Code Smells


Eliminated
◼ Inline Class ◼ Parallel Inheritance
Hierarchies
◼ Introduce Parameter
Object ◼ Message Chains

◼ Shotgun Surgery ◼ Inappropriate Intimacy

◼ Feature Envy ◼ Data Class

◼ Switch Statements
+ 22

Extract Class – Problem

◼ When one class does the work of two, awkwardness results.


+ 23

Extract Class – Solution

◼ Create a new class and place the fields and methods


responsible for the relevant functionality in it.
+ 24

Extract Class – Code Smell


Elimination
◼ Duplicate Code

◼ Large Class

◼ Divergent Change

◼ Data Clumps

◼ Primitive Obsession

◼ Temporary Field

◼ Inappropriate Intimacy
+ 25

Remove Middleman – Problem

◼ A class has too many methods that simply delegate to other


objects.
+ 26

Remove Middleman – Solution

◼ Delete these methods and force the client to call the end
methods directly.
+ 27

Remove Middleman – Code Smell


Elimination
◼ Middle Man
+ 28

The Catalog of Refactoring


Patterns
◼ Organizing Data: These refactoring techniques in this work
with data handling, replacing primitives with rich class
functionality. Another important aspect is untangling of class
associations, which makes classes more portable and
reusable.
◼ Change Value of Reference

6
◼ Change Reference to Value
◼ Duplicate Observed Data
◼ Self Encapsulated Fields
◼ Replace data with Objects
◼ Replace Array With Objects
◼ Change Unidirectional Association to Bi-directional
+ 29

The Catalog of Refactoring


Patterns
◼ Organizing Data
◼ Change Bi-directional Association to unidirectional
◼ Encapsulated Fields
◼ Encapsulated Collection
◼ Replace Magic number with symbolic Constants
◼ Replace type code with Class
◼ Replace type code with state/strategy
◼ Replace subclass with Fields
+ 30

Replace Data Value with Object –


Problem
◼ A class (or group of classes) contains a data field. The field
has its own behavior and associated data.
+ 31

Replace Data Value with Object –


Solution
◼ Create a new class, place the old field and its behavior in the
class, and store the object of the class in the original class.
+ 32

Replace Data Value with Object –


Code Smell Eliminated
◼ Duplicate Code
+ 33

Replace Array with Object –


Problem
◼ You have an array that contains various types of data.
+ 34

Replace Array with Object –


Solution
◼ Replace the array with an object that will have separate
fields for each element.
+ 35

Replace Array with Object – Code


Smell Eliminated
◼ Primitive Obsession
+ 36

Encapsulated Fields – Problem

◼ You have a public field.


+ 37

Encapsulated Fields – Solution

◼ Make the field private and create access methods for it.
+ 38

Encapsulated Fields – Code Smell


Eliminated
◼ Data Class
+ 39

Encapsulate Collection – Problem

◼ A class contains a collection field and a simple getter and


setter for working with the collection.
+ 40

Encapsulate Collection – Solution

◼ Make the getter-returned value read-only and create


methods for adding/deleting elements of the collection.
+ 41

Encapsulate Collection – Code


Smell Eliminated
◼ Data Class
+ 42

Replace magic number with


Symbolic Constant – Problem
◼ A number that has a certain meaning to it.
+ 43

Replace magic number with


Symbolic Constant – Solution
◼ Replace this number with a constant that has a human-
readable name explaining the meaning of the number.
+ 44

Replace code with Subclass –


Problem
◼ A coded type that directly affects program behavior (values
of this field trigger various code in conditionals).
+ 45

Replace code with Subclass –


Solution
◼ Create subclasses for each value of the coded type. Then
extract the relevant behaviors from the original class to these
subclasses. Replace the control flow code with
polymorphism.
+ 46

Replace code with Subclass –


Code Smell Eliminated
◼ Primitive Obsession
+

THANKS

You might also like