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

Android Architectures and Clean Code

Avatar for Frankie Sardo Frankie Sardo
November 24, 2013

Android Architectures and Clean Code

Avatar for Frankie Sardo

Frankie Sardo

November 24, 2013

Other Decks in Technology

Transcript

  1. // manager made me do it // hack for release

    1.2.4 // this should never happen // TODO: fix me before Sunday // WTF???
  2. Object calisthenics - One level of indentation per method -

    Don’t use the ELSE keyword - Wrap all primitives and String - First class collections - One dot per line - Don’t abbreviate - Keep all entities small - No classes with more than two instance variables - No getters and setters
  3. Kill the boilerplate code Intent intent = new Intent(this, VideoActivity.class);

    intent.putExtra("video", mVideo); intent.setFlags(FLAG_ACTIVITY_SINGLE_TOP); this.startActivity(intent);
  4. Bonus project: Icepick @Icicle String username; public void onCreate(Bundle savedInstanceState)

    { super.onCreate(savedInstanceState); Bundles.restoreInstanceState(this, savedInstanceState); } public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); Bundles.saveInstanceState(this, outState); }
  5. Mutability class Event { @JsonName("o_name") void setName(String name) {...} @JsonName("D_address")

    void setAddress(String date) {...} @JsonName("valid_for") void setExpires(String duration) {...} }
  6. Mutability class Event { final String name; final Address address;

    final DateRange duration; Event(String name, Address address, DateRange duration) {...} static class Address {...} static class DateRange {...} }
  7. Mutability String doSomething() { StringBuffer buffer = new StringBuffer(); if

    (buffer.size() == 0) { buffer.append("("); } // Share a mutable instance. // Potential bugs will lurk here. return doSomethingElse(buffer); }
  8. Mutability String doSomething() { String partialResult = ""; if (partialResult.length()

    == 0) { partialResult += "("; } // String is immutable. // Sharing won't cause troubles to the caller. return doSomethingElse(partialResult); }
  9. Bonus project: clj-ds - Persistent Stack - Persistent Vector -

    Persistent Set - Persistent Map - … and many more!
  10. Sync flow On New day: fetch products and offers On

    App opening: sync basket, user data, orders On Delivery screen: sync available slots On Basket changed: sync basket
  11. The lean way Remix: - SyncAdapter + BG ThreadPoolExecutor -

    Loaders for each piece of info - In-memory mutable data - RxJava for composition (Basket + Products) - Separate syncing from displaying