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

"Bring your Kotlin to Work" Day

"Bring your Kotlin to Work" Day

Guillermo Orellana

September 22, 2017
Tweet

More Decks by Guillermo Orellana

Other Decks in Programming

Transcript

  1. "Bring Your Kotlin To Work" Day September 2017 Maybe

  2. Guillermo Orellana Senior Keyboard Smasher github.com/wiyarmir https://www.linkedin.com/in/guillermoorellana @wiyarmir

  3. None
  4. – bringyourdogtoworkday.co.uk “[…] the presence of pets can substantially reduce

    a person’s stress level in the workplace. Increased job satisfaction, team co-operation and morale have all been reported in employees that spend the workday with their pets”
  5. – me “[…] the presence of Kotlin can substantially reduce

    a person’s stress level in the workplace. Increased job satisfaction, team co-operation and morale have all been reported in employees that spend the workday with Kotlin”
  6. None
  7. None
  8. None
  9. None
  10. The case for Kotlin

  11. Don’t stay in your Java bubble

  12. None
  13. Learn the basics

  14. val var

  15. Type Type?

  16. @NonNull private final String name = "Java"; private val name:

    String = "Kotlin"
  17. It's OK to write Java-ish Kotlin

  18. Improve by doing

  19. None
  20. Let the IDE help you

  21. None
  22. None
  23. None
  24. Let other people help you

  25. None
  26. Let the Kotlin community help you

  27. None
  28. Kotlin won't make bad code good

  29. The case against Kotlin

  30. None
  31. Fighting the compiler

  32. @JvmField @JvmStatic @JvmName @JvmSuppressWildcards @JvmWildcard @JvmOverloads

  33. None
  34. None
  35. Final by default

  36. None
  37. None
  38. None
  39. None
  40. None
  41. Style guide

  42. None
  43. ¯\_(ツ)_/¯

  44. Quizz time!

  45. class MarketDataViewModel { init { compositeDisposable.add(bindToMarketData()) } val compositeDisposable =

    CompositeDisposable() fun bindToMarketData(): Disposable = TODO() }
  46. None
  47. class MarketDataViewModel { val compositeDisposable = CompositeDisposable() init { compositeDisposable.add(bindToMarketData())

    } fun bindToMarketData(): Disposable = TODO() }
  48. class MarketDataViewModel { val compositeDisposable = CompositeDisposable() init { compositeDisposable.add(bindToMarketData())

    } val mapper = TODO() fun bindToMarketData(): Disposable = retrieveMarketData.getStream() .map(mapper) .subscribe() }
  49. RuntimeException

  50. class MarketDataViewModel { val compositeDisposable = CompositeDisposable() init { compositeDisposable.add(bindToMarketData())

    } val mapper = TODO() fun bindToMarketData(): Disposable = retrieveMarketData.getStream() .map(mapper) .subscribe() }
  51. class MarketDataViewModel { val compositeDisposable = CompositeDisposable() init { compositeDisposable.add(bindToMarketData())

    } val mapper = TODO() fun bindToMarketData(): Disposable = retrieveMarketData.getStream() .map(mapper) .subscribe() }
  52. class MarketDataViewModel { val compositeDisposable = CompositeDisposable() init { compositeDisposable.add(bindToMarketData())

    } val mapper = TODO() fun bindToMarketData(): Disposable = retrieveMarketData.getStream() .map(mapper) .subscribe() }
  53. class MarketDataViewModel { val compositeDisposable = CompositeDisposable() init { compositeDisposable.add(bindToMarketData())

    } val mapper = TODO() fun bindToMarketData(): Disposable = retrieveMarketData.getStream() .map(mapper) .subscribe() }
  54. class MarketDataViewModel { val compositeDisposable = CompositeDisposable() init { compositeDisposable.add(bindToMarketData())

    } val mapper = TODO() fun bindToMarketData(): Disposable = retrieveMarketData.getStream() .map(mapper) .subscribe() }
  55. RuntimeException

  56. class MarketDataViewModel { val compositeDisposable = CompositeDisposable() val mapper =

    TODO() init { compositeDisposable.add(bindToMarketData()) } fun bindToMarketData(): Disposable = retrieveMarketData.getStream() .map(mapper) .subscribe() }
  57. What I like from Kotlin

  58. Data Classes

  59. "POJOs"

  60. public class Balance { @NonNull private final BalanceType type; private

    final int value; public Balance(@NonNull BalanceType type, int value) { this.type = type; this.value = value; } @NonNull public BalanceType getType() { return type; } public int getValue() { return value;
  61. return type == balance.type; } @Override public int hashCode() {

    int result = type.hashCode(); result = 31 * result + value; return result; } @Override public String toString() { return "Balance{" + "type=" + type + ", value=" + value + '}'; } }
  62. data class Balance (val type: BalanceType, val value: Int)

  63. data class Balance ( val type: BalanceType = Invalid, val

    value: Int = 0 )
  64. Named parameters

  65. fun fromEntity(e: ModerationEntity) = ModerationModel( header = e.header, body =

    e.message, ctaPrimary = e.ctaPrimary, ctaSecondary = e.ctaSecondary, photos = e.photos )
  66. Sealed classes

  67. sealed class BalanceResult { data class Balance( val type: BalanceKind

    = Invalid, val value: Int = 0 ): BalanceResult() object BalanceError: BalanceResult() }
  68. Bonus points

  69. class MarketDataRepository( private val store: ReactiveStore<String, MarketData>, private val marketDataService:

    MarketDataService, private val marketDataMapper: MarketDataMapper) { fun getAllMarketData(): Flowable<Optional<List<MarketData >>> = store.getAll() fun fetchMarketData(): Completable = marketDataService.getMarketPrice() .subscribeOn(Schedulers.io()) .observeOn(Schedulers.computation()) .map(marketDataMapper) .doOnSuccess({ store.replaceAll(it) }) .toCompletable() } }
  70. Looks great in slides

  71. Fits great in slides

  72. Kotlin is here to stay

  73. Kotlin is here to stay

  74. Thank you!

  75. Inspiration • The Case Against Kotlin - Pinterest Engineering •

    Kotlin for grumpy Java developers - Pinterest Engineering • Kotlin: The Good, The Bad, and The Ugly
  76. Links • Try Kotlin - https://try.kotlinlang.org/ • Kotlin Koans -

    https://try.kotlinlang.org/#/Kotlin%20Koans/ • Kategory - https://github.com/kategory/kategory/ • Kotlinlang Slack - slack.kotlinlang.org
  77. Image credits • Android Studio Logo - Android Open Source

    Project (CCBY) • Renibeni - C. Mclean (Free Domain) • John Charles Dollman - Table d'Hote at a Dogs' Home (Free Domain) • Forbidden - Wikimedia (Free Domain) • 869018 - Pixbay (Free Domain)
  78. Questions? github.com/badoo https://habrahabr.ru/company/badoo/ @BadooDev