5 steps to clean your android code

22
5 Steps To Clean Your Android Code +Frankie Sardo

Upload: frankie-sardo

Post on 04-Jul-2015

1.164 views

Category:

Technology


2 download

DESCRIPTION

Most Android apps follow the same pattern: a RESTful third-party service provides information represented as JSON and XML, and your task is to present that information and to communicate user actions to the service. Designing and understanding the way data flows into your architecture usually result in a much cleaner codebase. Here you'll be presented 5 useful insights that will help you write code that is a pleasure to read, easy to test and fun to change.

TRANSCRIPT

Page 1: 5 Steps To Clean Your Android Code

5 Steps To Clean Your Android Code

+Frankie Sardo

Page 2: 5 Steps To Clean Your Android Code

Enchant me.Simplify my life.

Make me amazing.

Page 3: 5 Steps To Clean Your Android Code

Android UI Design Principles

Page 4: 5 Steps To Clean Your Android Code

Android Software Design Principles

?

Page 5: 5 Steps To Clean Your Android Code

Clean Code is UX for Software

Page 6: 5 Steps To Clean Your Android Code

#1

Model Objects

Page 7: 5 Steps To Clean Your Android Code

Automatic mapping is tempting

Page 8: 5 Steps To Clean Your Android Code

Keep you objects in shapeclass 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); }}

Page 9: 5 Steps To Clean Your Android Code

#2

External Services

Page 10: 5 Steps To Clean Your Android Code

Responses are unpredictable

Page 11: 5 Steps To Clean Your Android Code

Abstract over external APIs

Page 12: 5 Steps To Clean Your Android Code

#3

Business Logic

Page 13: 5 Steps To Clean Your Android Code

Concurrency is hard

Page 14: 5 Steps To Clean Your Android Code

Define your user storiesclass 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(); }

}

Page 15: 5 Steps To Clean Your Android Code

Keep modules loosely coupled

Page 16: 5 Steps To Clean Your Android Code

#4

Boilerplate code

Page 17: 5 Steps To Clean Your Android Code

Android code is verbose

Page 18: 5 Steps To Clean Your Android Code

Say what you meanclass 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); }}

Page 19: 5 Steps To Clean Your Android Code

#5

Unit test

Page 20: 5 Steps To Clean Your Android Code

Tests constrain your design

Page 21: 5 Steps To Clean Your Android Code

Practice makes perfect

Page 22: 5 Steps To Clean Your Android Code

Q&A Time

#gaagbtgithub/frankiesardo

novoda/blog