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

#AndroidDev like Engineers

Olmo Gallegos
November 09, 2015

#AndroidDev like Engineers

Initiation-level speech for novice / intermediate developers who want to improve their Android Development process and follow some widely extended and recommended principles.

Olmo Gallegos

November 09, 2015
Tweet

More Decks by Olmo Gallegos

Other Decks in Technology

Transcript

  1. #AndroidDev like engineers Víctor Olmo Gallegos Hernández Mobile Developer -

    AppAndWeb Olmo Gallegos @voghDev mobiledevstories.com
  2. Motivation Engineers doing really bad code. (I know it because

    i’ve done it myself) Programming is not easy !! even less doing it well Result: Engineers with very strong base, but...
  3. Motivation Engineers doing really bad code. (I know it because

    i’ve done it myself) Programming is not easy !! even less doing it well Result: Engineers with very strong base, but... Coding like plumbers
  4. Some examples... 2k/3kLOC God Activities / Fragments Strongly coupled spaghetti

    code Deficient (or lack of) architecture Incorrect design patterns - Common antipatterns (Singleton invasion, Observer / SRP violations...) Context misuse (memory leaks) More info: see S.T.U.P.I.D.
  5. Result Very unmaintainable code Project lives for 6 months and

    has to be fully rebuilt Very hard to add a simple features, high chance of breaking another ones
  6. Single responsibility principle (SRP) Basic programming principle: classes that do

    just one thing. A class or module should have one, and only one, reason to change. “No side effects” - my class must not be affected by changes in another classes “First rule of functions: They should be small Second rule: They should be smaller than that” SOLID
  7. Open-closed principle (OCP) Classes should be open for extension, and

    closed for modification The “Five lines of code” example. We’ll see examples of this l8r. SOLID
  8. Liskov Substitution Principle (LSP) A class should be substitutable by

    instances of its derivatives without altering the correctness of the program. SOLID
  9. Interface Segregation Principle Interfaces should be as simple as possible

    “Clients should not be forced to depend upon interfaces that they do not use” Counterexample: PagerAdapter SOLID
  10. Design Patterns Singletons are DANGEROUS! Android SDK provides you various

    methods for managing your scopes. You probably won’t need to write this code The S in S.T.U.P.I.D. is for Singleton!
  11. Design Patterns Model-View-Presenter Model-View-Controller natural extension. The usage of the

    App is modeled as an interaction between two main actors: the View and the Presenter. Activities / Fragments / CustomViews implement the abstract View class (do not confuse with android.view.View) Presenter represents the events that can happen to the App (onItemClicks, onDataReceived, onCreate). View represents the reactions to these events (showLoading, hideLoading, showUsersList, showErrorMessage, showPushDialog).
  12. Design Patterns Model-View-Presenter Model-View-Controller natural extension. The usage of the

    App is modeled as an interaction between two main actors: the View and the Presenter. Activities / Fragments / CustomViews implement the abstract View class (do not confuse with android.view.View) Presenter represents the events that can happen to the App (onItemClicks, onDataReceived, onCreate). View represents the reactions to these events (showLoading, hideLoading, showUsersList, showErrorMessage, showPushDialog).
  13. Design Patterns Model-View-Presenter Sadly, we’re not talking about MVP in

    more depth (though we should, it is probably the most interesting pattern) “We’re leaving the best pig without acorns”
  14. ➙ Naming conventions ➙ CustomViews + styleables ➙ Styles ➙

    Package distribution ➙ One Model per subsystem! ➙ Android studio features ➙ Mastering gradle ➙ Some interesting external libraries ◆ RecyclerView ◆ Picasso / Glide ◆ butterknife ◆ Android design library (Material stuff like Toolbar, CoordinatorLayout, Fab’s) Framework-dependent aspects ◆ retrofit2 ◆ dagger2 ◆ renderers ◆ DBFlow
  15. Widgets: @+id/customers_listView @+id/customers_tv_name @+id/customer_iv_picture Naming conventions Create your own naming

    conventions, based on existing Android ones! Activities: YourOwnNameActivity (e.g. CustomerDetailActivity) Fragments: YourOwnNameFragment (e.g. CustomersFragment) CustomViews: YourOwnNameView (e.g. CustomerProfileView) Layouts: activity_your_own_name.xml fragment_your_own_name.xml view_your_own_name.xml
  16. Naming conventions: Pros • Runtime errors can be brought to

    compilation time • Helps detecting code smells (if your activity/fragment/view doesn’t fit in the naming conventions, you’re probably doing it wrong) • Avoids NPEs and other errors due to name clashes (R.id.title) Examples can be found on the next slides...
  17. CustomViews Excellent way of dividing your responsibilities (cleaning your code)

    Three main ways of implementing CustomViews (there are more) Very likely to do Open/Closed code! 1. Extending from Widgets (TextView, ImageView, EditText…) 2. Extending from Layout / ViewGroup, then inflating XML layout 3. Extending from View and overriding onDraw to paint on canvas
  18. Extending from Layout/ViewGroup, then inflating XML layout (you can also

    add your widgets programatically) CustomViews
  19. • Do extensive use of styles • Separate your styles

    into different .xml files instead of one styles.xml ◦ res/values/button_styles.xml ◦ res/values/textview_styles.xml ◦ res/values/mycustomview_styles.xml • Also separate your styleables into different files (one file per CustomView) Styles
  20. Like naming and styles, have your own package distribution This

    could be an example ▪ datasource ▹ api ◦ parse ◦ retrofit ▹ mock ▹ db ◦ model ▪ global ▹ di ▹ util ▹ model ▹ MyApplication.java Package distribution ▪ interactor ▹ impl ▪ ui ▹ activity ▹ fragment ▹ view ▹ presenter
  21. Android Studio is your friend • Shortcuts ◦ Extract style

    ◦ Move line up / down ◦ Duplicate line ◦ Alt+Enter > Implement abstract class ◦ Alt+Enter > Create field / local var / method ◦ Alt+Enter > declare public / protected ◦ and much much more... • Live templates (use them with responsibility!) Android Studio Features
  22. Libraries from square • retrofit / retrofit2 • dagger •

    otto • picasso • butterknife Other libs from Google • dagger2 • RecyclerView • Android design library (CoordinatorLayout, Toolbar, Fabs…) External libraries • Palette
  23. More useful libs • Glide • renderers • google/exoplayer •

    DBFlow Libs ”made in Granada” :-) • SectionedRecyclerView (Tomás Ruiz-López) • WheelIndicatorView (David Lázaro) Thanks guys for the awesome work! External libraries
  24. More useful libs • Glide • renderers • google/exoplayer •

    DBFlow Libs ”made in Granada” :-) • SectionedRecyclerView (Tomás Ruiz-López) • WheelIndicatorView (David Lázaro) Thanks guys for the awesome work! External libraries
  25. “Hate the code you wrote 6 months ago” Continuous improvement

    is the key Thanks to those who inspired this speech and made this possible