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
  2. @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!)
  3. @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
  4. @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
  5. @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
  6. @ToddGinsberg Variables and Values var place: String = "Chicago" place

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

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

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

    name: String = "Todd" // May be null var city: String? = null // Safe traversal city?.toUpperCase()
  10. @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" }
  11. @ToddGinsberg Let’s Write a POJO! public class Person { public

    String firstName; public String lastName; }
  12. @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; } }
  13. @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; } }
  14. @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); } }
  15. @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 + '\'' + '}'; } }
  16. @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
  17. @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!
  18. @ToddGinsberg Little Things Add Up // No 'new' keyword val

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

    = 2 println("Todd has $d doughnuts!") // Raw string support """ { "name": "Todd" } """
  20. @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
  21. @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)