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

Kotlin why you should consider using it

Kotlin why you should consider using it

Souleymane Sidibe

December 16, 2017
Tweet

More Decks by Souleymane Sidibe

Other Decks in Programming

Transcript

  1. Why you should
    consider using it?
    @soulesidibe

    View full-size slide

  2. • Quick introduction to Kotlin
    • Features that I love
    • Kotlin for Android developers
    • What else?
    Agenda

    View full-size slide

  3. Quick introduction
    to Kotlin

    View full-size slide

  4. • Created by JetBrains
    • Statically-typed programming language
    • For the JVM

    View full-size slide

  5. Java interoperability
    • 100% interoperable with Java
    • Your framework and libs are available

    View full-size slide

  6. String interpolation
    val x = 4
    val y = 7
    print("sum of $x and $y is ${x + y}”)
    "// sum of 4 and 7 is 11

    View full-size slide

  7. Type inference
    val a = "abc" "// type inferred to String
    val b = 4 "// type inferred to Int
    val c: Double = 0.7 "// type declared explicitly
    val d: List = ArrayList() "// type declared explicitly

    View full-size slide

  8. Default Arguments
    fun build(title: String, width: Int = 800, height: Int = 600) {
    Frame(title, width, height)
    }
    fun main(args: Array) {
    build("Title")
    build("Title", 1024, 1024)
    build("Title", height = 1024)
    }

    View full-size slide

  9. Data Class
    data class Person(val name: String,
    var email: String,
    var age: Int)
    val person = Person("Abdou", "[email protected]", 20)
    "//Person(name=Abdou, [email protected], age=20)

    View full-size slide

  10. Better for loop
    for (i in 1"..100) { ""... }
    for (i in 0 until 100) { ""... }
    for (i in 2"..10 step 2) { ""... }
    for (i in 10 downTo 1) { ""... }

    View full-size slide

  11. Better collections
    api
    val list: List = listOf(1, 2, 3, 4)
    mapOf("Mouhamed" to 21, "ali" to 25, "Papi" to 29)
    setOf("one", "two", "three")
    val mutableList: MutableList = mutableListOf(1, 2, 3, 4)
    mutableMapOf("Mouhamed" to 21, "ali" to 25, "Papi" to 29)
    mutableSetOf("one", "two", "three")
    val list = listOf(1, 2, 3, 4)
    val newList = list.map { it * 2 }.filter { it > 2 }

    View full-size slide

  12. Features that I
    love

    View full-size slide

  13. Null safety
    • NullPointerException Problem
    • Nullable Type, Non Nullable Type
    fun main(args: Array) {
    var name: String = “Soulesidibe"
    }

    View full-size slide

  14. Null safety
    • NullPointerException Problem
    • Nullable Type, Non Nullable Type
    fun main(args: Array) {
    var name: String? = “Soulesidibe"
    }

    View full-size slide

  15. Null safety
    • NullPointerException Problem
    • Nullable Type, Non Nullable Type
    fun main(args: Array) {
    var name: String? = “Soulesidibe”
    val length = name?.length
    }

    View full-size slide

  16. Null safety
    • NullPointerException Problem
    • Nullable Type, Non Nullable Type
    fun main(args: Array) {
    var name: String? = “Soulesidibe”
    val length = if (name "== null) 0 else name.length
    }

    View full-size slide

  17. Extension functions
    • Extend a class without modifying it
    • Avoid utils classes
    fun String.hello(): String {
    return “Hello $this”
    }
    fun main(args: Array) {
    var name: String = “Soulesidibe”
    println(name.hello())
    }

    View full-size slide

  18. Extension functions
    • Extend a class without modifying it
    • Avoid utils classes
    fun MutableList.swap(index1: Int, index2: Int) {
    val tmp = this[index1]
    this[index1] = this[index2]
    this[index2] = tmp
    }
    fun main(args: Array) {
    var list = mutableListOf(1, 2, 3, 5)
    list.swap(1,2) "//1,3,2,5
    }

    View full-size slide

  19. High order functions
    • A function that takes a function as parameter
    • A function that returns a function
    fun operation(a: Int, b: Int, func: (Int,Int) "-> Int): Int {
    return func(a, b)
    }
    fun main(args: Array) {
    operation(3, 4, {a, b "-> a + b})
    }

    View full-size slide

  20. High order functions
    • A function that takes a function as parameter
    • A function that returns a function
    fun operation(a: Int, b: Int, func: (Int,Int) "-> Int): Int {
    return func(a, b)
    }
    fun main(args: Array) {
    operation(3, 4) {a, b "-> a + b}
    }

    View full-size slide

  21. Class Delegation
    •Design pattern in OOP
    •Object composition without inheritance
    •Native support on Kotlin
    interface Base {
    fun print()
    }
    class BaseImpl(val x: Int) : Base {
    override fun print() { print(x) }
    }
    class Derived(val x: Int) : Base by BaseImpl(x)
    fun main(args: Array) {
    Derived(10).print() "// prints 10
    }

    View full-size slide

  22. Class Delegation
    •Design pattern in OOP
    •Object composition without inheritance
    •Native support on Kotlin
    interface Base {
    fun print()
    }
    class BaseImpl(val x: Int) : Base {
    override fun print() { print(x) }
    }
    class Derived(b: Base) : Base by b
    fun main(args: Array) {
    val b = BaseImpl(10)
    Derived(b).print() "// prints 10
    }

    View full-size slide

  23. Kotlin for android
    developers

    View full-size slide

  24. Kotlin for android
    developers
    • Google announces official support on
    android
    • Android Studio supports Kotlin
    • Jake Wharton joins Google
    • The community adopts Kotlin

    View full-size slide

  25. Interoperability
    • 100% compatible with java 6, 7, 8, 9
    • Kotlin and Java interoperable
    class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    }
    }

    View full-size slide

  26. Be lazy
    • Value gets computed only upon first access
    • Better memory management(Heap)
    • Less garbage collection
    class MainActivity : AppCompatActivity() {
    val button: Button by lazy { findViewById(R.id.btn_click) }
    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    button.setOnClickListerner{
    "// code…
    }
    }
    }

    View full-size slide

  27. Extension functions
    • Avoid API version check
    • More concise code
    class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    if (button.isAttachedWindow()) {
    "// coded
    }
    }
    }
    fun View.isAttachedWindow(): Boolean {
    return if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
    ViewCompat.isAttachedToWindow(this)
    } else isAttachedToWindow
    }

    View full-size slide

  28. Extension functions
    • Avoid boilerplate
    • More concise code
    "// java
    String firstName = null;
    int firsNameColumn = cursor.getColumnIndexOrThrow("first_name");
    if (!cursor.isNull(firsNameColumn)) {
    firstName = cursor.getString(firsNameColumn);
    }

    View full-size slide

  29. Extension functions
    • Avoid boilerplate
    • More concise code
    "// java
    String firstName = null;
    int firsNameColumn = cursor.getColumnIndexOrThrow("first_name");
    if (!cursor.isNull(firsNameColumn)) {
    firstName = cursor.getString(firsNameColumn);
    }
    "// kotlin
    fun Cursor.getStringOrNull(columnName: String): String? {
    val index = getColumnIndexOrThrow(columnName)
    return if (isNull(index)) null else getString(index)
    }

    View full-size slide

  30. Extension functions
    • Avoid boilerplate
    • More concise code
    "// java
    String firstName = null;
    int firsNameColumn = cursor.getColumnIndexOrThrow("first_name");
    if (!cursor.isNull(firsNameColumn)) {
    firstName = cursor.getString(firsNameColumn);
    }
    "// kotlin
    fun Cursor.getStringOrNull(columnName: String): String? {
    val index = getColumnIndexOrThrow(columnName)
    return if (isNull(index)) null else getString(index)
    }
    val name = cursor.getStringOrNull("first_name")
    firstName.text = name "?: "Soulesidibe"

    View full-size slide

  31. What else?
    • Not just for android developers
    • Kotlin for server side(Spring, Ktor, Vertx,
    Spark)
    • Kotlin/Js (client and server)
    • Kotlin/native "=> Code sharing

    View full-size slide

  32. Where to learn Kotlin
    • Official Kotlin Doc
    • Jake Wharton talk
    • Antonio Leiva Blog
    • My blog
    • Try it online

    View full-size slide

  33. That’s it!
    @soulesidibe

    View full-size slide