Getting Kotlin in Your Codebase

Getting Kotlin in Your Codebase

http://oredev.org/2016/sessions/getting-kotlin-in-your-codebase

Excitement swirls around Kotlin, and we developers want it in our codebases. From an Android engineer's point of view, Eric will talk about the best ways to start using Kotlin at work and what parts of a Android codebase really benefit from Kotlin usage. With the 1.0 stable release, convincing your coworkers to get Kotlin into at least your tests is an endeavor worth undertaking!

627ce26ea490d993a49d6b263768ca67?s=128

Eric Cochran

November 10, 2016
Tweet

Transcript

  1. Getting Kotlin in Your Codebase Eric Cochran Oredev November 10,

    2016
  2. Kotlin Value Add Reduce Verbosity Nullability Lateinit Data Classes Mutability

    Extension Functions
  3. 3 Reduce Verbosity Lambdas val listener: (View) -> Unit =

    { logClick() } view.setOnClickListener (listener)
  4. 4 Reduce Verbosity Default Parameters fun print(list: List<String>, count: Int

    = list.size())
  5. 5 Nullability Nullability in the type system var name: String?

    = null Safe calls with ?. val name: String? = user.name?.trim() Assert not null with !! val name: String = user.name!! Compiler Tracking if (user.name != null) return user.name.trim()
  6. 6 Some explanatory text to introduce the theme of the

    deck and such. Lateinit Available for non-null, var, non-primitive properties
  7. 7 Some explanatory text to introduce the theme of the

    deck and such. Lateinit Easy Dependency Injection class MyView { private lateinit var dependency: Dependency fun setup(dependency: Dependency) { this.dependency = dependency } }
  8. 8 Some explanatory text to introduce the theme of the

    deck and such. Lateinit Tests public class MyViewTest { private lateinit var view: MyView @Setup fun setup() { view = MyView(…) view.setup(…) } }
  9. 9 data class Train(val number: Int, val name: String =

    “”, val stations: List<Station>) Generated equals, hashCode, copy Allow implementing interfaces Data Classes
  10. 10 Real immutable collections by default List vs MutableList Set

    vs MutableSet Map vs MutableMap Covariant mutable collections: val textViews: List<TextView> = listOf(name, badge) val views: List<View> = textViews Mutability and Immutability
  11. 11 Ending Util Hell @file:JvmName(“TextViewUtils”) fun TextView.isUserSet(user: User) = getText()

    == user.name Extension Functions
  12. 12 Measuring Performance var tock = measureTimeMillis { fetchAndSetNames() }

    log(tock)
  13. 13 Why Kotlin? Standard Library Scala: ~50K methods Groovy: ~30K

    methods Kotlin: ~6K methods
  14. 14 Convert to Kotlin Alt + Cmd + Shift +

    K
  15. 15 @Eric_Cochran github.com/NightlyNexus nightlynexus.com