Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The (best) good practices on android

Karumi
November 03, 2016

The (best) good practices on android

After more than 5 years working on Android, I have found a set of common problems that Android teams run into. In this talk we will go through a list of tips & tricks that can help these teams to improve their code and raise the quality bar.

The best practices do not exist, but the good ones do, and you will go home knowing a ton of them.

Karumi

November 03, 2016
Tweet

More Decks by Karumi

Other Decks in Technology

Transcript

  1. The Best Good Practices in Android Jorge Juan Barroso Carmona

    [email protected] @flipper83 +JorgeJBarroso Android Guy Android GDE
  2. Sergio Gutierrez Senior Mobile Engineer Pedro Gomez Senior Mobile Engineer

    Alberto Gragera Technical Director Davide Mendolia Senior Full Stack Engineer Fran Toro Senior Mobile Engineer
  3. Adam Tornhill “Any fool can write code that a computer

    can understand. Good programmers write code that humans can understand.” Martin Fowler
  4. Adam Tornhill public class UserDataManager { private User getUser(Context context,

    String id) { Cursor<User> cursor = userContentProvider.getUser(context, id); if(cursor == null) { cursor = loadUserFromApi(id); } /*…*/ return user; } s TUPID
  5. Adam Tornhill stup ID public class UserDataSourceImp implements UserDataSource {

    private User getUser(String id) { User value = map.get(id); if(value != null && value.getType() == 2) { return value; } else { return null; } }
  6. Adam Tornhill stupI D observable .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber() {

    @Override public void onCompleted() {} @Override public void onError(Throwable e) {} @Override public void onNext(String value) {}); }
  7. Adam Tornhill SOLID public class MainActivity extends Activity { public

    void onCreate() { userApi.getUser(getExtraUserId()) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(this::RenderUser); } }
  8. Adam Tornhill SOLID public class MainActivity extends Activity { public

    void onCreate() { controller.getUser(this::RenderUser); } }
  9. Adam Tornhill S OLID public class FeedAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    public void onBindViewHolder (ViewHolder holder, int position) { FeedEntry feedEntry = entries.get(position); if (feedEntry instanceOf AdsEntry) { renderAds(feedEntry); } else { renderEntry(feedEntry); } }
  10. Adam Tornhill S OLID public class FeedAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    public void onBindViewHolder (ViewHolder holder, int position) { FeedEntry feedEntry = entries.get(position); FeedRow row = feedFactory.get(feedEntry); row.render(); }
  11. Adam Tornhill SOL ID public static interface AnimatorListener {
 void

    onAnimationStart (Animator animation);
 void onAnimationEnd (Animator animation);
 void onAnimationCancel (Animator animation); void onAnimationRepeat (Animator animation);
 }
  12. Adam Tornhill SOL ID public class EmptyAnimatorListener implements AnimatorListener {


    void onAnimationStart (Animator animation) {}
 void onAnimationEnd (Animator animation) {}
 void onAnimationCancel (Animator animation) {} void onAnimationRepeat (Animator animation) {}
 }