I will have to
refactor ! And now
? Refactoring
Techniques in Java
Kamila Santos
Slide 2
Slide 2 text
Kamila santos
TECH LEAD @ZUPINNOVATION
MICROSOFT MVP
BOOK CO-AUTHOR, INSTRUCTOR
CONTENT CREATOR @KAMILA_CODE
CO-ORGANIZER @WOMAKERSCODE,
@PERIFACODE AND @DEVSJAVAGIRL
Slide 3
Slide 3 text
What is refactoring?
We can define refactoring as a modification made to the
internal structure of a software to make it simpler to
understand and less costly to change, without changing
its behavior.
Slide 4
Slide 4 text
Why should we refactor?
Refactoring improves software design
Refactoring makes software simpler to understand
Refactoring helps in finding bugs
Refactoring helps us develop faster
Slide 5
Slide 5 text
Preparatory refactoring
Slide 6
Slide 6 text
Refactoring for
understanding
Slide 7
Slide 7 text
Refactoring for garbage
collection
Slide 8
Slide 8 text
Long-running
refactoring
Slide 9
Slide 9 text
Refactoring in Code
review
Slide 10
Slide 10 text
What are the disadvantages of
refactoring?
Longer time to deliver features
Increases the chance of large merge conflicts
Most tests need to be rewritten
Slide 11
Slide 11 text
Code smells
indications of problems or deficiencies in the source
code of a computer program. These smells could be
signs of poor programming practices, poor design, or
possible bugs.
Slide 12
Slide 12 text
Code smells
When identified, code smells can be considered an
invitation to further investigate the code and make
improvements.
Slide 13
Slide 13 text
Code duplication
Slide 14
Slide 14 text
Very long methods
Slide 15
Slide 15 text
Class with many
responsibilities
Slide 16
Slide 16 text
Confusing and generic
variable names
Slide 17
Slide 17 text
Irrelevant or outdated
comments
Slide 18
Slide 18 text
Refactoring techniques
Slide 19
Slide 19 text
Extract function
is used when a piece of code in a method or function
can be grouped into a separate function.
Slide 20
Slide 20 text
Extract function
This function encapsulates the logic contained in the
original code snippet and can be called in other places,
avoiding code duplication.
Slide 21
Slide 21 text
No content
Slide 22
Slide 22 text
No content
Slide 23
Slide 23 text
Extract variable
is used when you have a complex expression in a piece
of code that can be extracted into a variable with a
descriptive name.
Slide 24
Slide 24 text
Extract Variable
you identify an expression that is difficult to understand
or that is used multiple times in the code. Then you
extract it into a separate variable and give that variable
a meaningful name.
Slide 25
Slide 25 text
Extract Variable
Slide 26
Slide 26 text
Extract Variable
Slide 27
Slide 27 text
Encapsulate Variable
is used to encapsulate a variable, making it private and
providing access methods (getters and setters) to
manipulate it. This technique helps improve data access
encapsulation, control, and consistency.
Slide 28
Slide 28 text
Encapsulate Variable
you identify a variable that is being accessed directly
from outside the class and make it private. You then
create getters and possibly setters to interact with that
variable.
Slide 29
Slide 29 text
No content
Slide 30
Slide 30 text
Introduce Parameter
Object
is used when a set of related parameters are being
passed repeatedly to a method or function. This
technique involves creating a new object that
encapsulates these related parameters, simplifying the
method call and improving code readability.
Slide 31
Slide 31 text
Introduce Parameter
Object
you identify a set of parameters that are frequently
passed together and group them into an object. This
new object is then passed as a single parameter to the
relevant method or function.
Slide 32
Slide 32 text
No content
Slide 33
Slide 33 text
Split Phase
is used when a method or function performs two or
more distinct phases or different types of tasks. This
technique involves separating these phases into
separate methods or functions, to improve the
readability, modularity and maintainability of the code.
Slide 34
Slide 34 text
Split Phase
you identify the different phases or types of tasks that
are being performed in a single method or function. You
then extract each phase into a separate method or
function, allowing each to be more focused and specific
in its responsibility.
Slide 35
Slide 35 text
No content
Slide 36
Slide 36 text
Extract class
is used when a class contains responsibilities or
functionality that can be better organized in a new,
separate class. This technique involves creating a new
class and transferring related attributes and methods to
this new class.
Slide 37
Slide 37 text
Remove Middle Man
is used when a class acts as an unnecessary
intermediary between other classes. This technique
involves eliminating the middleman, allowing method
calls to be made directly between the relevant classes.
Slide 38
Slide 38 text
No content
Slide 39
Slide 39 text
Hide delegate
is used when a class is providing direct access to a
delegate object and that direct access is being overused
or violating the principle of encapsulation. This
technique involves hiding the delegate object behind
methods of the class that encapsulate it, limiting direct
access to the delegate.
Slide 40
Slide 40 text
Move function
is used when a function or method is located in a class
that is not the most appropriate to house it. This
technique involves moving the function to another class
that has a more appropriate responsibility or where the
function can be more easily reused.
Slide 41
Slide 41 text
Remove Dead Code
is used when there are sections of code that are no
longer used and have no impact on the operation of the
program. This technique involves removing these
unnecessary code snippets, simplifying and making the
code cleaner and more readable.
Slide 42
Slide 42 text
Change Value to
Reference
is used when you have an object that is used as a value,
but you realize that you need to treat it as an object
with its own identity. This technique involves replacing
the value representation with a reference
representation, allowing multiple objects to refer to the
same instance.
Slide 43
Slide 43 text
No content
Slide 44
Slide 44 text
No content
Slide 45
Slide 45 text
Decompose Conditional
is used when you have a complex condition with
multiple branches and the logic becomes difficult to
understand and maintain. This technique involves
decomposing the condition into smaller, clearer parts,
making the code easier to understand and maintain.
Slide 46
Slide 46 text
Introduce Assertions
used to add assertive checks to code to detect and
capture errors or invalid states during development and
debugging. This technique involves inserting assertions
to validate conditions that must be true at certain points
in the code.
Slide 47
Slide 47 text
Replace Constructor with
Factory Method
is used when you have a class with one or more
constructors that can be overridden by a factory
function. This technique involves creating a static
function that encapsulates object creation, providing a
more expressive and flexible interface for creating
instances of the class.
Slide 48
Slide 48 text
No content
Slide 49
Slide 49 text
No content
Slide 50
Slide 50 text
Pull up method
is used when you have similar methods in subclasses
and want to move them to the superclass to avoid code
duplication and promote reuse. This technique involves
moving a method from one or more subclasses to the
parent class, making it a common method that can be
shared by all subclasses.
Slide 51
Slide 51 text
No content
Slide 52
Slide 52 text
No content
Slide 53
Slide 53 text
Extract Subclass
is used when you have a class that is performing
functionality that is specific to a subset of objects in
that class. This technique involves creating a new
subclass that represents this specific subset of objects
and moving the relevant functionality to this new
subclass.
Slide 54
Slide 54 text
Collapse Hierarchy
is used when you have a hierarchy of classes where
differentiation between subclasses becomes
unnecessary or irrelevant. This technique involves
removing one or more intermediate subclasses, moving
their attributes and behaviors directly to the parent
class.
Slide 55
Slide 55 text
No content
Slide 56
Slide 56 text
No content
Slide 57
Slide 57 text
Conclusion
Each refactoring technique aims to solve a specific
problem and they must often be applied together.