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

Getting Clean, Keeping Lean

Getting Clean, Keeping Lean

Joe Birch

March 17, 2017
Tweet

More Decks by Joe Birch

Other Decks in Programming

Transcript

  1. Getting clean.
    Keeping lean.

    View Slide

  2. @hitherejoe hitherejoe joebirch.co
    Joe Birch - Android Engineer @ Buffer

    View Slide

  3. @buffer buffer.com

    View Slide

  4. Greenfield projects

    View Slide

  5. View Slide

  6. - High-level of technical debt
    - Extremely tightly coupled concepts
    - No unit or UI tests
    - Difficult to know what is going on and where
    No longer lean

    View Slide

  7. - YOU know what is what
    - YOU know where class X is
    - YOU know where logic for Y is
    - But what about along the line…
    No longer lean
    (for now)
    (for now)
    (for now)

    View Slide

  8. 25
    50
    75
    100
    Output
    Debt

    View Slide

  9. 25
    50
    75
    100
    Output
    Debt

    View Slide

  10. For the sake of yours and future
    developers sanity - have
    empathy. Be mindful of the
    consequences and think before
    you craft.

    View Slide

  11. Buffer for Android

    View Slide

  12. View Slide

  13. View Slide

  14. View Slide

  15. - Lack of architecture
    - Bloated activities with multiple responsibilities
    - Difficult to test
    - Difficult to extend and maintain
    - Duplication of concepts == no reuse

    View Slide

  16. Moving to MVP
    - Fear of changing too much
    - Helped separate presentation logic
    - DataManagers to house data operations
    - Increased test coverage

    View Slide

  17. View Slide

  18. View Slide

  19. Not enough.
    - Bloated DataManager classes
    - Future changes, offline - where?
    - Presentation linked to specific data source
    - No clear line between responsibilites
    - Buffer is a big app. Data types, features, sources

    View Slide

  20. Architecture
    “The art of planning, designing and
    constructing buildings”

    View Slide

  21. Architecture
    “The art of planning, designing and
    constructing buildings concepts”

    View Slide

  22. View Slide

  23. View Slide

  24. View Slide

  25. View Slide

  26. View Slide


  27. Think outside of your team.

    View Slide

  28. Clean Architecture

    View Slide

  29. View Slide

  30. Separation of Concerns

    View Slide

  31. - Layers are independent of frameworks
    - Code becomes more testable
    - Inner layers independant of UI
    - Inner layers independant of external parties
    Separation of Concerns
    - Bugs more isolated

    View Slide

  32. View Slide

  33. Layer Models
    - Each layer has own Model classes
    - Removes dependancy on external models
    - Easily map with Mapper classes

    View Slide

  34. ModelA
    Mapper
    Model
    Mapper
    Model

    View Slide

  35. Enterprise Business Rules
    - Application Business Objects (Entities)
    - Unaffected by operational change
    - Can create before touching any UI

    View Slide

  36. public class TournamentModel {


    public String id;

    public String name;

    @SerializedName("full_name")

    public String status;

    @SerializedName("date_start")

    public String dateStart;

    @SerializedName("date_end")

    public String dateEnd;

    public int size;


    }

    View Slide

  37. Application Business Rules
    - Application specific business rules
    - Uses Cases
    - Affected by operational change
    - Isolated from external concerns

    View Slide

  38. public class GetSchedules extends UseCase {


    private final SchedulesRepository schedulesRepository;


    @Inject

    public GetSchedules(SchedulesRepository schedulesRepository) {

    this.schedulesRepository = schedulesRepository;

    }


    @Override

    public Single buildUseCaseObservable() {

    return schedulesRepository.getSchedules();

    }


    }

    View Slide

  39. View Slide

  40. Use Case
    Interface
    Interface Impl
    Activity
    Application
    Business
    Rules
    Interface
    Adapters

    View Slide

  41. Interface Adapters
    - Contains MVP architecture of our app
    - Presenters contain ‘callbacks’
    - Views define contract for implementation
    - Mapper converts data from external form

    View Slide

  42. public class UpdatesPresenter extends BasePresenter
    implements SingleObserver> {
    @Override
    public void onSubscribe(Disposable d) {

    }
    @Override
    public void onSuccess(List matches) {

    }
    @Override
    public void onError(Throwable e) {

    }
    }

    View Slide

  43. Presenter
    Interface
    Interface Impl
    Activity
    Interface
    Adapters
    Frameworks &
    Drivers

    View Slide

  44. Frameworks & Drivers
    - UI components (Activities, Fragments)
    - Networking actions (Retrofit)
    - Storage (Cache & DB)
    - Repository Pattern for interacting with these

    View Slide

  45. Repository
    Interface
    Repository
    Impl
    DataStore
    Interface
    DataStore
    Impl
    DataStore
    Impl
    DataStore
    Factory

    View Slide

  46. View Slide

  47. Layer Boundaries
    - Communication via abstractions (interfaces)
    - No direct name references in inner layers
    - Instead, inner layer provides this interface
    - aka, The Dependancy Inversion Principle

    View Slide

  48. Not limited to these layers
    Layer 1
    Layer 2
    Layer 3
    Layer 4

    View Slide

  49. Not limited to these layers
    Layer 1
    Layer 2
    Layer 3

    View Slide

  50. Not limited to these layers
    Layer 1
    Layer 2
    Layer 3
    Layer 4
    Layer 5
    Layer 6

    View Slide

  51. Test Coverage

    View Slide

  52. View Slide

  53. Learnings

    View Slide

  54. Advantages.
    - High percentage of code coverage
    - Incredibly easy to navigate package structure
    - Easier to maintain
    - Allows us to move faster
    - Focused classes and test classes

    View Slide

  55. - Seperation of concerns
    - Define, Test, Stabilise before UI
    - Futureproof implementations
    - Clear discipline, good practice
    - and more…

    View Slide

  56. Disadvantages
    - Adds initial overhead
    - Takes time to get used to
    - Can feel overkill for some tasks
    - Difficult to make decisions
    - Conversion of models between layers

    View Slide

  57. Winston
    Enterprise
    Business
    Rules
    Application
    Business
    Rules
    Interface
    Adapters
    Mobile
    UI
    TV
    UI
    Wear
    UI
    Frameworks &
    Drivers

    View Slide

  58. Winston
    Enterprise
    Business
    Rules
    Application
    Business
    Rules
    Interface
    Adapters
    Mobile
    UI
    TV
    UI
    Wear
    UI
    Frameworks &
    Drivers
    @hitherejoe hitherejoe joebirch.co

    View Slide

  59. Resources
    https://overflow.buffer.com/2016/12/22/rebuild-
    android-composer/
    https://github.com/googlesamples/android-
    architecture
    Clean Architecture: A Craftsman's Guide to Software
    Structure and Design (Robert C. Martin)

    View Slide