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

Using Kotlin in Production

Emma Guy
November 11, 2017

Using Kotlin in Production

Kotlin has fast become the new hotness in the Android world - but what is the reality of adding it to an app which is rapidly being built for a fast growing startup? In this talk we'll cover how we went about adding Kotlin to the Monzo app, why we made those choices and how it went.

https://devfest.gdg.london/schedule/2017-11-11?sessionId=204

Emma Guy

November 11, 2017
Tweet

More Decks by Emma Guy

Other Decks in Technology

Transcript

  1. Using Kotlin in Production
    @emmaguy

    View full-size slide

  2. How did we get there?
    Did it go well?
    Lessons learnt

    View full-size slide

  3. Convince your team

    View full-size slide

  4. Have an offsite
    Nerd out!

    View full-size slide

  5. Convince your manager(s)

    View full-size slide

  6. Christina Lee @ Google I/O
    https://www.youtube.com/watch?v=fPzxfeDJDzY

    View full-size slide

  7. Everyone is sold! Let’s go!

    View full-size slide

  8. “Let’s just start using it”
    - me, March 2017

    View full-size slide

  9. Decide how to use it

    View full-size slide

  10. Only tests?
    A layer of your application’s architecture?
    ‘Anything new’?

    View full-size slide

  11. @Test
    public void register_noPhoneNumber_showContactSupport() {
    when(pinManager.fetchPhoneNumber()).thenReturn("");
    presenter.register(view);
    verify(view).showContactSupport();
    }

    View full-size slide

  12. @Test
    fun register_noPhoneNumber_showContactSupport() {
    whenever(pinManager.fetchPhoneNumber()).thenReturn("")
    presenter.register(view)
    verify(view).showContactSupport()
    }

    View full-size slide

  13. Figure out any potential “incompatibilities”
    e.g. Realm, Lombok

    View full-size slide

  14. Learn & Adapt

    View full-size slide

  15. Progressed to use in data layer of our app
    ...then in Activities (replacing ButterKnife)
    ...then anything new!

    View full-size slide

  16. Expect discussions about nullability

    View full-size slide

  17. #kotlin-tips

    View full-size slide

  18. Stick with what you committed to for a while
    Assess
    Make changes

    View full-size slide

  19. Awesome things

    View full-size slide

  20. Kotlin = great for hiring

    View full-size slide

  21. @Deprecated
    https://hackernoon.com/how-kotlins-deprecated-relieves-pain-of-colossal-refactoring-8577545aaed

    View full-size slide

  22. Extension methods

    View full-size slide

  23. @Override
    public void showLoading() {
    progressBar.setVisibility(View.VISIBLE);
    }
    or
    override fun showLoading() {
    progressBar.visible()
    }

    View full-size slide

  24. fun ImageView.loadUrl(url: String) {
    Picasso.with(context).load(url).into(this)
    }
    imageView.loadUrl(url)

    View full-size slide

  25. fun Activity.screenWidth(): Int {
    val metrics = DisplayMetrics()
    windowManager.defaultDisplay.getMetrics(metrics)
    return metrics.widthPixels
    }

    View full-size slide

  26. Named/default arguments

    View full-size slide

  27. fun card(
    id: String = UUID.randomUUID().toString(),
    expires: String = "24/20",
    cardStatus: CardStatus = CardStatus.ACTIVE
    ): Card {
    return Card(id, expires, cardStatus)
    }
    Card(expires = "03/16", cardStatus = CardStatus.INACTIVE)

    View full-size slide

  28. setUpgradeBannerStyle(true)
    or
    setUpgradeBannerStyle(loudStyle = true)

    View full-size slide

  29. val arguments = Bundle().apply { putString(KEY_ID, id) }

    View full-size slide

  30. Things we learnt

    View full-size slide

  31. “Convert any file you touch” doesn’t work

    View full-size slide

  32. There is no ‘static’

    View full-size slide

  33. companion object {}
    https://medium.com/@BladeCoder/exploring-kotlins-hidden-costs-part-1-fbb9935d9b62

    View full-size slide

  34. Where do I put constants?

    View full-size slide

  35. Auto convert is not always your friend

    View full-size slide

  36. SAM Conversion

    View full-size slide

  37. public interface PayeeClickListener {
    void onPayeeClicked(Payee payee);
    }

    View full-size slide

  38. adapter.setPayeeClickListener(payee -> /* do something */);

    View full-size slide

  39. adapter.setPayeeClickListener(PayeeClickListener { /* do something
    */ })

    View full-size slide

  40. But, if we convert the interface to Kotlin

    View full-size slide

  41. adapter.setPayeeClickListener(object : PayeeClickListener {
    override fun onPayeeClicked(payee: Payee) {
    // do something
    }
    })

    View full-size slide

  42. New thing = less mature ecosystem
    IDE integration, style guide, code coverage tools

    View full-size slide

  43. Would you do it again?

    View full-size slide

  44. Thank you!
    @emmaguy

    View full-size slide

  45. Questions?
    @emmaguy

    View full-size slide