Slide 1

Slide 1 text

Ingredients for a 
 healthy codebase Romain Piel

Slide 2

Slide 2 text

@_rpiel Romain Piel at Songkick

Slide 3

Slide 3 text

Songkick

Slide 4

Slide 4 text

Architecture of an Android app?

Slide 5

Slide 5 text

Let me tell you a story…

Slide 6

Slide 6 text

John No knowledge in Android dev

Slide 7

Slide 7 text

Let’s build an Android app ! John No knowledge in Android dev

Slide 8

Slide 8 text

John MyActivity ApiManager

Slide 9

Slide 9 text

John MyActivity ApiManager MyActivity

Slide 10

Slide 10 text

John MyActivity ApiManager MyApplication MyActivity

Slide 11

Slide 11 text

John MyActivity ApiManager MyApplication MyActivity DbManager

Slide 12

Slide 12 text

Mary New hire !

Slide 13

Slide 13 text

Mary This codebase looks so old! Let’s add change a few things… New hire !

Slide 14

Slide 14 text

MyActivity ApiManager MyApplication MyActivity DbManager Mary John

Slide 15

Slide 15 text

MyActivity ApiManager MyApplication MyActivity DbManager Retrofit Mary John

Slide 16

Slide 16 text

MyActivity ApiManager MyApplication MyActivity DbManager Retrofit Mary NewFeature Activity NewFeature Fragment John

Slide 17

Slide 17 text

MyActivity ApiManager MyApplication MyActivity DbManager Retrofit Mary NewFeature Activity NewFeature Fragment RxJava John

Slide 18

Slide 18 text

App Mary John

Slide 19

Slide 19 text

App Mary John

Slide 20

Slide 20 text

App Mary John No idea where this crash is coming from

Slide 21

Slide 21 text

Francis New hire !

Slide 22

Slide 22 text

OMG ! Zero test ?! Francis New hire !

Slide 23

Slide 23 text

Unit tests App Mary John Francis

Slide 24

Slide 24 text

Unit tests App Mary John Francis Hard to catch up with the coverage & too many Android dependencies

Slide 25

Slide 25 text

App Mary Francis UI tests John

Slide 26

Slide 26 text

App Mary Francis UI tests Interactions with outside world = flaky tests John

Slide 27

Slide 27 text

Mary John Francis App

Slide 28

Slide 28 text

Mary John Francis App 1 year later…

Slide 29

Slide 29 text

App

Slide 30

Slide 30 text

App Dependant

Slide 31

Slide 31 text

App Dependant • UI and business logic

Slide 32

Slide 32 text

App Dependant • UI and business logic • Codebase and external libs

Slide 33

Slide 33 text

App Dependant Hard to test

Slide 34

Slide 34 text

App Dependant Hard to test Hard to maintain

Slide 35

Slide 35 text

Another approach

Slide 36

Slide 36 text

Systems should be • Independent of Frameworks • Testable • Independent of UI • Independent of Database • Independent of any external agency Clean architecture

Slide 37

Slide 37 text

Let’s consider this problem

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

Data layer

Slide 41

Slide 41 text

Domain layer Data layer

Slide 42

Slide 42 text

Presentation layer Domain layer Data layer

Slide 43

Slide 43 text

Data layer Domain layer Data layer

Slide 44

Slide 44 text

Data layer Domain layer Data layer

Slide 45

Slide 45 text

Data layer : repository pattern ArtistRepository Retrofit client Db client

Slide 46

Slide 46 text

Domain layer Presentation layer Domain layer Data layer

Slide 47

Slide 47 text

Domain layer

Slide 48

Slide 48 text

Domain layer • Orchestrates the flow of data with “use cases”

Slide 49

Slide 49 text

Domain layer • Orchestrates the flow of data with “use cases” • Offers its services to the presentation layer

Slide 50

Slide 50 text

Domain layer • Orchestrates the flow of data with “use cases” • Offers its services to the presentation layer • Pure Java module • No Android UI dependencies • No dependency to external source (db, content provider, shared preferences…)

Slide 51

Slide 51 text

Presentation layer

Slide 52

Slide 52 text

Slide 53

Slide 53 text

• Tied to the view lifecycle

Slide 54

Slide 54 text

• Tied to the view lifecycle • Receives user inputs

Slide 55

Slide 55 text

• Tied to the view lifecycle • Receives user inputs • Associated to specific view layouts (ListActivity)

Slide 56

Slide 56 text

• Tied to the view lifecycle • Receives user inputs • Associated to specific view layouts (ListActivity) Your very first Android Activity will be likely to be huge and untestable

Slide 57

Slide 57 text

View Presentation layer Domain layer Data Interaction

Slide 58

Slide 58 text

View Presentation layer Domain layer Data Interaction

Slide 59

Slide 59 text

[ ] View Presentation layer Domain layer Data Interaction

Slide 60

Slide 60 text

Presentation layer View Presenter Model Data Interaction

Slide 61

Slide 61 text

View Presenter Model Presentation layer interface SearchPresenter {
 void searchArtist(String searchTerm);
 void clickArtist(Artist artist);
 } interface SearchView {
 void showProgress();
 void hideProgress();
 void showArtists(List artists);
 }

Slide 62

Slide 62 text

Model vs. View model class Artist {
 String displayName;
 String uri;
 String id;
 LocalDate onTourUntil;
 }

Slide 63

Slide 63 text

class Artist {
 String displayName;
 String uri;
 String id;
 LocalDate onTourUntil;
 } Model vs. View model

Slide 64

Slide 64 text

class ArtistViewModel {
 String name; boolean isOnTour; } Model vs. View model

Slide 65

Slide 65 text

class ArtistViewModel {
 String name; boolean isOnTour; } public class ArtistViewHolder extends ViewHolder {
 
 @Bind(R.id.artist_name)
 TextView artistName; @Bind(R.id.on_tour)
 TextView onTour;
 
 public ArtistViewHolder(View itemView) {
 super(itemView);
 ButterKnife.bind(this, itemView);
 }
 
 @Override
 public void bind(ArtistViewModel viewModel) {
 artistName.setText(artistViewModel.name);
 onTour.setVisibility(artistViewModel.isOnTour ? View.VISIBLE : View.GONE );
 }
 }

Slide 66

Slide 66 text

Presentation layer interface SearchPresenter {
 void searchArtist(String searchTerm);
 void clickArtist(ArtistViewModel artist);
 } interface SearchView {
 void showProgress();
 void hideProgress();
 void showArtists(List artists);
 } View Presenter View model

Slide 67

Slide 67 text

Presentation layer class SearchFragment extends Fragment implements SearchView {
 
 SearchPresenter searchPresenter;
 
 @Override
 void showProgress() {
 // ...
 }
 
 @Override
 void hideProgress() {
 // ...
 }
 
 @Override
 void showArtists(List artists) {
 // ...
 } {...}
 }

Slide 68

Slide 68 text

Presentation layer class SearchFragment extends Fragment implements SearchView {
 
 SearchPresenter searchPresenter;
 {...} 
 void onItemClick(ArtistViewModel artist) {
 searchPresenter.clickArtist(artist); }
 
 void search(String searchTerm) {
 searchPresenter.searchArtist(searchTerm); }
 }

Slide 69

Slide 69 text

Presentation layer Domain layer Data layer

Slide 70

Slide 70 text

Communication Presentation layer Domain layer Data layer Subscriber Observable Observable Rx all the things!

Slide 71

Slide 71 text

interface ArtistRepository {
 Observable> search(String searchTerm);
 } Data layer Communication Rx all the things!

Slide 72

Slide 72 text

interface ArtistRepository {
 Observable> search(String searchTerm);
 } Domain layer Data layer interface SearchArtistUseCase {
 void subscribe(Subscriber subscriber); void unsubscribe();
 } Communication Rx all the things!

Slide 73

Slide 73 text

interface ArtistRepository {
 Observable> search(String searchTerm);
 } Presentation layer Domain layer Data layer interface SearchArtistUseCase {
 void subscribe(Subscriber subscriber); void unsubscribe();
 } interface SearchPresenter {
 void searchArtist(String searchTerm); void onDestroy();
 } Communication Rx all the things!

Slide 74

Slide 74 text

Structure Dagger

Slide 75

Slide 75 text

Structure Application
 component Dagger

Slide 76

Slide 76 text

Structure Application
 component Application modules Repositories Dagger

Slide 77

Slide 77 text

Structure Application
 component Activity component Application modules Repositories Dagger

Slide 78

Slide 78 text

Structure Application
 component Activity component Application modules Repositories Activity modules Activity Dagger

Slide 79

Slide 79 text

Structure Application
 component Activity component Application modules Repositories Fragment modules Presenter Use cases Activity modules Activity Dagger

Slide 80

Slide 80 text

Structure

Slide 81

Slide 81 text

Structure “Program to an interface, not an implementation” — Erich Gamma

Slide 82

Slide 82 text

Structure “Program to an interface, not an implementation” — Erich Gamma • Decouple the client from the implementation

Slide 83

Slide 83 text

Structure “Program to an interface, not an implementation” — Erich Gamma • Decouple the client from the implementation • Defines the vocabulary of the collaboration

Slide 84

Slide 84 text

Structure “Program to an interface, not an implementation” — Erich Gamma • Decouple the client from the implementation • Defines the vocabulary of the collaboration • Easy to test

Slide 85

Slide 85 text

Testing Presentation layer Domain layer Data layer Unit SearchUseCase ArtistRepository SearchPresenter SearchFragment

Slide 86

Slide 86 text

Testing Presentation layer Domain layer Data layer Unit SearchUseCase ArtistRepository SearchPresenter SearchFragment Testable without Robolectric

Slide 87

Slide 87 text

Testing Presentation layer Domain layer Data layer Unit SearchUseCase ArtistRepository SearchPresenter SearchFragment Testable without Robolectric Testable with Robolectric

Slide 88

Slide 88 text

Testing Presentation layer Domain layer Data layer UI

Slide 89

Slide 89 text

Testing Presentation layer Domain layer Data layer UI .json

Slide 90

Slide 90 text

Testing Presentation layer Domain layer Data layer UI .json class ArtistRepositoryTestImpl {
 
 JsonLoader jsonLoader;
 
 Observable> search(String searchTerm) {
 return jsonLoader.load(“search/artist.json”);
 }
 }

Slide 91

Slide 91 text

Conclusion

Slide 92

Slide 92 text

Conclusion • Choose an architecture and stick with it

Slide 93

Slide 93 text

Conclusion • Choose an architecture and stick with it • Test while you code

Slide 94

Slide 94 text

Conclusion • Choose an architecture and stick with it • Test while you code • Always do what’s best for the codebase, not for the feature you’re implementing

Slide 95

Slide 95 text

Songkick We’re hiring!

Slide 96

Slide 96 text

Links • Uncle Bob’s clean architecture 
 http://blog.8thlight.com/uncle-bob/2012/08/13/the-clean-architecture.html
 • Fernando Cejas - Architecting Android…The clean way?
 http://fernandocejas.com/2014/09/03/architecting-android-the-clean-way
 https://github.com/android10/Android-CleanArchitecture
 • Martin Fowler - The repository pattern
 http://martinfowler.com/eaaCatalog/repository.html
 • Erich Gamma - Design Principles from Design Patterns
 http://www.artima.com/lejava/articles/designprinciples.html

Slide 97

Slide 97 text

Thank you