Kotlin: Why Do You Care?

67
Kotlin in the Community Team Productivity Tools Languages Kotlin MPS TeamCity YouTrack Upsource IDEs IntelliJ IDEA PhpStorm PyCharm RubyMine WebStorm AppCode CLion .NET & Visual Studio developer productivity tools ReSharper ReSharper С++ dotCover dotTrace dotMemory dotPeek

Transcript of Kotlin: Why Do You Care?

Page 1: Kotlin: Why Do You Care?

Kotlin in the CommunityTeamProductivity Tools

LanguagesKotlin

MPS

TeamCity

YouTrack

Upsource

IDEsIntelliJ IDEA

PhpStorm

PyCharm

RubyMine

WebStorm

AppCode

CLion

.NET & Visual Studiodeveloper productivity tools

ReSharper

ReSharper С++

dotCover

dotTrace

dotMemory

dotPeek

Page 2: Kotlin: Why Do You Care?

Kotlin: Why Do You Care?

Dmitry Jemerov <[email protected]>

2

Page 3: Kotlin: Why Do You Care?

3

Page 4: Kotlin: Why Do You Care?

• Statically typed programming language targeting the JVM

• Support for functional and OO paradigms

• Pragmatic, safe, concise, great Java interop

• Free and open-source

• In public beta, 1.0 coming Real Soon Now

4

Page 5: Kotlin: Why Do You Care?

Outline• Why Kotlin was created

• Kotlin feature highlights

• How and why we’re using Kotlin

• Where Kotlin works best

• How you can benefit from Kotlin in your projects

5

Page 6: Kotlin: Why Do You Care?

Why was Kotlin created?

6

Page 7: Kotlin: Why Do You Care?

“We’ve built tools to support so many nice languages, and we’re still using Java”

7

JetBrains, 2010

Page 8: Kotlin: Why Do You Care?

Developer Productivity

8

Page 9: Kotlin: Why Do You Care?

Develop with Pleasure

9

Page 10: Kotlin: Why Do You Care?

Thought Leadership

• Programming languages are a popular discussion topic

• IntelliJ IDEA is always going to have best support for Kotlin

10

Page 11: Kotlin: Why Do You Care?

Good chance of success

• Technical: experience building intelligent tools to support other languages

• Marketing: company reputation and user trust

11

Page 12: Kotlin: Why Do You Care?

Kotlin Feature Highlights

12

Page 13: Kotlin: Why Do You Care?

Primary Constructorclass Person(val firstName: String, val lastName: String) {

}

Page 14: Kotlin: Why Do You Care?

Propertiesclass Person(val firstName: String, val lastName: String) { val fullName: String get() = "$firstName $lastName" }

Page 15: Kotlin: Why Do You Care?

Propertiesclass Person(val firstName: String, val lastName: String) { val fullName: String get() = "$firstName $lastName" }

Page 16: Kotlin: Why Do You Care?

Smart Castsinterface Exprclass Constant(val value: Double) : Exprclass Sum(val op1: Expr, val op2: Expr) : Expr

Page 17: Kotlin: Why Do You Care?

Smart Castsfun eval(e: Expr): Double = when(e) {

}

Page 18: Kotlin: Why Do You Care?

Smart Castsfun eval(e: Expr): Double = when(e) {

}

Page 19: Kotlin: Why Do You Care?

Smart Castsfun eval(e: Expr): Double = when(e) { is Constant -> e.value

}

Page 20: Kotlin: Why Do You Care?

Smart Castsfun eval(e: Expr): Double = when(e) { is Constant -> e.value

}

class Constant(val value: Double) : Expr

Page 21: Kotlin: Why Do You Care?

Smart Castsfun eval(e: Expr): Double = when(e) { is Constant -> e.value is Sum -> eval(e.op1) + eval(e.op2) else -> throw IllegalArgumentException()}

class Sum(val op1: Expr, val op2: Expr) : Expr

Page 22: Kotlin: Why Do You Care?

Extension Functions

fun String.lastChar() = this.get(this.length - 1) "abc".lastChar()

Page 23: Kotlin: Why Do You Care?

Extension Functions

fun String.lastChar() = this.get(this.length - 1) "abc".lastChar()

Page 24: Kotlin: Why Do You Care?

Extension Functions

fun String.lastChar() = this.get(this.length - 1) "abc".lastChar()

Page 25: Kotlin: Why Do You Care?

Nullability

Page 26: Kotlin: Why Do You Care?

Nullabilityclass TreeNode(val parent: TreeNode?, val left: TreeNode?, val right: TreeNode?) {

}

Page 27: Kotlin: Why Do You Care?

Nullabilityclass TreeNode(val parent: TreeNode?, val left: TreeNode?, val right: TreeNode?) { fun depth(): Int { val parentDepth = if (parent != null) parent.depth() else 0 return parentDepth + 1 } }

Page 28: Kotlin: Why Do You Care?

Nullabilityclass TreeNode(val parent: TreeNode?, val left: TreeNode?, val right: TreeNode?) { fun depth(): Int { return (parent?.depth() ?: 0) + 1 } }

Page 29: Kotlin: Why Do You Care?

Nullabilityclass TreeNode(val parent: TreeNode?, val left: TreeNode?, val right: TreeNode?) { fun depth(): Int { return (parent?.depth() ?: 0) + 1 } }

Page 30: Kotlin: Why Do You Care?

Collections and Lambdas

fun nobleNames(persons: List<Person>) = persons .filter { "von " in it.lastName }

Page 31: Kotlin: Why Do You Care?

Collections and Lambdas

fun nobleNames(persons: List<Person>) = persons .filter { "von " in it.lastName }

Page 32: Kotlin: Why Do You Care?

Collections and Lambdas

fun nobleNames(persons: List<Person>) = persons .filter { "von " in it.lastName } .sortBy { it.firstName }

Page 33: Kotlin: Why Do You Care?

Collections and Lambdas

fun nobleNames(persons: List<Person>) = persons .filter { "von " in it.lastName } .sortBy { it.firstName } .map { "${it.firstName} ${it.lastName}” }

Page 34: Kotlin: Why Do You Care?

How JetBrains Uses Kotlin

34

Page 35: Kotlin: Why Do You Care?

Projects Using Kotlin• Kotlin

• JetProfile (CRM, sales and license management)

• IntelliJ IDEA 15

• YouTrack v.next

• 2 new unannounced projects

• Research projects related to bioinformatics

• Plugins, Intranet services etc.

35

Page 36: Kotlin: Why Do You Care?

JetProfile

36

Page 37: Kotlin: Why Do You Care?

Проект JetProfile

37

Page 38: Kotlin: Why Do You Care?

JetProfile Project

• Started in July 2013

• Replaces old Spring-based system

• 100% Kotlin, 100k LOC

• Team of 6 developers

38

Page 39: Kotlin: Why Do You Care?

IntelliJ IDEA

39

Page 40: Kotlin: Why Do You Care?

Kotlin in IntelliJ IDEA

• In tests since version 14, in production since 15

• 74k LOC in Kotlin, out of which 18k in tests

• A few developers actively using Kotlin

• Kotlin team provides support with updating to new Kotlin versions

40

Page 41: Kotlin: Why Do You Care?

Where Kotlin Works Best

Page 42: Kotlin: Why Do You Care?

The Strengths of Kotlin

• Modeling the data of your application concisely and expressively

• Creating reusable abstractions using functional programming techniques

• Creating expressive domain-specific languages

Page 43: Kotlin: Why Do You Care?

Kotlin on the Server Side

• Java interop ensures that all existing Java frameworks can be used

• No rewrite required to start using Kotlin in existing codebase

• Existing investment is fully preserved

Page 44: Kotlin: Why Do You Care?

Server-side Kotlin Frameworks

• Kara: Web framework powering JetProfile https://github.com/shafirov/kara

• Exposed: Database access framework https://github.com/JetBrains/exposed

• Ktor: Experimental next generation Web framework https://github.com/kotlin/ktor

Page 45: Kotlin: Why Do You Care?

Kara: HTML Buildersfun HtmlBodyTag.renderPersonList( persons: Collection<Person>) { table { for (person in persons) { tr { td { +person.name } td { +person.age } } } } }

45

Page 46: Kotlin: Why Do You Care?

Kara: HTML Buildersfun HtmlBodyTag.renderPersonList( persons: Collection<Person>) { table { for (person in persons) { tr { td { +person.name } td { +person.age } } } } }

46

Page 47: Kotlin: Why Do You Care?

Exposed: Table structure

object CountryTable : IdTable() { val name = varchar("name", 250) .uniqueIndex()

val iso = varchar("iso", 3) .uniqueIndex() val currency = varchar("currency", 3) }

47

Page 48: Kotlin: Why Do You Care?

Exposed: Entities

class Country(id: EntityID) : Entity(id) { var name: String by CountryTable.name val allCustomers by Customer.referrersOn( CustomerTable.country_id) }

48

Page 49: Kotlin: Why Do You Care?

Exposed: Queries

val germany = Country .find { CountryTable.iso eq "de" } .first()

49

Page 50: Kotlin: Why Do You Care?

Kotlin for Android• Compatible with Java 6

• Small runtime library

• Great tooling in Android Studio

• Compatible with existing Java libraries and tools

• 100% Java performance

Page 51: Kotlin: Why Do You Care?

Kotlin Tools for Android

• Kotlin Android Extensions: no more findViewById()

• Anko: DSL for UI, extension functions https://github.com/jetbrains/anko

• KApt: annotation processing

Page 52: Kotlin: Why Do You Care?

Kotlin Android Extensionsimport kotlinx.android.synthetic.activity_main.click_me_buttonpublic class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) click_me_button.onClick { toast("Thank you!") } } }

Page 53: Kotlin: Why Do You Care?

Kotlin Android Extensionsimport kotlinx.android.synthetic.activity_main.click_me_buttonpublic class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) click_me_button.onClick { toast("Thank you!") } } }

Page 54: Kotlin: Why Do You Care?

Kotlin Android Extensionsimport kotlinx.android.synthetic.activity_main.click_me_buttonpublic class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) click_me_button.onClick { toast("Thank you!") } } }

Page 55: Kotlin: Why Do You Care?

Anko: Android UI Builders

tableLayout { for (location in locations) { tableRow { textView(text = location.name) .lparams(column = 1) } }}

Page 56: Kotlin: Why Do You Care?

Kotlin in the Community

Page 57: Kotlin: Why Do You Care?

How to succeed with Kotlin

57

Page 58: Kotlin: Why Do You Care?

Take initiative

58

Page 59: Kotlin: Why Do You Care?

Take initiative

• Kotlin is not a silver bullet and not a magical solution for the problems of your project

• Kotlin makes you more productive and your work more enjoyable

• Try it and share the results with your teammates

59

Page 60: Kotlin: Why Do You Care?

Don’t wait for a new project

• Kotlin can be easily integrated into existing Java codebases

60

Page 61: Kotlin: Why Do You Care?

Where to start using Kotlin

• Tests - OK not informative

• Plugins and utilities - better

• Core and frameworks - best

61

Page 62: Kotlin: Why Do You Care?

Don’t fear the learning curve

• At JetBrains, summer interns become productive with Kotlin very quickly and deliver great results

62

Page 63: Kotlin: Why Do You Care?

Use the Java to Kotlin converter

• Convert only the classes that you’re going to modify

• Cleanup and refactor the code produced by the converter

63

Page 64: Kotlin: Why Do You Care?

Use the Java to Kotlin converter

64

Page 65: Kotlin: Why Do You Care?

Summary

• JetBrains entrusts the most business-critical tasks to Kotlin

• Kotlin works great for both server-side and Android development

• Take initiative and start using Kotlin today!

65

Page 66: Kotlin: Why Do You Care?
Page 67: Kotlin: Why Do You Care?

Q&A

https://kotlinlang.org/ https://try.kotlinlang.org/

@kotlin

[email protected] @intelliyole

67