SlideShare a Scribd company logo
1 of 22
Download to read offline
5 Steps To Clean Your Android Code
+Frankie Sardo
Enchant me.
Simplify my life.
Make me amazing.
Android UI Design Principles
Android Software Design Principles
?
Clean Code is UX for Software
#1
Model Objects
Automatic mapping is tempting
Keep you objects in shape
class Event {
private final String name;
private final Location where;
private final DateRange duration;
private final Type type;
public Event(String name, Location where, DateRange when, Type type) {
this.name = name;
this.where = where;
this.duration = when;
this.type = type;
if (name.isEmpty()) {
throw new IllegalArgumentException("Event name is empty");
}
if (type == WORKSHOP && when.includes(27TH_MARCH )) {
throw new IllegalArgumentException("Workshops are scheduled after 27th of March");
}
}
public boolean clashesWith(Event otherEvent) {
return duration.overlaps(otherEvent.duration) &&
where.equals(otherEvent.where);
}
}
#2
External Services
Responses are unpredictable
Abstract over external APIs
#3
Business Logic
Concurrency is hard
Define your user stories
class SearchRepositoriesScenario implements Scenario {
final SearchRepositoriesPresentation presentation;
final GithubApi githubApi;
final SearchRepositoriesRequest request;
final Database database;
public SearchRepositoriesScenario(...) { ... }
public void run() {
presentation.showLoading();
String keyword = request.getKeyword();
Repositories repositories = githubApi.searchRepositories(keyword);
SearchRepositoriesResponse response = from(repositories);
database.persist(response);
presentation.updateList(response);
presentation.hideLoading();
}
}
Keep modules loosely coupled
#4
Boilerplate code
Android code is verbose
Say what you mean
class SearchRepositoriesActivity extends ListActivity {
@Inject SearchRepositoriesController controller;
@Icicle String query; // github.com/frankiesardo/icepick
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handleIntent(getIntent());
controller.displaySearch(query);
}
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
query = intent.getStringExtra(SearchManager.QUERY);
controller.startSearch(query);
}
}
public boolean onCreateOptionsMenu(Menu menu) {
return MenuHelper.inflate(getMenuInflater()).search()
.up(ParentActivity.class).help().into(menu);
}
}
#5
Unit test
Tests constrain your design
Practice makes perfect
Q&A Time
#gaagbt
github/frankiesardo
novoda/blog

More Related Content

Similar to 5 Steps To Clean Your Android Code

Intro to object oriented programming
Intro to object oriented programmingIntro to object oriented programming
Intro to object oriented programming
David Giard
 
10 ways to make your code rock
10 ways to make your code rock10 ways to make your code rock
10 ways to make your code rock
martincronje
 

Similar to 5 Steps To Clean Your Android Code (20)

Matteo Vaccari - TDD per Android | Codemotion Milan 2015
Matteo Vaccari - TDD per Android | Codemotion Milan 2015Matteo Vaccari - TDD per Android | Codemotion Milan 2015
Matteo Vaccari - TDD per Android | Codemotion Milan 2015
 
TDD and mobile development: some forgotten techniques, illustrated with Android
TDD and mobile development: some forgotten techniques, illustrated with AndroidTDD and mobile development: some forgotten techniques, illustrated with Android
TDD and mobile development: some forgotten techniques, illustrated with Android
 
Clean Software Design: The Practices to Make The Design Simple
Clean Software Design: The Practices to Make The Design SimpleClean Software Design: The Practices to Make The Design Simple
Clean Software Design: The Practices to Make The Design Simple
 
2019: A Local Hacking Odyssey - MITM attack against password manager @ BSides...
2019: A Local Hacking Odyssey - MITM attack against password manager @ BSides...2019: A Local Hacking Odyssey - MITM attack against password manager @ BSides...
2019: A Local Hacking Odyssey - MITM attack against password manager @ BSides...
 
Key points of good software programming
Key points of good software programmingKey points of good software programming
Key points of good software programming
 
Intro to object oriented programming
Intro to object oriented programmingIntro to object oriented programming
Intro to object oriented programming
 
Roslyn: el futuro de C# y VB.NET by Rodolfo Finochietti
Roslyn: el futuro de C# y VB.NET by Rodolfo FinochiettiRoslyn: el futuro de C# y VB.NET by Rodolfo Finochietti
Roslyn: el futuro de C# y VB.NET by Rodolfo Finochietti
 
Roslyn: el futuro de C#
Roslyn: el futuro de C#Roslyn: el futuro de C#
Roslyn: el futuro de C#
 
Practices For Becoming A Better Programmer
Practices For Becoming A Better ProgrammerPractices For Becoming A Better Programmer
Practices For Becoming A Better Programmer
 
Automated Discovery of Deserialization Gadget Chains
 Automated Discovery of Deserialization Gadget Chains Automated Discovery of Deserialization Gadget Chains
Automated Discovery of Deserialization Gadget Chains
 
Crash wars - The handling awakens
Crash wars - The handling awakensCrash wars - The handling awakens
Crash wars - The handling awakens
 
10 ways to make your code rock
10 ways to make your code rock10 ways to make your code rock
10 ways to make your code rock
 
The art of android hacking
The art of  android hackingThe art of  android hacking
The art of android hacking
 
The art of android hacking by Abhinav Mishra (0ctac0der)
The art of  android hacking by Abhinav Mishra (0ctac0der)The art of  android hacking by Abhinav Mishra (0ctac0der)
The art of android hacking by Abhinav Mishra (0ctac0der)
 
Combining the Security Risks of Native and Web Development: Hybrid Apps
Combining the Security Risks of Native and Web Development: Hybrid AppsCombining the Security Risks of Native and Web Development: Hybrid Apps
Combining the Security Risks of Native and Web Development: Hybrid Apps
 
Home Improvement: Architecture & Kotlin
Home Improvement: Architecture & KotlinHome Improvement: Architecture & Kotlin
Home Improvement: Architecture & Kotlin
 
Dev fest kyoto_2021-flutter_test
Dev fest kyoto_2021-flutter_testDev fest kyoto_2021-flutter_test
Dev fest kyoto_2021-flutter_test
 
React native: building shared components for Android and iOS
React native: building shared components for Android and iOSReact native: building shared components for Android and iOS
React native: building shared components for Android and iOS
 
Clean code
Clean codeClean code
Clean code
 
Crash Wars - The handling awakens
Crash Wars  - The handling awakensCrash Wars  - The handling awakens
Crash Wars - The handling awakens
 

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Recently uploaded (20)

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 

5 Steps To Clean Your Android Code

  • 1. 5 Steps To Clean Your Android Code +Frankie Sardo
  • 2. Enchant me. Simplify my life. Make me amazing.
  • 3. Android UI Design Principles
  • 4. Android Software Design Principles ?
  • 5. Clean Code is UX for Software
  • 8. Keep you objects in shape class Event { private final String name; private final Location where; private final DateRange duration; private final Type type; public Event(String name, Location where, DateRange when, Type type) { this.name = name; this.where = where; this.duration = when; this.type = type; if (name.isEmpty()) { throw new IllegalArgumentException("Event name is empty"); } if (type == WORKSHOP && when.includes(27TH_MARCH )) { throw new IllegalArgumentException("Workshops are scheduled after 27th of March"); } } public boolean clashesWith(Event otherEvent) { return duration.overlaps(otherEvent.duration) && where.equals(otherEvent.where); } }
  • 14. Define your user stories class SearchRepositoriesScenario implements Scenario { final SearchRepositoriesPresentation presentation; final GithubApi githubApi; final SearchRepositoriesRequest request; final Database database; public SearchRepositoriesScenario(...) { ... } public void run() { presentation.showLoading(); String keyword = request.getKeyword(); Repositories repositories = githubApi.searchRepositories(keyword); SearchRepositoriesResponse response = from(repositories); database.persist(response); presentation.updateList(response); presentation.hideLoading(); } }
  • 17. Android code is verbose
  • 18. Say what you mean class SearchRepositoriesActivity extends ListActivity { @Inject SearchRepositoriesController controller; @Icicle String query; // github.com/frankiesardo/icepick protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); handleIntent(getIntent()); controller.displaySearch(query); } protected void onNewIntent(Intent intent) { super.onNewIntent(intent); handleIntent(intent); } private void handleIntent(Intent intent) { if (Intent.ACTION_SEARCH.equals(intent.getAction())) { query = intent.getStringExtra(SearchManager.QUERY); controller.startSearch(query); } } public boolean onCreateOptionsMenu(Menu menu) { return MenuHelper.inflate(getMenuInflater()).search() .up(ParentActivity.class).help().into(menu); } }