$30 off During Our Annual Pro Sale. View Details »

Single-responsibility principle meets the real world!

Single-responsibility principle meets the real world!

Presented at BlrDroid + BlrKotlin meetup on Aug 19, 2023.

"No plan survives contact with the enemy." goes an adage. This is sadly true for most software engineering principles too. Developers must constantly work against entropy to restore order in their codebases.

SOLID principles have guided many developers to validate their solutions and have helped many build maintainable software systems. However, it can be daunting to practice in the real world without constant feedback, upskilling, and support from peers and tooling.

In this talk, we aim to take a closer look at the first of the five object-oriented design principles from SOLID—Single-responsibility principle. We'll look at large classes and how they violate SRP. We'll also discuss insights and heuristics to identify each of these responsibilities.

What's good comes out of mere insights and heuristics? We'll also put these to good use in practice. At the end of the session, we'll pick one of the most popular messaging apps on Android and do the following,

- Visualize it with tooling
- Identify multiple responsibilities
- Discuss code smells
- Extract at least one responsibility into a separate class without tests

Ragunath Jawahar

August 19, 2023
Tweet

More Decks by Ragunath Jawahar

Other Decks in Programming

Transcript

  1. @ragunathjawahar / legacycode.com
    Single-responsibility principle

    View Slide

  2. @ragunathjawahar / legacycode.com
    Single-responsibility principle
    meets the real world! 🥳

    View Slide

  3. SOLID
    @ragunathjawahar / legacycode.com

    View Slide

  4. SOLID
    @ragunathjawahar / legacycode.com

    View Slide

  5. Single-responsibility principle
    • Every class should have only one responsibility
    • A class should have only one reason to change
    • A module should be responsible to one, and only one, actor
    @ragunathjawahar / legacycode.com

    View Slide

  6. Why do we have large classes?
    @ragunathjawahar / legacycode.com

    View Slide

  7. Why do we have large classes?
    • Code smells
    • Long methods
    • Feature-envy
    • Too many methods/functions
    • Missing abstraction
    • Duplication
    • Primitive obsession
    • Data clumps
    • …
    • Developer churn
    • No tests
    • Cross-cutting concerns
    • Utilities
    • High-level classes

    View Slide

  8. Why do we have large classes?
    • Code smells
    • Long methods
    • Feature-envy
    • Too many methods/functions
    • Missing abstraction
    • Duplication
    • Primitive obsession
    • Data clumps
    • …
    • Developer churn
    • No tests
    • Cross-cutting concerns
    • Utilities
    • High-level classes

    View Slide

  9. High-level classes
    The term "high-level classes" typically refers to classes that operate
    at a higher level of abstraction compared to other classes in the
    system. They generally encapsulate broad concepts, complex
    behaviors, or orchestration tasks rather than speci
    fi
    c, detailed
    implementations.
    @ragunathjawahar / legacycode.com

    View Slide

  10. High-level classes
    The term "high-level classes" typically refers to classes that operate
    at a higher level of abstraction compared to other classes in the
    system. They generally encapsulate broad concepts, complex
    behaviors, or orchestration tasks rather than speci
    fi
    c, detailed
    implementations.
    @ragunathjawahar / legacycode.com

    View Slide

  11. High-level classes
    The term "high-level classes" typically refers to classes that operate
    at a higher level of abstraction compared to other classes in the
    system. They generally encapsulate broad concepts, complex
    behaviors, or orchestration tasks rather than speci
    fi
    c, detailed
    implementations.
    @ragunathjawahar / legacycode.com

    View Slide

  12. High-level classes
    The term "high-level classes" typically refers to classes that operate
    at a higher level of abstraction compared to other classes in the
    system. They generally encapsulate broad concepts, complex
    behaviors, or orchestration tasks rather than speci
    fi
    c, detailed
    implementations.
    @ragunathjawahar / legacycode.com

    View Slide

  13. High-level classes
    The term "high-level classes" typically refers to classes that operate
    at a higher level of abstraction compared to other classes in the
    system. They generally encapsulate broad concepts, complex
    behaviors, or orchestration tasks rather than speci
    fi
    c, detailed
    implementations.
    @ragunathjawahar / legacycode.com

    View Slide

  14. High-level classes
    The term "high-level classes" typically refers to classes that operate
    at a higher level of abstraction compared to other classes in the
    system. They generally encapsulate broad concepts, complex
    behaviors, or orchestration tasks rather than speci
    fi
    c, detailed
    implementations.
    @ragunathjawahar / legacycode.com

    View Slide

  15. TLDR;
    They just have too many things to do 🤷
    @ragunathjawahar / legacycode.com

    View Slide

  16. How do we deal with them?
    @ragunathjawahar / legacycode.com

    View Slide

  17. —Albert Einstein
    “We cannot solve our problems with the
    same thinking we used when we created
    them.”

    View Slide

  18. Eureka +
    StoryViewerPageFragment.kt
    @ragunathjawahar / legacycode.com

    View Slide

  19. Eureka +
    StoryViewerPageFragment.kt
    @ragunathjawahar / legacycode.com

    View Slide

  20. Eureka +
    StoryViewerPageFragment.kt
    @ragunathjawahar / legacycode.com

    View Slide

  21. Eureka +
    StoryViewerPageFragment.kt
    (1350 LOC)
    @ragunathjawahar / legacycode.com

    View Slide

  22. Live Demo 🧑💻
    @ragunathjawahar / legacycode.com

    View Slide

  23. Do
    • Use IDE-assisted refactoring
    • Use a git GUI client
    • Use micro-commits with Arlo Belshee’s commit notation
    • Pair/ensemble program
    • Verify manually

    View Slide

  24. Don’t
    • Have long running branches
    • Push it too far
    @ragunathjawahar / legacycode.com

    View Slide

  25. Installation
    $ brew install legacycodehq/tap/eureka
    @ragunathjawahar / legacycode.com

    View Slide

  26. Watch
    $ eureka watch -x android StoryViewerPageFragment
    @ragunathjawahar / legacycode.com

    View Slide

  27. @ragunathjawahar / legacycode.com

    View Slide

  28. https://github.com/
    LegacyCodeHQ/eureka
    @ragunathjawahar / legacycode.com

    View Slide

  29. Questions?
    @ragunathjawahar / legacycode.com

    View Slide