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

Kotlin, the good

Kotlin, the good

This presentation was done at GDG Lagos's edition of Google I/O 2017 Extended.

In this presentation, Moyin (https://speakerdeck.com/moyheen/) and I introduced the audience to Kotlin. We spoke about the history, the exciting new features and how to get started with learning it.

Segun Famisa

June 19, 2017
Tweet

More Decks by Segun Famisa

Other Decks in Programming

Transcript

  1. Kotlin
    The good
    Moyinoluwa Adeyemi

    View Slide

  2. Kotlin
    The good, the bad and the ugly
    Segun Famisa
    Moyinoluwa Adeyemi

    View Slide

  3. Intro to Kotlin - History
    Statically typed programming language, and runs on the JVM.

    View Slide

  4. Intro to Kotlin - History
    Statically typed programming language, and runs on the JVM.
    Kotlin was developed by JetBrains (Makers of IntelliJ)

    View Slide

  5. Intro to Kotlin - History
    Statically typed programming language, and runs on the JVM.
    Kotlin was developed by JetBrains (Makers of IntelliJ)
    First commit dates back to 2010, but was first publicly seen around 2011.

    View Slide

  6. Intro to Kotlin - Why did Google make it official?
    Awesome features - nullable types, concise, data classes, etc.
    Great community support
    There was an overwhelming request for Kotlin official support on Android

    View Slide

  7. Interesting Features of Kotlin

    View Slide

  8. Null Safety - nullable types and non-nullable types
    Protects against NullPointerException the $1,000,000,000 mistake

    View Slide

  9. Null Safety - nullable types and non-nullable types
    // compiler error
    var name: String
    name = null
    A non-nullable object can’t be null
    Protects against NullPointerException the $1,000,000,000 mistake

    View Slide

  10. Null Safety - nullable types and non-nullable types
    // compiler error
    var name: String
    name = null
    // compiler error
    var name: String?
    val length = name.length
    A non-nullable object can’t be null
    Protects against NullPointerException the $1,000,000,000 mistake
    Specify a nullable object by using “?”
    Kotlin ensures that you don’t mistakenly operate on
    nullable objects

    View Slide

  11. Null Safety - Accessing properties in a nullable object
    // handle non-null case and null case
    var name: String? = null
    val length = if (name != null) name.length else 0
    1. Checking for null types

    View Slide

  12. Null Safety - Accessing properties in a nullable object
    // use ?. to make safe call
    var name: String? = null
    ...
    val length = name?.length
    Use ?. to safely access a property/method on a
    nullable object
    If name happens to be null, the value of length is 0
    (inferred integer).
    length would be null if it were of another type.
    2. Making safe calls using “?.”

    View Slide

  13. Null Safety - Accessing properties in a nullable object
    // use elvis operator
    var name: String? = null
    val length = name?.length ?: 0
    This reads as “if name is not null, use
    name.length else use 0”
    3. Making use of the “?:” elvis operator

    View Slide

  14. Null Safety - Accessing properties in a nullable object
    // use !! assertion
    var name: String? = null
    val length = name!!.length
    This reads as “if name is not null, use
    name.length else throw a null pointer
    exception”
    4. Making use of the “!!” assertion operator

    View Slide

  15. Null Safety - NPE free!
    You can only have the NullPointerException in Kotlin if:
    1. You explicitly throw a NPE
    2. You make use of the !! operator
    3. An external Java code causes the NPE.

    View Slide

  16. Smart inference
    // you don’t need to explicitly put a type
    val name = "Segun" // string
    val age = 24 // int
    val grade = 'A' // char
    val hobbies = listOf("karate", "eating") // list of strings
    Kotlin can smartly infer the type of your variable from the data it contains

    View Slide

  17. Immutability
    Kotlin helps developers to be intentional about immutability.
    Immutability simply means that things you create can’t be changed.
    We need immutability because it:
    ● helps us with thread safety - no synchronization issues
    ● is good for value objects - objects that simply hold value, POJOs etc.
    ● helps debugging threaded applications without losing your hair

    View Slide

  18. Immutability - How does it work in Java?
    ● Immutability in Java?
    final classes
    private fields
    no setters
    ● Immutability in Kotlin? Guess?

    View Slide

  19. Immutability - How does Kotlin help?
    var vs val
    // compiler error: val cannot be reassigned
    val name = "Moyin"
    name = name.toUpperCase()
    // works fine
    var name = "Moyin"
    name = name.toUpperCase()

    View Slide

  20. Immutability - How does Kotlin help?
    Immutable and Mutable collections
    // immutable collection
    val unchangeableHobbies = listOf("coding", "eating")
    unchangeableHobbies.add() // add method doesn’t exist
    // mutable collection
    val changeableHobbies = mutableListOf("karate", "basketball")
    changeableHobbies.add("soccer") // you can add

    View Slide

  21. public final class ImmutableClassJava {
    private final String name;
    private final int age;
    public ImmutableClassJava(String name, int age) {
    this.name = name;
    this.age = age;
    }
    // no setters
    public String getName() {
    return name;
    }
    public int getAge() {
    return age;
    }
    }
    class ImmutableClass(val name: String,
    val age: Int,
    val grade: Char,
    val hobbies: List)
    ● Class is final by default
    ● val implies that the parameters are final as well (values
    can’t be assigned)
    Immutability in Java vs Kotlin

    View Slide

  22. Functions
    // function sample
    fun sampleFunc() {
    // code goes here
    }
    A function is declared using the “fun”
    keyword

    View Slide

  23. Functions
    // function sample
    fun sampleFunc() {
    // code goes here
    }
    // function with param
    fun sampleFuncWithParam(param: String) {
    // code goes here
    }
    A function is declared using the “fun”
    keyword
    Method parameters use the “name:Type”
    notation

    View Slide

  24. Functions
    // function sample
    fun sampleFunc() {
    // code goes here
    }
    // function with param
    fun sampleFuncWithParam(param: String) {
    // code goes here
    }
    // func with param and return type
    fun capitalize(param: String): String {
    return param.toUpperCase()
    }
    Method parameters use the “name:Type”
    notation
    Return types are specified after the method
    definition.
    A function is declared using the “fun”
    keyword

    View Slide

  25. Functions - infix functions
    // infix function
    infix fun Int.times(x: Int): Int {
    return this * x
    }

    View Slide

  26. Functions - infix functions
    // infix function
    infix fun Int.times(x: Int): Int {
    return this * x
    }
    // usage
    fun useInfix() {
    val product = 2 times 5
    println(product)
    }

    View Slide

  27. Functions - extension functions
    // extension function
    fun Int.square(): Int {
    return this * this
    }

    View Slide

  28. Functions - extension functions
    // extension function
    fun Int.square(): Int {
    return this * this
    }
    fun useExtension() {
    val square = 2.square()
    println(square)
    }

    View Slide

  29. Functions
    Others:
    ● Higher order functions
    ● Lambdas
    ● Inline functions etc.

    View Slide

  30. Conciseness

    View Slide

  31. public void doSomething() {
    // do something
    }
    fun doSomething(): Unit {
    // do same thing
    }
    Conciseness in Kotlin

    View Slide

  32. fun doSomething(): Unit {
    // do same thing
    }
    Conciseness in Kotlin
    public void doSomething() {
    // do something
    }

    View Slide

  33. fun doSomething() {
    // do same thing
    }
    Conciseness in Kotlin
    public void doSomething() {
    // do something
    }

    View Slide

  34. public String getName() {
    return name;
    }
    fun getName(): String {
    return name
    }
    Conciseness in Kotlin

    View Slide

  35. // Java
    fun getName() {
    return name
    }
    Conciseness in Kotlin
    public String getName() {
    return name;
    }

    View Slide

  36. fun getName() = name
    Conciseness in Kotlin
    public String getName() {
    return name;
    }

    View Slide

  37. Classes

    View Slide

  38. public class Person {
    private String name;
    String getName () {
    return name;
    }
    void setName (String name) {
    this.name = name;
    }
    @Override
    public String toString () {
    return "Person{" +
    "name='" + name + '\'' +
    '}';
    }
    }
    data class Person(val name: String)
    Classes in Java vs Data Classes in Kotlin

    View Slide

  39. public class Person {
    private String name;
    String getName () {
    return name;
    }
    void setName (String name) {
    this.name = name;
    }
    @Override
    public String toString () {
    return "Person{" +
    "name='" + name + '\'' +
    '}';
    }
    }
    class Person(name: String) {
    var name: String
    get() = name
    set(name) {
    this.name = name
    }
    fun getNameLength(): int {
    return name.length
    }
    }
    public class Person {
    private String name;
    String getName () {
    return name;
    }
    void setName (String name) {
    this.name = name;
    }
    public int getNameLength () {
    return name.length
    }
    }
    Classes in Java vs Regular Classes in Kotlin

    View Slide

  40. Classes in Java vs Regular Classes in Kotlin
    public class Person {
    private String name;
    String getName () {
    return name;
    }
    void setName (String name) {
    this.name = name;
    }
    public int getNameLength () {
    return name.length
    }
    }
    class Person(name: String) {
    var name: String
    get() = name
    set(name) {
    this.name = name
    }
    fun getNameLength() = name.length
    }

    View Slide

  41. Getting started

    View Slide

  42. Kotlin Koans
    ● Koans online

    View Slide

  43. Kotlin Koans
    ● Koans online
    ● Offline Kotlin
    Educational Plugin

    View Slide

  44. Kotlin Koans
    ● Koans online
    ● Offline Kotlin
    Educational Plugin
    ● Clone the project on
    Github

    View Slide

  45. Kotlin Koans
    ● Koans online
    ● Offline Kotlin
    Educational Plugin
    ● Clone the project on
    Github
    IDE support
    Kotlin Koans
    + plugin

    View Slide

  46. Kotlin Koans
    ● Koans online
    ● Offline Kotlin
    Educational Plugin
    ● Clone the project on
    Github
    IDE support
    = built-in support
    + plugin

    View Slide

  47. Kotlin Koans
    ● Koans online
    ● Offline Kotlin
    Educational Plugin
    ● Clone the project on
    Github
    IDE support
    = built-in support
    = built-in support
    + plugin

    View Slide

  48. Kotlin Koans
    ● Koans online
    ● Offline Kotlin
    Educational Plugin
    ● Clone the project on
    Github
    IDE support Community
    Kotlin Weekly
    http://www.kotlinweekly.net/
    = built-in support
    = built-in support
    + plugin

    View Slide

  49. Kotlin Koans
    ● Koans online
    ● Offline Kotlin
    Educational Plugin
    ● Clone the project on
    Github
    IDE support Community
    Kotlin Weekly
    http://www.kotlinweekly.net/
    Kotlin Conf
    kotlinconf.com
    +
    = built-in support
    plugin
    = built-in support

    View Slide

  50. Thank you!
    @segunfamisa
    segunfamisa.com
    @moyheen
    medium.com/@moyinoluwa

    View Slide