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 Slide

  2. @ToddGinsberg
    Questions?
    Office Hours
    11:20

    View Slide

  3. @ToddGinsberg
    What Is Kotlin?

    View 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 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 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 Slide

  7. @ToddGinsberg
    Am I Alone?

    View Slide

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

    View 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 Slide

  10. @ToddGinsberg
    Syntax

    View Slide

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

    View Slide

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

    View Slide

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

    View 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 Slide

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

    View 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 Slide

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

    View 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 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 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 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 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 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 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 Slide

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

    View Slide

  26. @ToddGinsberg
    Community

    View 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 Slide

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

    View Slide

  29. @ToddGinsberg
    Summary

    View Slide

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

    View 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 Slide

  32. @ToddGinsberg
    Is Kotlin is Right For You?

    View Slide

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

    View Slide