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

Code Surgery: Refactoring for a Healthy App

Love
April 06, 2024

Code Surgery: Refactoring for a Healthy App

In this presentation, we'll delve into the world of code refactoring, a powerful technique for enhancing the quality of your codebase. We'll explore what refactoring is, its benefits, and practical refactoring techniques you can implement to make your code cleaner, more understandable, and easier to maintain.

Love

April 06, 2024
Tweet

More Decks by Love

Other Decks in Technology

Transcript

  1. Code Surgery: Refactoring for a Healthy App Love Otudor Android

    Developer and Technical Writer, Frontendlabs
  2. - Martin Fowler Whenever I have to think to understand

    what the code is doing, I ask myself if I can refactor the code to make that understanding more immediately apparent
  3. Why Refactor Your Code? 1. Easy to read and understand.

    2. Improved Maintainability. 3. Reduced Bugs and Code Smells.
  4. Why Refactor Your Code? 1. Easy to read and understand.

    2. Improved Maintainability. 3. Reduced Bugs and Code Smells. 4. Improved Team Collaboration.
  5. Examples of code smells: 1. Long methods. 2. Duplicate code.

    3. God classes. 4. Long parameter lists. 5. Magic Numbers. 6. Nested Conditional Statements (if-else hell)
  6. Challenges of Refactoring 1. Time Constraints. 2. Fear of Breaking

    Things. 3. Testing Overhead. 4. Lack of Awareness.
  7. Challenges of Refactoring 1. Time Constraints. 2. Fear of Breaking

    Things. 3. Testing Overhead. 4. Lack of Awareness. 5. Legacy Code.
  8. Refactoring Techniques 1. Composing Methods. 2. Moving Features between Objects.

    3. Organizing Data. 4. Dealing with Generalization. 5. Simplifying Conditional Expressions.
  9. Refactoring Best Practices 1. Understand the code. 2. Refactor in

    small, incremental steps to minimize the risk of regressions.
  10. Refactoring Best Practices 1. Understand the code. 2. Refactor in

    small, incremental steps to minimize the risk of regressions. 3. Write unit tests before refactoring to ensure functionality is preserved.
  11. Refactoring Best Practices 4. Document your changes to keep your

    team informed and maintain code clarity.
  12. Refactoring Best Practices 4. Document your changes to keep your

    team informed and maintain code clarity. 5. Automate wherever possible.
  13. Refactoring Best Practices 4. Document your changes to keep your

    team informed and maintain code clarity 5. Automate wherever possible. 6. Fix software defects separately.
  14. Refactoring Best Practices 4. Document your changes to keep your

    team informed and maintain code clarity 5. Automate wherever possible. 6. Fix software defects separately. 7. Refactor first before adding updates or new features to existing code to reduce technical debt.
  15. Long methods Break down to smaller, focused methods with clear

    responsibilities. Each method should ideally handle a single step or subtask within the overall functionality
  16. God classes 1. Break down the class into smaller, focused

    classes based on functionality. 2. Utilize delegation to distribute responsibilities across multiple classes.
  17. Long Parameter Lists 1. Create data classes or helper objects

    to group related parameters. 2. Utilize default parameter values for optional parameters. 3. Consider builder pattern for complex object creation with many parameters.
  18. Magic Numbers 1. Define constants or static variables with clear

    and descriptive names for these values. 2. Use configuration files or resource bundles to store these values.
  19. If-Else Hell 1. Utilize the when expression for handling multiple

    conditions in a cleaner and more readable way. 2. Refactor complex logic into separate functions or use the Strategy pattern.
  20. Some Refactoring Tools 1. IDE’s: Android Studio, IntelliJ IDEA, etc.

    2. JRefactor. 3. SourceMage. 4. CodeRush (for Visual Studio). 5. Refactoring.Guru.