$30 off During Our Annual Pro Sale. View Details »

Kotlin Best Practices by Étienne Caron

GDG Montreal
February 28, 2018

Kotlin Best Practices by Étienne Caron

GDG Montreal

February 28, 2018
Tweet

More Decks by GDG Montreal

Other Decks in Technology

Transcript

  1. Kotlin Guides & Best Practices

  2. Photo source info here Increase Developer Happiness?

  3. None
  4. Huffington Post - Flow https://goo.gl/KKjVqG

  5. Kaizen Approach : 1% Better Every Day https://goo.gl/C6VJcz “Compounding is

    the greatest mathematical discovery of all time.” — Albert Einstein
  6. /join #kotlin

  7. Photo source info here Short Term • Books on the

    Basics • Best Practices and Idioms • Style Guides • Android Extensions + KTX
  8. Official site - https://kotlinlang.org/ Books

  9. https://blog.philipphauer.de/idiomatic-kotlin-best-practices/

  10. None
  11. None
  12. Named Arguments instead of Fluent Setter

  13. apply() for Grouping Object Initialization

  14. Concisely Deal with Nullability Avoid if-null Checks

  15. Concisely Deal with Nullability Avoid if-type Checks

  16. Concisely Deal with Nullability Avoid not-null Assertions !!

  17. Consider let() Concisely Deal with Nullability

  18. safeLet(productIds, productVariantIds) { products, variants -> // Do some work

    } fun <T1 : Any, T2 : Any, R : Any> safeLet(p1: T1?, p2: T2?, block: (T1, T2) -> R?): R? { return if (p1 != null && p2 != null) block(p1, p2) else null } safeLet() Concisely Deal with Nullability
  19. Concise Mapping with Single Expression Functions

  20. object for Stateless Interface Implementations

  21. Destructuring

  22. https://kotlinlang.org/docs/reference/coding-conventions.html https://android.github.io/kotlin-guides/style.html

  23. Photo source info here TLDR; Use the code formatter. •

    No Tabs • No Wildcard Imports • Indentation 4 spaces • Line wrap at 100 chars
  24. None
  25. None
  26. None
  27. None
  28. None
  29. None
  30. None
  31. None
  32. None
  33. https://kotlinlang.org/docs/tutorials/android-plugin.html

  34. None
  35. https://github.com/android/android-ktx PREV IEW

  36. Photo source info here Long Term • Architecture Patterns •

    Functional Programming • Reactive Programming
  37. https://www.raywenderlich.com/168038/common-design-patterns-android-kotlin https://speakerdeck.com/karumi/architecture-patterns-in-practice-with-kotlin

  38. https://github.com/googlesamples/android-architecture

  39. None
  40. https://medium.freecodecamp.org/functional-programming-for-android-developers-part-1-a58d40d6e742

  41. Functional Programming Among other advantages, functional programming allows us to

    reduce side-effects, which in turn makes our code… • less error-prone, • easier to understand, • easier to test and • thread-safe. https://blog.philipphauer.de/idiomatic-kotlin-best-practices/
  42. Functional Programming In contrast to Java 8, Kotlin has way

    better support for functional programming: • Immutability: val for variables and properties, immutable data classes, copy() • Expressions: Single expression functions. if, when and try-catch are expressions. We can combine these control structures with other expressions concisely. • Function Types • Concise Lambda Expressions • Kotlin’s Collection API These features allow writing functional code in a safe, concise and expressive way. Consequently, we can create pure functions (functions without side-effects) more easily. https://blog.philipphauer.de/idiomatic-kotlin-best-practices/
  43. https://goo.gl/FCKCFm https://goo.gl/iGxum7 https://goo.gl/49qAXV https://goo.gl/JKw6Pw

  44. Photo source info here Books • Kotlin in Action •

    Kotlin for Android Developers • Android Development with Kotlin • Programming Kotlin • Reactive Programming in Kotlin • Mastering Android Development with Kotlin • Kotlin Blueprints • Kotlin Programming Cookbook
  45. Photo source info here Online Community •Android Kotlin Guides •Philipp

    Hauer's Idiomatic Kotlin Article •Kotlin Conf 2017 ◦ Huyen Tue Dao & Christina Lee's “Road to Kotlintown” ◦ Christina Lee's “Two Stones, One Bird”