Slide 1

Slide 1 text

I will have to refactor ! And now ? Kamila Santos REFACTORING TECHNIQUES IN JAVA

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

schedule principles of refactoring when should we refactor? code smells refactoring techniques

Slide 4

Slide 4 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 5

Slide 5 text

Why should we refactor? Refactoring improves software design Refactoring makes software simpler to understand Improvement in the quality of blended learning Refactoring helps in finding bugs Refactoring helps us develop faster

Slide 6

Slide 6 text

Preparatory refactoring

Slide 7

Slide 7 text

Refactoring for Comprehension

Slide 8

Slide 8 text

Refactoring for garbage collection

Slide 9

Slide 9 text

Long term refactoring

Slide 10

Slide 10 text

Refactoring in Code Review

Slide 11

Slide 11 text

What are the downsides of doing refactorings? It took us longer to deliver some features Increased chance of merge conflicts Tests that need rewriting

Slide 12

Slide 12 text

Code smells Are indications of problems in the source code of a computer program. These smells could be signs of bad programming practices, poor design, or potential bugs.

Slide 13

Slide 13 text

Code smells Code smells are symptoms that code may be difficult to understand, maintain, or evolve. They are not specific errors, but rather characteristics that indicate possible underlying issues. When identified, code smells can be considered as an invitation to delve deeper into the code and make improvements.

Slide 14

Slide 14 text

Code duplication having repeating code snippets can make maintenance more difficult, as any change has to be done in multiple parts.

Slide 15

Slide 15 text

Very long methods or functions when a method or function is too long, it can make the code difficult to read and understand.

Slide 16

Slide 16 text

Class or method with many responsibilities a class or method with too many responsibilities violates the single responsibility principle, making it difficult to understand and maintain.

Slide 17

Slide 17 text

Confusing or generic variable names Variable names that are not descriptive or that don't follow naming conventions can make code difficult to understand.

Slide 18

Slide 18 text

Irrelevant or outdated comments comments that are not in line with the actual code or that don't provide useful information can be misleading and lead to errors.

Slide 19

Slide 19 text

Refactoring Techniques

Slide 20

Slide 20 text

Extract Function is used when a piece of code in a method or function can be wrapped in a separate function. This is done to improve code readability, reusability, and maintainability.

Slide 21

Slide 21 text

Extract Function you identify a block of code that performs a specific task and extract it into a new function with a descriptive name. This function encapsulates the logic contained in the original code snippet and can be called elsewhere, avoiding code duplication

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 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. This technique helps improve code readability, understandability, and maintainability.

Slide 25

Slide 25 text

Extract Variable you identify an expression that is difficult to understand or that is used multiple times in the code. You then extract it into a separate variable and give that variable a meaningful name.

Slide 26

Slide 26 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 the encapsulation, control, and consistency of data access.

Slide 27

Slide 27 text

Encapsulate Variable You identify a variable that is being accessed directly from outside the class and make it private. You then create accessor methods (getters) and possibly modification methods (setters) to interact with that variable.

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

Introduce Parameter Object is used when a set of related parameters is being repeatedly passed 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 30

Slide 30 text

Introduce Parameter Object you identify a set of parameters that are often 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 31

Slide 31 text

No content

Slide 32

Slide 32 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 33

Slide 33 text

Split Phase You identify the different phases or types of tasks being performed within 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 34

Slide 34 text

No content

Slide 35

Slide 35 text

Extract class is used when a class contains responsibilities or functionality that can be better organized in a separate new class. This technique involves creating a new class and transferring related attributes and methods to that new class.

Slide 36

Slide 36 text

Extract class you identify a set of attributes and methods within an existing class that are related to a specific responsibility or functionality. You then extract those attributes and methods into a new class, making them more cohesive and making the code easier to maintain.

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

Remove Middle Man you identify a class that is acting as a go-between, simply passing method calls to another class without adding any significant value or logic. You then direct the method calls directly to the class being brokered.

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 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 41

Slide 41 text

Hide delegate you identify the points where the class is exposing the delegate object and providing direct access to it. You then hide the delegate behind methods of the class that act as intermediaries, allowing the class to control how the delegate is accessed and preventing uncontrolled direct access.

Slide 42

Slide 42 text

Move function is applied when a function or method is located in a class that is not the best fit to contain it. This technique involves moving the function to another class that has more suitable responsibility or where the function can be more easily reused.

Slide 43

Slide 43 text

Move function And how to apply it? You should identify a function or method that is in a class where it doesn't fit well in terms of responsibility or cohesiveness. Then you move that role to a more appropriate class, ensuring the functionality is placed in the right context.

Slide 44

Slide 44 text

Remove Dead Code It is recommended when there are code snippets that are no longer used and have no impact on the program's operation. This technique involves removing these unnecessary code snippets, simplifying and making the code cleaner and more readable.

Slide 45

Slide 45 text

Remove Dead Code These snippets can include unused variables, methods or functions not called, conditional blocks that are never true or false, and so on.

Slide 46

Slide 46 text

Change Value to Reference this technique is used when we have an object that is used as a value, but we realize that it is necessary to treat it as an object with its own identity.

Slide 47

Slide 47 text

Change Value to Reference to apply this technique you must identify an object that must be treated as a single entity and not as a value. So you create a new class to represent that object, where equality is based on identity rather than object content. It then replaces occurrences of the value object with references to the new class.

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

Decompose Conditional Is used when you have a complex condition with many branches and the logic becomes difficult to understand and maintain. This technique involves breaking down the condition into smaller, clearer parts, making the code easier to understand and maintain.

Slide 50

Slide 50 text

Decompose Conditional The steps for this technique are as follows: you identify a complex condition with multiple clauses and extract each clause into a separate method or function with a descriptive name.

Slide 51

Slide 51 text

Decompose Conditional You then replace the original condition with calling those methods or functions, making the code more readable.

Slide 52

Slide 52 text

Introduce Assertions You locate a point in the code where you need to guarantee a specific condition. You then add an assertion that checks for that condition, and if the condition isn't true, an error or exception is thrown.

Slide 53

Slide 53 text

Replace Constructor with Factory Method Is applicable when you have a class with one or more constructors that can be overridden by a factory function.

Slide 54

Slide 54 text

Replace Constructor with Factory Method This technique consists of creating a static function that encapsulates object creation, providing a more expressive and flexible interface for creating instances of the class.

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

Pull up method you find a method in one or more subclasses that has a similar implementation and can be moved to the parent class.

Slide 57

Slide 57 text

Pull up method You then move that method into the parent class and adjust corresponding calls in subclasses to call the method in the parent class.

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 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.

Slide 60

Slide 60 text

Extract Subclass This technique involves creating a new subclass that represents that specific subset of objects and moving the relevant functionality into that new subclass.

Slide 61

Slide 61 text

Collapse Hierarchy is used when you have a hierarchy of classes where the differentiation between the subclasses becomes unnecessary or irrelevant.

Slide 62

Slide 62 text

Collapse Hierarchy you identify that the differentiation between the subclasses is no longer necessary or relevant. You then remove the intermediate subclasses and move their attributes and behaviors directly into the parent class. This simplifies the hierarchy, reduces complexity, and improves code readability.

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

Conclusion Each refactoring technique aims to solve a specific problem and many times they are even used together. There are several other techniques besides those mentioned here that can help you a lot in everyday life.

Slide 65

Slide 65 text

Do you have any questions? Hope you learned something new.