Upgrade to Pro — share decks privately, control downloads, hide ads and more …

[Developer week latam] I will have to refactor ...

[Developer week latam] I will have to refactor ! And now ?Refactoring Techniques in Java

More Decks by Kamila de fatima santos oliveira

Other Decks in Programming

Transcript

  1. I will have to refactor ! And now ? Kamila

    Santos REFACTORING TECHNIQUES IN JAVA
  2. Kamila santos TECH LEAD @ZUPINNOVATION MICROSOFT MVP BOOK CO-AUTHOR, INSTRUCTOR

    CONTENT CREATOR @KAMILA_CODE CO-ORGANIZER @WOMAKERSCODE, @PERIFACODE AND @DEVSJAVAGIRL
  3. 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.
  4. 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
  5. What are the downsides of doing refactorings? It took us

    longer to deliver some features Increased chance of merge conflicts Tests that need rewriting
  6. 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.
  7. 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.
  8. Code duplication having repeating code snippets can make maintenance more

    difficult, as any change has to be done in multiple parts.
  9. Very long methods or functions when a method or function

    is too long, it can make the code difficult to read and understand.
  10. 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.
  11. Confusing or generic variable names Variable names that are not

    descriptive or that don't follow naming conventions can make code difficult to understand.
  12. 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.
  13. 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.
  14. 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
  15. 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.
  16. 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.
  17. 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.
  18. 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.
  19. 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.
  20. 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.
  21. 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.
  22. 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.
  23. 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.
  24. 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.
  25. 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.
  26. 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.
  27. 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.
  28. 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.
  29. 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.
  30. 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.
  31. 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.
  32. 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.
  33. 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.
  34. 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.
  35. 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.
  36. 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.
  37. Decompose Conditional You then replace the original condition with calling

    those methods or functions, making the code more readable.
  38. 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.
  39. 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.
  40. 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.
  41. 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.
  42. 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.
  43. Extract Subclass is used when you have a class that

    is performing functionality that is specific to a subset of objects in that class.
  44. 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.
  45. Collapse Hierarchy is used when you have a hierarchy of

    classes where the differentiation between the subclasses becomes unnecessary or irrelevant.
  46. 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.
  47. 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.