Is Kotlin Right For You? - Lead Dev Austin 2018

2777430c1f9869e411ab5d8b17d8ba2a?s=47 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.

2777430c1f9869e411ab5d8b17d8ba2a?s=128

Todd Ginsberg

December 06, 2018
Tweet

Transcript

  1. Is Kotlin Right For You? The Lead Developer Austin -

    2018 2018-12-06 Todd Ginsberg @ToddGinsberg Principal Software Developer
  2. @ToddGinsberg Questions? Office Hours 11:20

  3. @ToddGinsberg What Is Kotlin?

  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!)
  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
  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
  7. @ToddGinsberg Am I Alone?

  8. @ToddGinsberg Recommended on Android Beep! Prefer Kotlin over Java! -

    Google I/O 2018
  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
  10. @ToddGinsberg Syntax

  11. @ToddGinsberg Variables and Values var place: String = "Chicago" place

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

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

    // OK! val name = "Todd" name = "Emma" // Compile Error!
  14. @ToddGinsberg Null Safety // Guaranteed to never be null val

    name: String = "Todd" // May be null var city: String? = null // Safe traversal city?.toUpperCase()
  15. @ToddGinsberg Combine Safe-Traversal and Elvis println( city?.toUpperCase() ?: ”UNKNOWN” )

  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" }
  17. @ToddGinsberg Let’s Write a POJO! public class Person { public

    String firstName; public String lastName; }
  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; } }
  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; } }
  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); } }
  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 + '\'' + '}'; } }
  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
  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!
  24. @ToddGinsberg Little Things Add Up // No 'new' keyword val

    p = Person("Todd", "Ginsberg") // Simple Structural Equality "A String" == "A String" 42 == 42
  25. @ToddGinsberg Little Things Add Up // String interpolation val d

    = 2 println("Todd has $d doughnuts!") // Raw string support """ { "name": "Todd" } """
  26. @ToddGinsberg Community

  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
  28. @ToddGinsberg Fun == Fun Kotlin Thanks, Stack Overflow!

  29. @ToddGinsberg Summary

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

  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)
  32. @ToddGinsberg Is Kotlin is Right For You?

  33. Than You! @ToddGinsberg https://todd.ginsberg.com todd@ginsberg.com