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.

Taking Kotlin to production, Seriously

770 views

Published on

Next Insurance was founded in the beginning of 2016 and first lines of our production code started accumulating in May 2016. In the beginning I have started writing in Java and experimenting with Kotlin, which saw its 1.0 release two months earlier. 6 months later, the development of our backend services has totally shifted to Kotlin. We still keep a few classes in Java just to make sure that the integration remains seamless but the vast majority of our codebase is written in Kotlin. In this talk I will cover the language features and why I think it is awesome, from null safety to smart casts and data classes. We will also look into the future with 1.1 async/await feature and more.

Published in: Software
  • Be the first to comment

Taking Kotlin to production, Seriously

  1. 1. Haim Yadid / Next Insurance mapOf( taking?. kotlin to production ) Seriously!!
  2. 2. Disclaimer ‫בה‬ ‫לנאמר‬ ‫לייחס‬ ‫ואין‬ ‫סאטירה‬ ‫תוכנית‬ ‫הינה‬ ‫זו‬ ‫תוכנית‬ ‫כלשהי‬ ‫תכנות‬ ‫בשפת‬ ‫לפגוע‬ ‫ניסיון‬ ‫או‬ ‫אחרת‬ ‫משמעות‬ ‫שום‬ ‫בה‬ ‫המופיעים‬ ‫מהתכנים‬ ‫נפגע‬ ‫חש‬ ‫הנך‬ ‫זאת‬ ‫בכל‬ ‫אם‬ ‫מראש‬ ‫מתנצלים‬ ‫אנו‬ ‫בסקאלה‬ ‫מתכנת‬ ‫אתה‬ ‫אם‬ ‫או‬
  3. 3. About Me ❖ Developing software since 1984 (Boy, am I getting old?) ❖ Basic -> Pascal -> C -> C++ -> Java -> Kotlin ❖ Architect in Mercury ❖ Independent performance expert for 7 years ❖ Backend developer in
  4. 4. ❖ Founded in the Beginning of 2016 ❖ HQ@Palo Alto RnD@Kfar Saba ❖ Aims to disrupt the Small businesses insurance field ❖ Providing online experience which is simple fast and transparent ❖ We started to write real code on May 2016
  5. 5. Kotlin Java Javascript Node.JS Dropwizard RDS (mysql)
  6. 6. What is Kotlin ? ❖ Statically typed programming language ❖ for the JVM, Android and the browser (JS) ❖ 100% interoperable with Java™ (well almost) ❖ Developed by Jetbrains ❖ Revealed as an open source in July 2011 ❖ v1.0 Release on Feb 2016 ❖ 1.0.5 current stable version (28.11.2016)
  7. 7. Kotlin Features ❖ Concise ❖ Safe ❖ Versatile ❖ Practical ❖ Interoperable
  8. 8. Why Kotlin Java Scala Cloju Groovy/ JRuby Java8 Strongly
 Typed Loosely
 Typed OO/verbose Functional/Rich )))))))))))))))))))))))))))))))
  9. 9. Why Kotlin Java Scala Cloju Groovy/ JRuby Java8 Strongly
 Typed Loosely
 Typed OO/verbose Functional/Rich Kotlin )))))))))))))))))))))))))))))))
  10. 10. Scala can do it !! ? ❖ Yes Scala can do it ❖ Academic / Complicated ❖ Scala compile time is horrific ❖ Operator overloading -> OMG
  11. 11. Getting Started ❖ Kotlin has very lightweight standard library ❖ Mostly rely on Java collections for good (interop) and for bad (all the rest) ❖ Kotlin compiles to Java6 byte code ❖ Has no special build tool just use Maven / Gradle
  12. 12. Kotlin & Android ❖ Kotlin got widely accepted on the android community ❖ Runtime has low footprint ❖ Compiles to …. Java 6 ❖ Brings lambdas to Android dev and a lot of cool features
  13. 13. Trivial Stuff ❖ No new keyword ❖ No checked exceptions ❖ == structural equality ❖ === referencial equality ❖ No primitives (only under the hood) ❖ No arrays they are classes ❖ Any ~ java.lang.Object
  14. 14. Fun ❖ Functions are denoted with the fun keyword ❖ They can be member of a class ❖ Or top level functions inside a kotlin file ❖ can support tail recursion ❖ can be inlined ❖ fun funny(funnier: String) = "Hello world"
  15. 15. Fun Parameters ❖ Pascal notation ❖ Function parameters may have default values ❖ Call to function can be done using parameter names fun mergeUser(first: String = "a", last: String) = first + last
 
 fun foo() {
 mergeUser(first="Albert",last= "Einstein")
 mergeUser(last= "Einstein")
 }
  16. 16. Extension functions ❖ Not like ruby monkey patching ❖ Only extend functionality cannot override ❖ Not part of the class ❖ Static methods with the receiver as first a parameter fun String.double(sep: String) = "$this$sep$this"
  17. 17. ❖ variables are declared using the keywords ❖ val (immutable) ❖ var (mutable) ❖ fun funny(funnier: String): String {
 val x = "Hello world"
 return x
 } val /var
  18. 18. Classes ❖ less verbose than Java ❖ no static members (use companion object) ❖ public is default ❖ Must be declared open to be overridden ❖ override keyword is mandatory class Producer(val name:String, val value: Int)
  19. 19. Properties ❖ All members of classes are access via proper accessors (getter and setters) ❖ The usage of getters and setters is implicit ❖ one can override implementation of setters and getters
  20. 20. Delegate Class interface Foo {
 fun funfun()
 }
 
 class FooImpl(val x: Int) : Foo {
 override fun funfun() = print(x)
 }
 
 class Wrapper(f: Foo) : Foo by f
 
 fun main(args: Array<String>) {
 val f = FooImpl(10)
 val wf = Wrapper(f)
 wf.funfun() // prints 10
 }
  21. 21. Smart Casts ❖ If you check that a class is of a certain type no need to cast it open class A {
 fun smile() { println(":)")}
 }
 
 class B : A() {
 fun laugh() = println("ha ha ha")
 }
 
 fun funnier(a: A) {
 if (a is B ) {
 a.laugh()
 }
 }
  22. 22. Null Safety ❖ Nullability part of the type of an object ❖ Option[T] Optional<T> ❖ Swift anyone ? var msg : String = "Welcome"
 msg = null val nullableMsg : String? = null
  23. 23. Safe operator ?. ❖ The safe accessor operator ?. will result null ❖ Elvis operator for default fun funny(funnier: String?): Int? {
 
 println(x?.length ?: "")
 return x?.length
 }
  24. 24. Bang Bang !! ❖ The bang bang !! throws an NPE if object is null fun funny(funnier: String?): String {
 return funnier!!
 }
  25. 25. Data classes ❖ Less powerful version of to case classes in Scala ❖ Implementation of ❖ hashcode() equals() ❖ copy() ❖ de structuring (component1 component2 …) data class User(val firstName: String, val lastName: String)
 
 fun smile(user: User) {
 val (first,last) = user
 }
  26. 26. When ❖ Reminds Scala’s pattern matching but not as strong ❖ More capable than select in java when (x) {
 is Int -> print(x + 1)
 is String -> print(x.length + 1)
 is IntArray -> print(x.sum())
 }
  27. 27. Sane Operator Overloading ❖ override only a set of the predefined operators ❖ By implementing the method with the correct name Expression Translated to a + b a.plus(b) a - b a.minus(b) a * b a.times(b) a / b a.div(b) a % b a.mod(b) a..b a.rangeTo(b)
  28. 28. Sane Operator Overloading data class Complex(val x: Double, val y: Double) {
 operator fun inc(): Complex {
 return Complex(x + 1, y + 1)
 }
 
 infix operator fun plus(c: Complex): Complex {
 return (Complex(c.x + x, c.y + y))
 }
 
 }
  29. 29. Collections ❖ no language operators for construction of lists and maps ❖ using underlying java collections ❖ Have two versions mutable and immutable ❖ Immutability is a lie at least ATM it is only an immutable view of a mutable collection.
  30. 30. Collections val chars = mapOf("Terion" to "Lannister", "John" to "Snow")
 chars["Terion"]
 chars["Aria"] = “Stark" 
 
 val chars2 = mutableMapOf<String,String>()
 chars2["A girl"] = "has no name"
 
 val sigils = listOf("Direwolf", "Lion", "Dragon", "Flower")
 println(sigils[0])
  31. 31. Kotlin 1.1 ❖ Java 7/8 support -> generate J8 class files ❖ Coroutines : async / await (stackless continuations) ❖ Generators: Yield ❖ inheritance of data classes
  32. 32. Generators (yield) val fibonacci: Sequence<Int> = generate { yield(1) // first Fibonacci number var cur = 1 var next = 1 while (true) { yield(next) // next Fibonacci number val tmp = cur + next cur = next next = tmp } }
  33. 33. Questions?

×