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

[We are developers Java developer day ] - I will have to refactor ! And now ? Refactoring Techniques in Java

[We are developers Java developer day ] - I will have to refactor ! And now ? Refactoring Techniques in Java

Tweet

More Decks by Kamila de fatima santos oliveira

Other Decks in Programming

Transcript

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

    Techniques in Java Kamila Santos
  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 Refactoring helps in finding bugs Refactoring helps us develop faster
  5. What are the disadvantages of refactoring? Longer time to deliver

    features Increases the chance of large merge conflicts Most tests need to be rewritten
  6. 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.
  7. Code smells When identified, code smells can be considered an

    invitation to further investigate the code and make improvements.
  8. Extract function is used when a piece of code in

    a method or function can be grouped into a separate function.
  9. Extract function This function encapsulates the logic contained in the

    original code snippet and can be called in other places, avoiding code duplication.
  10. 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.
  11. 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.
  12. 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.
  13. 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.
  14. 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.
  15. 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.
  16. 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.
  17. 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.
  18. 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.
  19. 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.
  20. 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.
  21. 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.
  22. 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.
  23. 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.
  24. 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.
  25. 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.
  26. 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.
  27. 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.
  28. 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.
  29. 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.