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

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
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Recently uploaded (20)

Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
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...
 
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...
 
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
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
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?
 
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...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
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
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 

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); } }