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

Is Kotlin Right For You? - Lead Dev Austin 2018

Todd Ginsberg
December 06, 2018

Is Kotlin Right For You? - Lead Dev Austin 2018

A Lightning Talk version of "Is Kotlin Right For You?" I gave this talk at The Lead Developer - Austin 2018. It is intended for an audience that knows how to program, but perhaps not in Java or Kotlin. It goes over the top level features and design philosophy of Kotlin.

Todd Ginsberg

December 06, 2018
Tweet

More Decks by Todd Ginsberg

Other Decks in Technology

Transcript

  1. Is Kotlin
    Right For You?
    The Lead Developer Austin - 2018
    2018-12-06
    Todd Ginsberg
    @ToddGinsberg
    Principal Software Developer

    View full-size slide

  2. @ToddGinsberg
    Questions?
    Office Hours
    11:20

    View full-size slide

  3. @ToddGinsberg
    What Is Kotlin?

    View full-size slide

  4. @ToddGinsberg
    What Is Kotlin?
    Statically typed language, developed by JetBrains
    Released under Apache 2.0 license
    Designed as a general purpose language
    • Targets JVM 6 or 8 bytecode
    • Targets ECMAScript 5.1
    • Targets native thanks to LLVM (Beta!)

    View full-size slide

  5. @ToddGinsberg
    Major Features
    Designed to avoid entire classes of defects
    Null-safe
    178,000+ issues on GitHub!
    12,543 duplicates on Stack Overflow!
    100% Interoperable with Java
    Pragmatic – No shame in copying language features that
    make developers more productive

    View full-size slide

  6. @ToddGinsberg
    Make “Bad” Choices Explicit or Impossible
    Fixed in Kotlin:
    - Singleton support built in
    - Override keyword mandatory
    - Properties over fields with getter/setter
    - Mutability is minimized (val + collections)
    - Inheritance prohibited by default
    - Builders are easy with default values
    - No checked exceptions
    - Structural equality is the same everywhere
    - Delegation support makes composition easier

    View full-size slide

  7. @ToddGinsberg
    Am I Alone?

    View full-size slide

  8. @ToddGinsberg
    Recommended on Android
    Beep! Prefer
    Kotlin over Java!
    - Google I/O 2018

    View full-size slide

  9. @ToddGinsberg
    Spring Framework Support
    Kotlin is fully supported in Spring Framework 5
    Kotlin is an option on start.spring.io
    Comprehensive Kotlin documentation and examples

    View full-size slide

  10. @ToddGinsberg
    Syntax

    View full-size slide

  11. @ToddGinsberg
    Variables and Values
    var place: String = "Chicago"
    place = ”Austin" // OK!
    val name: String = "Todd"
    name = "Emma" // Compile Error!

    View full-size slide

  12. @ToddGinsberg
    Inferred Types
    var place: String = "Chicago"
    place = ”Austin" // OK!
    val name: String = "Todd"
    name = "Emma" // Compile Error!

    View full-size slide

  13. @ToddGinsberg
    Inferred Types
    var place = "Chicago"
    place = ”Austin" // OK!
    val name = "Todd"
    name = "Emma" // Compile Error!

    View full-size slide

  14. @ToddGinsberg
    Null Safety
    // Guaranteed to never be null
    val name: String = "Todd"
    // May be null
    var city: String? = null
    // Safe traversal
    city?.toUpperCase()

    View full-size slide

  15. @ToddGinsberg
    Combine Safe-Traversal and Elvis
    println(
    city?.toUpperCase() ?: ”UNKNOWN”
    )

    View full-size slide

  16. @ToddGinsberg
    When > Switch
    val result = when(x) {
    0 -> "x is 0”
    in 1..10 -> "x is between 1 and 10”
    in someSet -> "x is in someSet”
    parseString(s) -> "the same as parseString”
    else -> "x doesn't match anything"
    }

    View full-size slide

  17. @ToddGinsberg
    Let’s Write a POJO!
    public class Person {
    public String firstName;
    public String lastName;
    }

    View full-size slide

  18. @ToddGinsberg
    Let’s Write a POJO!
    public class Person {
    private String firstName;
    private String lastName;
    public String getFirstName() {
    return firstName;
    }
    public void setFirstName(final String firstName) {
    this.firstName = firstName;
    }
    public String getLastName() {
    return lastName;
    }
    public void setLastName(final String lastName) {
    this.lastName = lastName;
    }
    }

    View full-size slide

  19. @ToddGinsberg
    Let’s Write a POJO!
    public class Person {
    private String firstName;
    private String lastName;
    public Person() { }
    public Person(final String firstName, final String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
    }
    public String getFirstName() {
    return firstName;
    }
    public void setFirstName(final String firstName) {
    this.firstName = firstName;
    }
    public String getLastName() {
    return lastName;
    }
    public void setLastName(final String lastName) {
    this.lastName = lastName;
    }
    }

    View full-size slide

  20. @ToddGinsberg
    Let’s Write a POJO!
    import java.util.Objects;
    public class Person {
    private String firstName;
    private String lastName;
    public Person() { }
    public Person(final String firstName, final String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
    }
    public String getFirstName() {
    return firstName;
    }
    public void setFirstName(final String firstName) {
    this.firstName = firstName;
    }
    public String getLastName() {
    return lastName;
    }
    public void setLastName(final String lastName) {
    this.lastName = lastName;
    }
    @Override
    public boolean equals(final Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    final Person person = (Person) o;
    return Objects.equals(firstName, person.firstName) &&
    Objects.equals(lastName, person.lastName);
    }
    @Override
    public int hashCode() {
    return Objects.hash(firstName, lastName);
    }
    }

    View full-size slide

  21. @ToddGinsberg
    Let’s Write a POJO!
    import java.util.Objects;
    public class Person {
    private String firstName;
    private String lastName;
    public Person() { }
    public Person(final String firstName, final String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
    }
    public String getFirstName() {
    return firstName;
    }
    public void setFirstName(final String firstName) {
    this.firstName = firstName;
    }
    public String getLastName() {
    return lastName;
    }
    public void setLastName(final String lastName) {
    this.lastName = lastName;
    }
    @Override
    public boolean equals(final Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    final Person person = (Person) o;
    return Objects.equals(firstName, person.firstName) &&
    Objects.equals(lastName, person.lastName);
    }
    @Override
    public int hashCode() {
    return Objects.hash(firstName, lastName);
    }
    @Override
    public String toString() {
    return "Person{" +
    "firstName='" + firstName + '\'' +
    ", lastName='" + lastName + '\'' +
    '}';
    }
    }

    View full-size slide

  22. @ToddGinsberg
    Data Classes To The Rescue!
    data class Person(var firstName: String,
    var lastName: String)
    • toString()
    • hashCode() and equals()
    • Getters and Setters as Properties
    • Copy constructors

    View full-size slide

  23. @ToddGinsberg
    Extension FUNctions
    // Java
    public static boolean isEven(int i) {
    return i % 2 == 0;
    }
    // Kotlin
    fun Int.isEven(): Boolean = this % 2 == 0
    2.isEven() // True!

    View full-size slide

  24. @ToddGinsberg
    Little Things Add Up
    // No 'new' keyword
    val p = Person("Todd", "Ginsberg")
    // Simple Structural Equality
    "A String" == "A String"
    42 == 42

    View full-size slide

  25. @ToddGinsberg
    Little Things Add Up
    // String interpolation
    val d = 2
    println("Todd has $d doughnuts!")
    // Raw string support
    """
    {
    "name": "Todd"
    }
    """

    View full-size slide

  26. @ToddGinsberg
    Community

    View full-size slide

  27. @ToddGinsberg
    Community Resources
    https://kotlinlang.slack.com
    • 19,500+ welcoming and helpful members
    • Language designers hang out here
    • Get answers and chat with people who work with the
    language

    View full-size slide

  28. @ToddGinsberg
    Fun == Fun
    Kotlin
    Thanks, Stack Overflow!

    View full-size slide

  29. @ToddGinsberg
    Summary

    View full-size slide

  30. @ToddGinsberg
    Learn More
    https://play.kotlinlang.org

    View full-size slide

  31. @ToddGinsberg
    Why Kotlin is Right For Me
    I write far less code.
    The code I write is more expressive and clear.
    I avoid whole classes of defects.
    Writing Kotlin, for me, is more fun.
    Plays well with the tools I already use (Spring, IDEA, Gradle)

    View full-size slide

  32. @ToddGinsberg
    Is Kotlin is Right For You?

    View full-size slide

  33. Than You!
    @ToddGinsberg
    https://todd.ginsberg.com
    [email protected]

    View full-size slide