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

How to Write Clean Code in Android

Mohanraj K.M.
July 03, 2017
250

How to Write Clean Code in Android

Mohanraj K.M.

July 03, 2017
Tweet

Transcript

  1. android Clean Code
    Mohanraj Karatadipalayam
    @kmmraj

    View Slide

  2. Let's Start With Unit Testing
    • Typical Activity Class
    • Data retrieval
    • Data manipulation and decoration
    • UI-specific activities like rendering,
    painting, responding to user events, and
    the creation of fragments.
    • One class does all work
    • Massive Activity, Massive Fragment,
    Massive ViewController

    View Slide

  3. Can I Test them ?
    • Lets take data retrieval
    • All methods are intertwined
    • Doing everything everywhere
    • I need API provider
    • I need SQLite
    • I need to convert from datamodel
    to viewmodel
    • Mocking cannot rescue

    View Slide

  4. Does your App Architecture supports unit
    testing ?
    • Search for the testing tools
    • Forgetting the need

    View Slide

  5. Where to start ?
    • Clean architecture
    • VIPER
    • Force fitting in to Android
    • clean-swift

    View Slide

  6. Android Clean Code

    View Slide

  7. Lets spilt the Monolithic Activity
    • Retrieve the data
    • Modify the model data
    according to viewmodel
    needs
    • Handle UI specific work
    • How they talk to each
    other ?
    • Interfaces
    • Unidirectional Flow

    View Slide

  8. Activity

    View Slide

  9. Activity Unit Testing
    • Using Roboletric to
    simulate the Activity
    • Canary test
    • How do we test the
    Activity without
    Interactor?

    View Slide

  10. Activity Unit Testing

    View Slide

  11. Activity Unit Testing

    View Slide

  12. Interactor
    • Interactor
    retrieves the
    data from
    data sources
    like the local
    DB, web
    service, and
    content
    provider.

    View Slide

  13. Why we need Worker ?
    • If the task is simpler- No
    need
    • If you foresee that the
    task can be reused
    somewhere else in the
    app, move it to the
    Worker.

    View Slide

  14. Testing Interactor
    • How to test
    Interactor
    with out
    Worker &
    Presenter

    View Slide

  15. Testing Interactor

    View Slide

  16. Presenter
    • Reduce the Model to VM

    View Slide

  17. Presenter

    View Slide

  18. Testing Presenter
    • FIRST
    • Fast
    • Isolated
    • Repeatable
    • Self-Validating
    • Timely

    View Slide

  19. Router
    • Determines the next
    screen
    • Pass the needed data to
    the next screen

    View Slide

  20. Testing Router

    View Slide

  21. Configurator

    View Slide

  22. Scaffolding

    View Slide

  23. What’s next?
    • Predictability
    • Developers can work across the technologies
    • Contribute
    • Refine

    View Slide