Device fragmentation vs clean code

1,361 views

Published on

Published in: Engineering
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,361
On SlideShare
0
From Embeds
0
Number of Embeds
120
Actions
Shares
0
Downloads
15
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Device fragmentation vs clean code

  1. 1. Iordanis Giannakakis @iordanis_g Device Fragmentation vs Clean Code
  2. 2. Introduction • Iordanis “Jordan” Giannakakis • Android Team Lead at Shazam • @iordanis_g
  3. 3. How Shazam works
  4. 4. Running order • What is fragmentation & clean code? • Make the most of the Android platform • DIY: Compatible cross-version features • Handle complexity with MVP • Write tests that cover all devices • Q & A
  5. 5. Running order • What is fragmentation & clean code? • Make the most of the Android platform • DIY: Compatible cross-version features • Handle complexity with MVP • Write tests that cover all devices • Q & A
  6. 6. Device fragmentation
  7. 7. Distribution of OS versions (03/14) • KitKat only 2x as big as Froyo (2010) – 2.5% vs. 1.2% • Gingerbread still 20% (2010)
  8. 8. Screen variations
  9. 9. Variety of capabilities
  10. 10. Variety of capabilities
  11. 11. Does diversification matter? • Variety is the power of Android • Google Play Services updates • OS features as apps • Compatibility libraries
  12. 12. Bad code
  13. 13. Bad code • Big ‘If’ statements, heavily branched • Unmaintainable • Duplication • Mixed concerns • Untestable
  14. 14. Clean code
  15. 15. Clean code • Easy to change • Object model • Single responsibilities • Reusable • Testable
  16. 16. Code quality: Is it a concern really? • Short-term projects • Individually developed • Lean • A/B testing
  17. 17. Code quality: Is it a concern really? • Codebase gets bigger • Need to grow team • No tests • Code becomes legacy
  18. 18. Code quality at Shazam
  19. 19. Running order • What is fragmentation & clean code? • Make the most of the Android platform • DIY: Compatible cross-version features • Handle complexity with MVP • Write tests that cover all devices • Q & A
  20. 20. Resource System res/
  21. 21. Some resource types • Layouts • Drawables • Values – Attributes – Colours – Strings – Styles
  22. 22. Some resource qualifiers • Locale • Screen – Size – Density – Orientation – Ratio • Platform version
  23. 23. Customise UI
  24. 24. Switchable components
  25. 25. Switchable components
  26. 26. Code example: Resource System github.com/iordanis/androidCleanCode
  27. 27. What we have gained • Less code • Separated presentation to logic • Flexible UI
  28. 28. Running order • What is fragmentation & clean code? • Make the most of the Android platform • DIY: Compatible cross-version features • Handle complexity with MVP • Write tests that cover all devices • Q & A
  29. 29. API change SharedPreferences.Editor • commit() – slow, since v1 • apply() – fast, since v9
  30. 30. Compatibility libraries • Backport features to old OS versions • Version checking and graceful degradation • Same package structure, different namespace – android.app – android.support.v2.app • “Deprecated by design”
  31. 31. Compatibility libraries • Official Libraries – Support v4, v8, v13 – AppCompat v7 • 3rd party: – ActionbarSherlock – Nineoldandroids
  32. 32. Dependency Inject, Yourself (DIY) • Break hardcoded dependencies • What vs how • Behaviour vs implementation • Swappable dependencies for test and production runtimes
  33. 33. Without Dependency Injection Client Feature X Feature X
  34. 34. Dependency Injection Client Injector Feature X Interface
  35. 35. Drive for DIY • Fast • Simple • No magic necessary • Pure Java! • Run Android code on JVM for tests java.lang.RuntimeException: Stub!
  36. 36. Code example: SharedPreferences Compatibility github.com/iordanis/androidCleanCode
  37. 37. What we have gained • Eliminated ‘ifs’ from production code • Maintainability • Testable code • Reusable code • More users
  38. 38. Running order • What is fragmentation & clean code? • Make the most of the Android platform • DIY: Compatible cross-version features • Handle complexity with MVP • Write tests that cover all devices • Q & A
  39. 39. Capabilities example – Google Play Services
  40. 40. Activity lifecycle • Complex • Few callbacks relevant
  41. 41. Issues with presentation complexity • Compounds complexity of lifecycle • Testing presentation logic difficult • Unfulfilled dependencies when in JVM java.lang.RuntimeException: Stub! • Robolectric is the only option
  42. 42. The MVP pattern Presenter Model View
  43. 43. The MVP pattern, with Android Presenter Model View start stop Android
  44. 44. Drive for MVP • Make presentation logic testable • Avoid Android dependencies • No need to test the “slave” view • Avoid Robolectric
  45. 45. Code example: MVP github.com/iordanis/androidCleanCode
  46. 46. What we have gained • Separation of concerns • Testable presentation logic • Reusable code • Avoiding need for dependencies when running Android tests on JVM
  47. 47. Running order • What is fragmentation & clean code? • Make the most of the Android platform • DIY: Compatible cross-version features • Handle complexity with MVP • Write tests that cover all devices • Q & A
  48. 48. Test requirements • Expressive • Flexible • Fast • Helpful diagnostics
  49. 49. Testing Android apps • Unit • Integration • System
  50. 50. System tests • Instrumentation framework • Robotium & Espresso • gwen
  51. 51. Behaviour tests • Given: arrange • When: act • Then: assert
  52. 52. Example Given the server returns a track When the user taps the Shazam button Then the user sees the track result
  53. 53. gwen given(server).returnsATrack() when(user).tapsTheShazamButton() then(user).seesTheTrackResult() https://github.com/shazam/gwen
  54. 54. Phone behaviour
  55. 55. Tablet behaviour
  56. 56. Code example: System Tests github.com/iordanis/androidCleanCode
  57. 57. Test speed • All tests on all devices – not always necessary • Device clouds • Gradle • Maven
  58. 58. Spoon http://square.github.io/spoon/
  59. 59. Fork • Inspired by Spoon • Infinitely scalable – 1 test / 3secs • Pooled execution – Create pool for any device parameter
  60. 60. Fork
  61. 61. Diagnostics
  62. 62. Flexibility @SkipOnDevice( deviceType = TABLET_LARGE) @SkipOnDevice( playServices = NOT_AVAILABLE)
  63. 63. Summary • Relationship of fragmentation & clean code • How to make the most of Android • How to write compatible code cleanly • How to apply the MVP pattern • How to write awesome automation tests
  64. 64. Notes @iordanis_g +IordanisGiannakakis Code examples: github.com/iordanis/androidCleanCode Hiring...
  65. 65. References • Responsible Design For Android: https://leanpub.com/ResponsibleDesignAndroid-Part1 • 50 Android Hacks: http://www.manning.com/sessa/ • Google I/O 2012: Multi-Versioning Android User Interfaces: https://www.youtube.com/watch?v=amZM8oZBgfk • Android Dashboards: https://developer.android.com/about/dashboards/index.html • Android Fragmentation visualized: http://opensignal.com/reports/fragmentation-2013/
  66. 66. Questions?

×