Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Loading in …3
×
1 of 36

Idiomatic Kotlin for Android

2

Share

Download to read offline

Refactoring and Kotlin-ising an Android FragmentPagerAdapter

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all

Idiomatic Kotlin for Android

  1. 1. Idiomatic Kotlin (with Android flavouring) Orion Health Auckland 24th June 2016
  2. 2. Idiomatic: Pronunciation: /ˌɪdɪəˈmatɪk/ ADJECTIVE 1 Using, containing, or denoting expressions that are natural to a native speaker
  3. 3. Or: Learning how to write Kotlin-y Kotlin instead of Java-y Kotlin
  4. 4. Example: FragmentPagerAdapter with 3 static pages
  5. 5. Java version DRY violation! Meaningful information
  6. 6. [ ] Objective (pseudo-code) • Pass just the meaningful info to a new adapter • Reduce noise/kill boilerplate • Don’t repeat ourselves
  7. 7. What is this as actual code? “A list of things that return Fragments” Java = “A list of objects that return Fragments” [ ]
  8. 8. Java attempt Introduce a SAM (Single Abstract Method) interface:
  9. 9. DRY
  10. 10. Let’s see the calling code (e.g. Activity with a ViewPager)…
  11. 11. • Pass just the meaningful info to a new adapter • Reduce noise/kill boilerplate • Don’t repeat ourselves Java version ✔ ✔ ❌
  12. 12. Java version • Achieved DRY but… • Far more boilerplate than the original! • And worse, boilerplate is in calling code rather than hidden in implementation (anonymous inner classes) • Conclusion: not worth it. Stick with adapter coupled to concrete fragments
  13. 13. Kotlin version
  14. 14. What is this as actual code? “A list of things that return Fragments” Kotlin = “A list of functions that return Fragments” [ ]
  15. 15. “A list of functions that return Fragments” What does look like in Kotlin?
  16. 16. Function that takes nothing and returns a Fragment:
  17. 17. List of functions that takes nothing and return a Fragment:
  18. 18. Lambda syntax to kill more boilerplate:
  19. 19. Type inference to kill more boilerplate:
  20. 20. Refactor constructor to accept a list of funcs
  21. 21. Replaced .get(n) call with index operator [n] Replaced explicit .invoke() call with parentheses
  22. 22. Curly braces and return statement replaced with =
  23. 23. Return types removed, they can be inferred!
  24. 24. Let’s see the calling code (e.g. Activity with a ViewPager)…
  25. 25. • Pass just the meaningful info to a new adapter • Reduce noise/kill boilerplate • Don’t repeat ourselves Kotlin version ✔ ✔ ✔
  26. 26. Recap • Function types - passing functions as parameters • Lambda (function literal) syntax { OnboardingFragment() } • Type inference • Indexing operator .get(n) -> [] • Invoke operator .invoke() -> ()
  27. 27. Conclusion • Reduce your (Java-imposed) tolerance to boilerplate. It’s even more of a code smell in Kotlin! • Lots of language tools to kill boilerplate! • Passing functions around is new territory, be creative
  28. 28. Matthew Clarke
 Oblig twitter handle: @kiwiandroiddev
 
 References:
 https://kotlinlang.org/docs/reference/operator- overloading.html Thank you

×