SlideShare a Scribd company logo
1 of 61
Download to read offline
Keeping it Clean:
Clean code, for dirty developers
- Rich King -
Senior Android Developer
100+ million installs
190 countries
100K+ lines of code
185 Activities
23 Services
Architecture
Start of development
ActivitiesLoadersNetwork
Some time passes
Services
ActivitiesLoadersNetwork Global
Cache
And then…..
Services
ActivitiesEvent BusNetwork Global
Cache
GCM
Global
Listener
DB
Technical Debt
Time
Debt
So, what is Clean Architecture?
“ A GOOD ARCHITECTURE
EMPHASIZES THE USE-CASES
AND DECOUPLES THEM FROM
PERIPHERAL CONCERNS
— Robert C. Martin
Clean Architecture
• Coined by Robert C. Martin
• Combination of various ideas
- Hexagonal Architecture (a.k.a. Ports and Adapters)
- Onion Architecture
- Screaming Architecture
- Data, context and interaction paradigm
- Boundary, controller entity objects
- Single responsibility principle
https://8thlight.com/blog/uncle-bob/2012/08/13/the-clean-architecture.html
Clean Architecture
Key points:
1. Use cases
2.Structure
3.Dependancies
4.Models
5.Testable
Use cases
• Use cases capture business rules
Send
Message
Bob Alice
What does it do?
• Structure should indicate what the application is, not how it
does it.
What does it do?
• Structure should indicate what the application is, not how it
does it.
com.myapp
activities
services
presenters
content providers
views
What does it do?
• Structure should indicate what the application is, not how it
does it.
chat
conversations
com.myapp
Dependencies
UI
Business Logic
Data
Infrastructure
Dependencies
Use Cases
PresentersU
I
H
TTP
D
B
C
ontrollers
Services
Dependencies
Inversion of Control
Class A Class B
Dependency
Inversion of Control
Class A Class B
Injected
Inversion of Control
Send
Message
Injected
Inversion of Control
Send
Message
Carrier
Pigeon
Injected
Inversion of Control
Send
Message
HTTP
Injected
Why different models?
Why different models?
Use Case Model
•message
•id
•from
Data Model
•message
•id
•from
•cacheTime
View Model
•message
•id
•from
•itemSpacing
Clean Architecture
Presenter
View
Gateway
Impl
Memory
Cache
Disk
Cache
Network
Gateway
<I>
Use Case
Entity
Entity
Entity
Clean Architecture
Gateway
Use Case
Entity
Entity
Entity
<I>
Clean Architecture
Presenter
View
Gateway
Impl
Memory
Cache
Disk
Cache
Network
Gateway
<I>
Use Case
Entity
Entity
Entity
Clean Architecture
Presenter
View
Model
Clean Architecture
Presenter
View
Gateway
Impl
Memory
Cache
Disk
Cache
Network
Gateway
<I>
Use Case
Entity
Entity
Entity
Clean Architecture
Gateway
Impl
Memory
Cache
Disk
Cache
Network
Clean Architecture
Presenter
View
Gateway
Impl
Memory
Cache
Disk
Cache
Network
Gateway
<I>
Use Case
Entity
Entity
Entity
Clean Architecture
List of
Messages
Open
Conversation
Presenter
View
Gateway
Impl
Memory
Cache
Disk
Cache
Network
Gateway
<I>
Use Case
Entity
Entity
Entity
Bob
Implementation Details
interface MessageGateway {
List<Message> get(String conversationId);
int send(String conversationId, String to, String from,
String message);
boolean delete(String messageId);
void subscribe(Callback callback);
}
Gateway
class GetMessages {
...
List<M> execute(conversationId) {
return mMessageGateway.get(conversationId);
}
}
Use cases
class SendMessage {
...
int execute(conversationId, ...) {
int result = mMessageGateway.send(...);
if(result == SUCCESS)
mConversationGateway.update(conversationId);
return result;
}
}
Use cases
class SendMessage {
...
int execute(conversationId, ...) {
int result = mMessageGateway.send(...);
if(result == SUCCESS)
mConversationGateway.update(conversationId);
return result;
}
}
Use cases
class SendMessage {
...
int execute(conversationId, ...) {
int result = mMessageGateway.send(...);
if(result == SUCCESS)
mConversationGateway.update(conversationId);
return result;
}
}
Use cases
class MessageScreenPresenter {
...
void onSendMessage(message) {
int result = mSendMessage.execute(...);
switch (result) {
case ERROR:
mView.displayError(...)
...
}
}
Presentation
MainThread
RxJava
• Simple to switch threads
• Transformations are convenient
• Comes with a cost
https://github.com/ReactiveX/RxJava
mExecutor.post(new Runnable() {
void run() {
// do something
mHandler.post(new Runnable() {
void run() {
// publish result
}
});
}
});
!RxJava
mExecutor.post(() -> {
// do something
mHandler.post(() -> {
// publish result
}
}
}
}
!RxJava with lambdas
doSomething()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(result -> /* Handle Result */)
RxJava
Rx, Very Nice
interface MessageGateway {
Single<List<M>> get(...);
Single<Integer> send(...);
Single<Boolean> delete(...);
Observable<Update> subscribe(...);
}
Gateway
class GetMessages {
...
Single<List<M>> execute(conversationId) {
return mMessageRepo.get(conversationId);
}
}
Use cases
class SendMessage {
...
Single<Integer> execute(conversationId, ...) {
return mMessageGateway.send(…)
.doOnNext(r -> if(r == SUCCESS)
mConversationGateway.update(conversationId);)
}
}
Use cases
class MessageScreenPresenter {
...
void onStart() {
mSendMessage.execute(mConversationId)
.observeOn(AndroidSchedulers.mainThread())
.filter(result -> result == ERROR)
.subscribe(mView::displayError);
...
}
}
Presentation
class SomeGatewayImpl {
Single<Result> load(…) {
return loadFromMemory()
.flatMap(r -> r != null ?
Single.just(r) : loadFromDB());
}
}
Caching simplified
class SomeGatewayImpl {
Single<Result> load(…) {
return loadFromMemory()
.flatMap(r -> r != null ?
Single.just(r) : loadFromDB());
}
}
Caching simplified
class SomeGatewayImpl {
Single<Result> load(…) {
return loadFromMemory()
.flatMap(r -> r != null ?
Single.just(r) : loadFromDB());
}
}
Caching simplified
class SomeGatewayImpl {
Single<Result> load(…) {
return loadFromMemory()
.flatMap(r -> r != null ?
Single.just(r) : loadFromDB());
}
}
Caching simplified
class SomeGatewayImpl {
Single<Result> load(…) {
return loadFromMemory()
.flatMap(r -> r != null ?
Single.just(r) : loadFromDB());
}
}
Caching simplified
Was it worth it?
• Boilerplate
• Learning curve
• Testing
• Everything has a place
• Decisions can be postponed
• Implementations are replaceable
How can you do it?
• Start small
• Discuss and experiment
• Tech Talks
• Code reviews
Cheers!
github.com/badoo/Chateau
techblog.badoo.com
github.com/kingamajick
@kingamajick

More Related Content

What's hot

A Separation of Concerns: Clean Architecture on Android
A Separation of Concerns: Clean Architecture on AndroidA Separation of Concerns: Clean Architecture on Android
A Separation of Concerns: Clean Architecture on Android
Outware Mobile
 

What's hot (20)

SOLID Principles and The Clean Architecture
SOLID Principles and The Clean ArchitectureSOLID Principles and The Clean Architecture
SOLID Principles and The Clean Architecture
 
Clean Architecture
Clean ArchitectureClean Architecture
Clean Architecture
 
Clean architecture with asp.net core
Clean architecture with asp.net coreClean architecture with asp.net core
Clean architecture with asp.net core
 
Clean architecture - Protecting the Domain
Clean architecture - Protecting the DomainClean architecture - Protecting the Domain
Clean architecture - Protecting the Domain
 
Anatomy of a Spring Boot App with Clean Architecture - Spring I/O 2023
Anatomy of a Spring Boot App with Clean Architecture - Spring I/O 2023Anatomy of a Spring Boot App with Clean Architecture - Spring I/O 2023
Anatomy of a Spring Boot App with Clean Architecture - Spring I/O 2023
 
A Separation of Concerns: Clean Architecture on Android
A Separation of Concerns: Clean Architecture on AndroidA Separation of Concerns: Clean Architecture on Android
A Separation of Concerns: Clean Architecture on Android
 
Hexagonal architecture for java applications
Hexagonal architecture for java applicationsHexagonal architecture for java applications
Hexagonal architecture for java applications
 
Clean pragmatic architecture @ devflix
Clean pragmatic architecture @ devflixClean pragmatic architecture @ devflix
Clean pragmatic architecture @ devflix
 
Hexagonal architecture with Spring Boot
Hexagonal architecture with Spring BootHexagonal architecture with Spring Boot
Hexagonal architecture with Spring Boot
 
Hexagonal Architecture
Hexagonal ArchitectureHexagonal Architecture
Hexagonal Architecture
 
Hexagonal architecture
Hexagonal architectureHexagonal architecture
Hexagonal architecture
 
The Art of Unit Testing - Towards a Testable Design
The Art of Unit Testing - Towards a Testable DesignThe Art of Unit Testing - Towards a Testable Design
The Art of Unit Testing - Towards a Testable Design
 
MVVM & RxSwift
MVVM & RxSwiftMVVM & RxSwift
MVVM & RxSwift
 
Hexagonal architecture with Spring Boot
Hexagonal architecture with Spring BootHexagonal architecture with Spring Boot
Hexagonal architecture with Spring Boot
 
Clean architecture on android
Clean architecture on androidClean architecture on android
Clean architecture on android
 
Domain Driven Design (DDD)
Domain Driven Design (DDD)Domain Driven Design (DDD)
Domain Driven Design (DDD)
 
Clean code
Clean code Clean code
Clean code
 
Clean code
Clean codeClean code
Clean code
 
ASP.NET MVC Presentation
ASP.NET MVC PresentationASP.NET MVC Presentation
ASP.NET MVC Presentation
 
Clean Architecture Essentials - Stockholm Software Craftsmanship
Clean Architecture Essentials - Stockholm Software CraftsmanshipClean Architecture Essentials - Stockholm Software Craftsmanship
Clean Architecture Essentials - Stockholm Software Craftsmanship
 

Viewers also liked

Design pattern in android
Design pattern in androidDesign pattern in android
Design pattern in android
Jay Kumarr
 
Lightning Talk - Clean Architecture and Design
Lightning Talk - Clean Architecture and DesignLightning Talk - Clean Architecture and Design
Lightning Talk - Clean Architecture and Design
Deivison Sporteman
 
Clean architecture: Android
Clean architecture: AndroidClean architecture: Android
Clean architecture: Android
intive
 
Clean Architecture by Andrzej Bednarz
Clean Architecture by Andrzej BednarzClean Architecture by Andrzej Bednarz
Clean Architecture by Andrzej Bednarz
NetworkedAssets
 
Onion Architecture
Onion ArchitectureOnion Architecture
Onion Architecture
matthidinger
 

Viewers also liked (20)

Design pattern in android
Design pattern in androidDesign pattern in android
Design pattern in android
 
Android Clean Architecture for Dummies
Android Clean Architecture for DummiesAndroid Clean Architecture for Dummies
Android Clean Architecture for Dummies
 
Lightning Talk - Clean Architecture and Design
Lightning Talk - Clean Architecture and DesignLightning Talk - Clean Architecture and Design
Lightning Talk - Clean Architecture and Design
 
Clean architecture: Android
Clean architecture: AndroidClean architecture: Android
Clean architecture: Android
 
Clean Architecture by Andrzej Bednarz
Clean Architecture by Andrzej BednarzClean Architecture by Andrzej Bednarz
Clean Architecture by Andrzej Bednarz
 
Android Design Principles and Popular Patterns
Android Design Principles and Popular PatternsAndroid Design Principles and Popular Patterns
Android Design Principles and Popular Patterns
 
Onion Architecture
Onion ArchitectureOnion Architecture
Onion Architecture
 
Is Activity God? ~ The MVP Architecture ~
Is Activity God? ~ The MVP Architecture ~Is Activity God? ~ The MVP Architecture ~
Is Activity God? ~ The MVP Architecture ~
 
Effective Android UI - English
Effective Android UI - EnglishEffective Android UI - English
Effective Android UI - English
 
Android cleanarchitecture
Android cleanarchitectureAndroid cleanarchitecture
Android cleanarchitecture
 
Skroutz Android MVP and Adapter Delegates presentation
Skroutz Android MVP and Adapter Delegates  presentationSkroutz Android MVP and Adapter Delegates  presentation
Skroutz Android MVP and Adapter Delegates presentation
 
Android Effective UI: Tips, Tricks and Patterns
Android Effective UI: Tips, Tricks and PatternsAndroid Effective UI: Tips, Tricks and Patterns
Android Effective UI: Tips, Tricks and Patterns
 
Android Architecture MVP Pattern
Android Architecture MVP Pattern Android Architecture MVP Pattern
Android Architecture MVP Pattern
 
iOS advanced architecture workshop 3h edition
iOS advanced architecture workshop 3h editioniOS advanced architecture workshop 3h edition
iOS advanced architecture workshop 3h edition
 
Designing a participatory sensing game with children
Designing a participatory sensing game with childrenDesigning a participatory sensing game with children
Designing a participatory sensing game with children
 
Clean architecture - PHP
Clean architecture - PHPClean architecture - PHP
Clean architecture - PHP
 
Efficient HTTP Apis
Efficient HTTP ApisEfficient HTTP Apis
Efficient HTTP Apis
 
iks auf der gearconf 2012: Clean Code - Von der Lehre in den Alltag
iks auf der gearconf 2012: Clean Code - Von der Lehre in den Alltagiks auf der gearconf 2012: Clean Code - Von der Lehre in den Alltag
iks auf der gearconf 2012: Clean Code - Von der Lehre in den Alltag
 
Introduction to VIPER Architecture
Introduction to VIPER ArchitectureIntroduction to VIPER Architecture
Introduction to VIPER Architecture
 
Clean Architecture in Android. UPTech TechTalk
Clean Architecture in Android. UPTech TechTalkClean Architecture in Android. UPTech TechTalk
Clean Architecture in Android. UPTech TechTalk
 

Similar to Clean Architecture

Vertical Slicing Architectures
Vertical Slicing ArchitecturesVertical Slicing Architectures
Vertical Slicing Architectures
Victor Rentea
 
Google Developer Fest 2010
Google Developer Fest 2010Google Developer Fest 2010
Google Developer Fest 2010
Chris Ramsdale
 

Similar to Clean Architecture (20)

DDD, CQRS and testing with ASP.Net MVC
DDD, CQRS and testing with ASP.Net MVCDDD, CQRS and testing with ASP.Net MVC
DDD, CQRS and testing with ASP.Net MVC
 
Microservices Chaos Testing at Jet
Microservices Chaos Testing at JetMicroservices Chaos Testing at Jet
Microservices Chaos Testing at Jet
 
Patterns & Practices for Cloud-based Microservices
Patterns & Practices for Cloud-based MicroservicesPatterns & Practices for Cloud-based Microservices
Patterns & Practices for Cloud-based Microservices
 
Architecting Microservices in .Net
Architecting Microservices in .NetArchitecting Microservices in .Net
Architecting Microservices in .Net
 
Clean Architecture @ Taxibeat
Clean Architecture @ TaxibeatClean Architecture @ Taxibeat
Clean Architecture @ Taxibeat
 
Vertical Slicing Architectures
Vertical Slicing ArchitecturesVertical Slicing Architectures
Vertical Slicing Architectures
 
Low latency microservices in java QCon New York 2016
Low latency microservices in java   QCon New York 2016Low latency microservices in java   QCon New York 2016
Low latency microservices in java QCon New York 2016
 
Patterns and practices for real-world event-driven microservices by Rachel Re...
Patterns and practices for real-world event-driven microservices by Rachel Re...Patterns and practices for real-world event-driven microservices by Rachel Re...
Patterns and practices for real-world event-driven microservices by Rachel Re...
 
Patterns and practices for real-world event-driven microservices
Patterns and practices for real-world event-driven microservicesPatterns and practices for real-world event-driven microservices
Patterns and practices for real-world event-driven microservices
 
Google Developer Fest 2010
Google Developer Fest 2010Google Developer Fest 2010
Google Developer Fest 2010
 
How to write clean & testable code without losing your mind
How to write clean & testable code without losing your mindHow to write clean & testable code without losing your mind
How to write clean & testable code without losing your mind
 
TU Delft Presentation - Cloud & Serverless
TU Delft Presentation - Cloud & ServerlessTU Delft Presentation - Cloud & Serverless
TU Delft Presentation - Cloud & Serverless
 
Old code doesn't stink
Old code doesn't stinkOld code doesn't stink
Old code doesn't stink
 
Tdd,Ioc
Tdd,IocTdd,Ioc
Tdd,Ioc
 
angular2.0
angular2.0angular2.0
angular2.0
 
How and why we evolved a legacy Java web application to Scala... and we are s...
How and why we evolved a legacy Java web application to Scala... and we are s...How and why we evolved a legacy Java web application to Scala... and we are s...
How and why we evolved a legacy Java web application to Scala... and we are s...
 
Building Web Apps Sanely - EclipseCon 2010
Building Web Apps Sanely - EclipseCon 2010Building Web Apps Sanely - EclipseCon 2010
Building Web Apps Sanely - EclipseCon 2010
 
Js in quick books
Js in quick booksJs in quick books
Js in quick books
 
2017 Microservices Practitioner Virtual Summit: Microservices at Squarespace ...
2017 Microservices Practitioner Virtual Summit: Microservices at Squarespace ...2017 Microservices Practitioner Virtual Summit: Microservices at Squarespace ...
2017 Microservices Practitioner Virtual Summit: Microservices at Squarespace ...
 
Scalable Cloud Solutions with Node.js
Scalable Cloud Solutions with Node.jsScalable Cloud Solutions with Node.js
Scalable Cloud Solutions with Node.js
 

More from Badoo

Universal programming recipes​ - Ekaterina Trofimenko - Women In Technology
Universal programming recipes​ - Ekaterina Trofimenko - Women In TechnologyUniversal programming recipes​ - Ekaterina Trofimenko - Women In Technology
Universal programming recipes​ - Ekaterina Trofimenko - Women In Technology
Badoo
 

More from Badoo (6)

iOS Parallel Automation: run faster than fast — Viktar Karanevich — SeleniumC...
iOS Parallel Automation: run faster than fast — Viktar Karanevich — SeleniumC...iOS Parallel Automation: run faster than fast — Viktar Karanevich — SeleniumC...
iOS Parallel Automation: run faster than fast — Viktar Karanevich — SeleniumC...
 
Mobile Web Test Automation: to the Desktop! - Alexander Bayandin - Mobile Tes...
Mobile Web Test Automation: to the Desktop! - Alexander Bayandin - Mobile Tes...Mobile Web Test Automation: to the Desktop! - Alexander Bayandin - Mobile Tes...
Mobile Web Test Automation: to the Desktop! - Alexander Bayandin - Mobile Tes...
 
iOS Parallel Automation - Viktar Karanevich - Mobile Test Automation Meetup (...
iOS Parallel Automation - Viktar Karanevich - Mobile Test Automation Meetup (...iOS Parallel Automation - Viktar Karanevich - Mobile Test Automation Meetup (...
iOS Parallel Automation - Viktar Karanevich - Mobile Test Automation Meetup (...
 
Half way to clean architecture - Dmytro Voronkevych - Droidcon Berlin
Half way to clean architecture - Dmytro Voronkevych - Droidcon BerlinHalf way to clean architecture - Dmytro Voronkevych - Droidcon Berlin
Half way to clean architecture - Dmytro Voronkevych - Droidcon Berlin
 
Universal programming recipes​ - Ekaterina Trofimenko - Women In Technology
Universal programming recipes​ - Ekaterina Trofimenko - Women In TechnologyUniversal programming recipes​ - Ekaterina Trofimenko - Women In Technology
Universal programming recipes​ - Ekaterina Trofimenko - Women In Technology
 
How Badoo Saved $1M Switching to PHP7 - Nikolay Krapivnyy - PHPDay Verona 2016
How Badoo Saved $1M Switching to PHP7 - Nikolay Krapivnyy - PHPDay Verona 2016How Badoo Saved $1M Switching to PHP7 - Nikolay Krapivnyy - PHPDay Verona 2016
How Badoo Saved $1M Switching to PHP7 - Nikolay Krapivnyy - PHPDay Verona 2016
 

Recently uploaded

Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Cara Menggugurkan Kandungan 087776558899
 

Recently uploaded (6)

Leading Mobile App Development Companies in India (2).pdf
Leading Mobile App Development Companies in India (2).pdfLeading Mobile App Development Companies in India (2).pdf
Leading Mobile App Development Companies in India (2).pdf
 
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
 
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost LoverPowerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
 
9999266834 Call Girls In Noida Sector 52 (Delhi) Call Girl Service
9999266834 Call Girls In Noida Sector 52 (Delhi) Call Girl Service9999266834 Call Girls In Noida Sector 52 (Delhi) Call Girl Service
9999266834 Call Girls In Noida Sector 52 (Delhi) Call Girl Service
 
FULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCR
FULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCRFULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCR
FULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCR
 
BDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort ServiceBDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort Service
 

Clean Architecture