SlideShare a Scribd company logo
Better Apps with Kotlin!
Pavel Shackih, SBT
t.me/PavelShackih
Motivation
Android core app quality: the app does not crash, force
close, freeze, or otherwise function abnormally on any
targeted device.
Motivation
Android core app quality: the app does not crash, force
close, freeze, or otherwise function abnormally on any
targeted device.
JSR-305, Android Support Annotations
@Nonnull
@Override
public User save(@Nonnull User user) {
// ...
userDao.save(user);
// ...
return user;
}
JSR-305, Android Support Annotations
1. Hard to maintain.
2. Verbose, but could be simplified using @ParametersAreNonnullByDefault or
@ParametersAreNullableByDefault.
3. Just warning.
4. “Leak” NPE.
5. ~6500 in SBOL.
Optional<T>
@Override
public Optional<User> findById(long id) {
return Optional.ofNullable(userDao.findById(id));
}
public void printUser(long id) {
Optional<User> optional = findById(id);
optional.ifPresent(user -> System.out.println(user));
}
Optional<T>
1. Verbose.
2. GC overhead.
3. Problem with third-party libraries.
Боремся с NPE вместе с Kotlin, Павел Шацких СберТех
Here comes Kotlin
fun main(args: Array<String>) {
val hello = "Hello, Innopolis!"
println(hello)
}
Here comes Kotlin
● statically-typed, object-oriented, functional;
● target: JVM, Android, JS, Native (in progress);
● fully interoperable with Java;
● compact runtime lib, good tooling;
● runs as fast as an equivalent on Java;
● easy to learn for Java developers;
● official Android development language.
Not nullable types by default
var foo: String = "Foo" // var foo = "Foo"
foo = null // Compile error: Null can not be a value of a non-null type String
var bar: String? = "Bar"
bar = null // Ok
bar = foo // Ok
foo = bar // Error
Not nullable types by default
String
String?
String? = String or null
Deal with nullable types
val user: User? = null
println(user.address.street)
// Error: only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable
receiver of type kotlin.String?
Smartcast
val user: User? = null
if (user != null && user.address != null) {
println(user.address.street) // Now: user: User, address: Address
}
User?
User
Address?
Address
Safe call operator ?.
val user: User? = null
println(user?.address)
if (user != null) {
return user.getAddress();
} else {
return null;
}
user?.address
user.address
null
*Kotlin in Action
user != null
user == null
Safe call operator always returns
nullable type of <T>, so <T> result type
transform to type <T?>
println(user?.address?.street)
Chaining safe call operators:
Elvis operator ?:
val user: User? = null
println(user ?: ADMIN)
if (user != null) {
return user;
} else {
return ADMIN;
}
user
user
ADMIN
*Kotlin in Action
user != null
user == null
println(user?.address?.street ?: UNKNOWN_STREET)
Let function
val user: User? = null
user?.let { println(it) }
if (user != null) {
System.out.println(user);
}
user
println(user)
user != null
*Kotlin in Action
Not null assertions: !! operator
val user: User? = null
println(user!!.address)
System.out.println(user.getAddress());
user
user.address
NullPointerException
*Kotlin in Action
user != null
user == null
Platform types and Java interoperability
Java Kotlin
@Nullable + String String?
@NotNull + String String
String String!
String! = String or String?
val stringValue = Java.getString()
println(stringValue.length) // Ok
println(stringValue?.length) // Ok
Kotlin nullability: Examples
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
val id = savedInstanceState.getString(ID) // compile error
}
}
class MyIntentService : IntentService("MyIntentService") {
override fun onHandleIntent(intent: Intent?) {
val param1 = intent.getStringExtra(EXTRA_PARAM1) // compile error
}
}
Intrinsics
● Apply Intrinsics.checkParameterIsNotNull() for every public function for each
non-null parameter.
● Disable using -Xno-param-assertions or Proguard.
JDK 1.8 Kotlin
@Override
public void forEachRemaining(DoubleConsumer
action) {
Objects.requireNonNull(action);
if (count == -2) {
action.accept(first);
count = -1;
}
}
@NotNull
public User save(@NotNull User user) {
Intrinsics.checkParameterIsNotNull(user, "user");
this.userDao.save(user);
return user;
}
Kotlin nullability
1. Hard to maintain built-in type system.
2. Verbose special syntax for nullable values.
3. Not error, just a warning, no guarantees compile time error.
4. “Leak” NPE kotlin intrinsics.
5. GC overhead no overhead at all.
6. Problem with third-party libraries kotlin compiler could read default java
annotations from external byte code.
Extension functions
Java Kotlin
public static boolean isEmpty(@Nullable String string) {
return string == null || string.isEmpty();
}
fun String?.isEmpty(): Boolean {
return this == null || this.length == 0
}
String text = ...;
if (isEmpty(text)) {
System.out.println("Empty text!");
}
val text = ...
if (text.isEmpty()) {
println("Empty text!")
}
Extension functions: Lambdas
fun String.printWithCondition(predicate: (s: String) -> Boolean) {
if (predicate(this)) {
println(this)
}
}
val text = "Hello"
text.printWithCondition { it == "Hello" }
Extension functions: Lambdas with inline
inline fun String.printWithCondition(predicate: (s: String) -> Boolean) {
if (predicate(this)) {
println(this)
}
}
val text = "Hello"
text.printWithCondition { it == "Hello" }
String text = "Hello";
if(Intrinsics.areEqual(text, "Hello")) {
System.out.println(text);
}
Extension functions: AutoCloseable
val fos = FileOutputStream("data.txt")
val result = fos.use {
// do some stuff with stream
"result"
}
println(result)
val resultSet: ResultSet? = ...
resultSet?.use {
println(it.getString(USER_ID))
}
Extension functions: Collections
val list = listOf("Foo", "Bar", "Buzz")
list.filter { it.startsWith("B") } // [Bar, Buzz]
list.map { it.toUpperCase() } // [FOO, BAR, BUZZ]
list.flatMap { it.asIterable() } // [F, o, o, B, a, r, B, u, z, z]
list.groupBy { it.first() } // {F=[Foo], B=[Bar, Buzz]}
list.fold(0, { acc, s -> acc + s.length }) // 10
list.reduce { acc, s -> s + acc } // BuzzBarFoo
Extension functions: Collections
1. Immutable by default.
2. Mutable collections should be created explicitly, e.g. by mutableListOf(),
mutableSetOf(), mutableMapOf() or using constructors.
3. Not lazy by default: each transformation → new collection.
4. Sequences are lazy like a Java 8 Streams. No parallelism.
5. Default implementations: ArrayList, HashSet and HashMap.
6. Array supports same functional operators.
Extension functions: Lambda Extensions
inline fun String.printWithCondition(predicate: String.() -> Boolean) {
if (predicate()) { // predicate(this)
println(this)
}
}
val text = "Hello"
text.printWithCondition { length > 3 }
Extension functions: Standard functions
public inline fun <T> T.apply(block: T.() -> Unit): T { block(); return this }
val user = User().apply {
id = 1L
name = "John Smith"
}
public inline fun <T, R> T.let(block: (T) -> R): R = block(this)
user?.let {
println(user.name)
}
Conclusion: tools
1. Dagger 2, Butterknife, RxJava 2, Retrofit 2, Realm, Room...
2. Built-in plugin in AS 3.0.
3. Static analysis - https://github.com/arturbosch/detekt.
4. https://kotlinlang.org/docs/reference/coding-conventions.html.
5. Proguard - ok.
6. Debugging - as java.
7. Compile speed - as java - https://habrahabr.ru/company/badoo/blog/329026/.
8. Java projects: Spring 5, Reactor, Corda, Gradle, Spark.
9. Android projects: Hotellook, Avito.
Conclusion: next
1. Intro to Kotlin at Google IO 17: https://youtu.be/X1RVYt2QKQE.
2. Jake Wharton and Christina Lee at Google IO 17:
https://youtu.be/fPzxfeDJDzY.
3. https://developer.android.com/kotlin/index.html.
4. https://kotlinlang.org/docs/reference/.
5. 10 Kotlin Tricks in 10 ish minutes by Jake Wharton:
https://youtu.be/0sPzDwS55wM.
6. Kotlin in Action: https://www.manning.com/books/kotlin-in-action.
7. Kotlin type system: http://natpryce.com/articles/000818.html.
8. Perfomance: https://goo.gl/J2FVSm and https://goo.gl/CDKxw9.
Conclusion: community
1. https://t.me/kotlin_lang @kotlin_lang.
2. http://slack.kotlinlang.org/.
3. http://www.kotlinweekly.net/.

More Related Content

What's hot (19)

PDF
Apache Commons - Don\'t re-invent the wheel
tcurdt
 
PDF
Concurrency Concepts in Java
Doug Hawkins
 
PDF
Java Generics - by Example
Ganesh Samarthyam
 
PDF
Kotlin cheat sheet by ekito
Arnaud Giuliani
 
PPTX
Use of Apache Commons and Utilities
Pramod Kumar
 
PDF
Develop your next app with kotlin @ AndroidMakersFr 2017
Arnaud Giuliani
 
PPTX
A topology of memory leaks on the JVM
Rafael Winterhalter
 
PPT
Java performance
Sergey D
 
PPTX
Code generation for alternative languages
Rafael Winterhalter
 
PDF
Twins: Object Oriented Programming and Functional Programming
RichardWarburton
 
PDF
1 kotlin vs. java: some java issues addressed in kotlin
Sergey Bandysik
 
PPTX
The Java memory model made easy
Rafael Winterhalter
 
PDF
Transaction is a monad
Jarek Ratajski
 
PPTX
모던자바의 역습
DoHyun Jung
 
PPTX
Java byte code in practice
Rafael Winterhalter
 
PPT
Swiss army knife Spring
Mario Fusco
 
PDF
Java Class Design
Ganesh Samarthyam
 
PPTX
Akka in-action
Raymond Roestenburg
 
PDF
Kotlin, smarter development for the jvm
Arnaud Giuliani
 
Apache Commons - Don\'t re-invent the wheel
tcurdt
 
Concurrency Concepts in Java
Doug Hawkins
 
Java Generics - by Example
Ganesh Samarthyam
 
Kotlin cheat sheet by ekito
Arnaud Giuliani
 
Use of Apache Commons and Utilities
Pramod Kumar
 
Develop your next app with kotlin @ AndroidMakersFr 2017
Arnaud Giuliani
 
A topology of memory leaks on the JVM
Rafael Winterhalter
 
Java performance
Sergey D
 
Code generation for alternative languages
Rafael Winterhalter
 
Twins: Object Oriented Programming and Functional Programming
RichardWarburton
 
1 kotlin vs. java: some java issues addressed in kotlin
Sergey Bandysik
 
The Java memory model made easy
Rafael Winterhalter
 
Transaction is a monad
Jarek Ratajski
 
모던자바의 역습
DoHyun Jung
 
Java byte code in practice
Rafael Winterhalter
 
Swiss army knife Spring
Mario Fusco
 
Java Class Design
Ganesh Samarthyam
 
Akka in-action
Raymond Roestenburg
 
Kotlin, smarter development for the jvm
Arnaud Giuliani
 

Similar to Боремся с NPE вместе с Kotlin, Павел Шацких СберТех (20)

PDF
Kotlin: A pragmatic language by JetBrains
Jigar Gosar
 
PDF
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
Codemotion
 
PDF
Kotlin what_you_need_to_know-converted event 4 with nigerians
junaidhasan17
 
PPTX
Kotlin as a Better Java
Garth Gilmour
 
PDF
Kotlin for Android devs
Adit Lal
 
PDF
Be More Productive with Kotlin
Brandon Wever
 
PDF
Privet Kotlin (Windy City DevFest)
Cody Engel
 
PDF
Poniendo Kotlin en producción a palos (Kotlin in production, the hard way)
Andrés Viedma Peláez
 
PDF
Kotlin for Android - Vali Iorgu - mRready
MobileAcademy
 
PDF
A quick and fast intro to Kotlin
XPeppers
 
PPTX
Android & Kotlin - The code awakens #02
Omar Miatello
 
PPTX
KotlinForJavaDevelopers-UJUG.pptx
Ian Robertson
 
PPTX
Intro to kotlin 2018
Shady Selim
 
PPTX
Introduction to Koltin for Android Part I
Atif AbbAsi
 
PPTX
Introduction to Kotlin Language and its application to Android platform
EastBanc Tachnologies
 
PDF
Kotlin a problem solver - gdd extended pune
Hardik Trivedi
 
PDF
Kotlin
Nodirbek Usmanov
 
PDF
Kotlin: maybe it's the right time
Davide Cerbo
 
PDF
Exploring Kotlin
Johan Haleby
 
Kotlin: A pragmatic language by JetBrains
Jigar Gosar
 
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
Codemotion
 
Kotlin what_you_need_to_know-converted event 4 with nigerians
junaidhasan17
 
Kotlin as a Better Java
Garth Gilmour
 
Kotlin for Android devs
Adit Lal
 
Be More Productive with Kotlin
Brandon Wever
 
Privet Kotlin (Windy City DevFest)
Cody Engel
 
Poniendo Kotlin en producción a palos (Kotlin in production, the hard way)
Andrés Viedma Peláez
 
Kotlin for Android - Vali Iorgu - mRready
MobileAcademy
 
A quick and fast intro to Kotlin
XPeppers
 
Android & Kotlin - The code awakens #02
Omar Miatello
 
KotlinForJavaDevelopers-UJUG.pptx
Ian Robertson
 
Intro to kotlin 2018
Shady Selim
 
Introduction to Koltin for Android Part I
Atif AbbAsi
 
Introduction to Kotlin Language and its application to Android platform
EastBanc Tachnologies
 
Kotlin a problem solver - gdd extended pune
Hardik Trivedi
 
Kotlin: maybe it's the right time
Davide Cerbo
 
Exploring Kotlin
Johan Haleby
 
Ad

More from Сбертех | SberTech (11)

PDF
Есть ли жизнь без Dagger'a, Михаил Крестьянинов Avito
Сбертех | SberTech
 
PDF
Feature toggles в процессе подбора, Алексей Ульенков СберТех
Сбертех | SberTech
 
PDF
Чистая архитектура, Артур Бадретдинов АБЦТ
Сбертех | SberTech
 
PDF
Модульная архитектура Сбербанк Онлайн, Владимир Озеров и Александр Черушнико...
Сбертех | SberTech
 
PDF
Один день из жизни iOs разработчика, Александр Сычёв Rambler&Co
Сбертех | SberTech
 
PDF
Аспекты применения Agile для крупных хранилищ данных
Сбертех | SberTech
 
PDF
Internet of things
Сбертех | SberTech
 
PDF
Биометрия и платежи
Сбертех | SberTech
 
PDF
самое интересное в мире блокчейн, опыт и рецепты от сбербанка
Сбертех | SberTech
 
PDF
Подходы к построению хранилищ данных в крупных организациях
Сбертех | SberTech
 
Есть ли жизнь без Dagger'a, Михаил Крестьянинов Avito
Сбертех | SberTech
 
Feature toggles в процессе подбора, Алексей Ульенков СберТех
Сбертех | SberTech
 
Чистая архитектура, Артур Бадретдинов АБЦТ
Сбертех | SberTech
 
Модульная архитектура Сбербанк Онлайн, Владимир Озеров и Александр Черушнико...
Сбертех | SberTech
 
Один день из жизни iOs разработчика, Александр Сычёв Rambler&Co
Сбертех | SberTech
 
Аспекты применения Agile для крупных хранилищ данных
Сбертех | SberTech
 
Internet of things
Сбертех | SberTech
 
Биометрия и платежи
Сбертех | SberTech
 
самое интересное в мире блокчейн, опыт и рецепты от сбербанка
Сбертех | SberTech
 
Подходы к построению хранилищ данных в крупных организациях
Сбертех | SberTech
 
Ad

Recently uploaded (20)

PDF
Predicting the unpredictable: re-engineering recommendation algorithms for fr...
Speck&Tech
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PPTX
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PDF
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
PPTX
Building a Production-Ready Barts Health Secure Data Environment Tooling, Acc...
Barts Health
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
PDF
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
PPTX
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
PDF
Wojciech Ciemski for Top Cyber News MAGAZINE. June 2025
Dr. Ludmila Morozova-Buss
 
Predicting the unpredictable: re-engineering recommendation algorithms for fr...
Speck&Tech
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
Building a Production-Ready Barts Health Secure Data Environment Tooling, Acc...
Barts Health
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
Wojciech Ciemski for Top Cyber News MAGAZINE. June 2025
Dr. Ludmila Morozova-Buss
 

Боремся с NPE вместе с Kotlin, Павел Шацких СберТех

  • 1. Better Apps with Kotlin! Pavel Shackih, SBT t.me/PavelShackih
  • 2. Motivation Android core app quality: the app does not crash, force close, freeze, or otherwise function abnormally on any targeted device.
  • 3. Motivation Android core app quality: the app does not crash, force close, freeze, or otherwise function abnormally on any targeted device.
  • 4. JSR-305, Android Support Annotations @Nonnull @Override public User save(@Nonnull User user) { // ... userDao.save(user); // ... return user; }
  • 5. JSR-305, Android Support Annotations 1. Hard to maintain. 2. Verbose, but could be simplified using @ParametersAreNonnullByDefault or @ParametersAreNullableByDefault. 3. Just warning. 4. “Leak” NPE. 5. ~6500 in SBOL.
  • 6. Optional<T> @Override public Optional<User> findById(long id) { return Optional.ofNullable(userDao.findById(id)); } public void printUser(long id) { Optional<User> optional = findById(id); optional.ifPresent(user -> System.out.println(user)); }
  • 7. Optional<T> 1. Verbose. 2. GC overhead. 3. Problem with third-party libraries.
  • 9. Here comes Kotlin fun main(args: Array<String>) { val hello = "Hello, Innopolis!" println(hello) }
  • 10. Here comes Kotlin ● statically-typed, object-oriented, functional; ● target: JVM, Android, JS, Native (in progress); ● fully interoperable with Java; ● compact runtime lib, good tooling; ● runs as fast as an equivalent on Java; ● easy to learn for Java developers; ● official Android development language.
  • 11. Not nullable types by default var foo: String = "Foo" // var foo = "Foo" foo = null // Compile error: Null can not be a value of a non-null type String var bar: String? = "Bar" bar = null // Ok bar = foo // Ok foo = bar // Error
  • 12. Not nullable types by default String String? String? = String or null
  • 13. Deal with nullable types val user: User? = null println(user.address.street) // Error: only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type kotlin.String?
  • 14. Smartcast val user: User? = null if (user != null && user.address != null) { println(user.address.street) // Now: user: User, address: Address } User? User Address? Address
  • 15. Safe call operator ?. val user: User? = null println(user?.address) if (user != null) { return user.getAddress(); } else { return null; } user?.address user.address null *Kotlin in Action user != null user == null Safe call operator always returns nullable type of <T>, so <T> result type transform to type <T?> println(user?.address?.street) Chaining safe call operators:
  • 16. Elvis operator ?: val user: User? = null println(user ?: ADMIN) if (user != null) { return user; } else { return ADMIN; } user user ADMIN *Kotlin in Action user != null user == null println(user?.address?.street ?: UNKNOWN_STREET)
  • 17. Let function val user: User? = null user?.let { println(it) } if (user != null) { System.out.println(user); } user println(user) user != null *Kotlin in Action
  • 18. Not null assertions: !! operator val user: User? = null println(user!!.address) System.out.println(user.getAddress()); user user.address NullPointerException *Kotlin in Action user != null user == null
  • 19. Platform types and Java interoperability Java Kotlin @Nullable + String String? @NotNull + String String String String! String! = String or String? val stringValue = Java.getString() println(stringValue.length) // Ok println(stringValue?.length) // Ok
  • 20. Kotlin nullability: Examples class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main2) val id = savedInstanceState.getString(ID) // compile error } } class MyIntentService : IntentService("MyIntentService") { override fun onHandleIntent(intent: Intent?) { val param1 = intent.getStringExtra(EXTRA_PARAM1) // compile error } }
  • 21. Intrinsics ● Apply Intrinsics.checkParameterIsNotNull() for every public function for each non-null parameter. ● Disable using -Xno-param-assertions or Proguard. JDK 1.8 Kotlin @Override public void forEachRemaining(DoubleConsumer action) { Objects.requireNonNull(action); if (count == -2) { action.accept(first); count = -1; } } @NotNull public User save(@NotNull User user) { Intrinsics.checkParameterIsNotNull(user, "user"); this.userDao.save(user); return user; }
  • 22. Kotlin nullability 1. Hard to maintain built-in type system. 2. Verbose special syntax for nullable values. 3. Not error, just a warning, no guarantees compile time error. 4. “Leak” NPE kotlin intrinsics. 5. GC overhead no overhead at all. 6. Problem with third-party libraries kotlin compiler could read default java annotations from external byte code.
  • 23. Extension functions Java Kotlin public static boolean isEmpty(@Nullable String string) { return string == null || string.isEmpty(); } fun String?.isEmpty(): Boolean { return this == null || this.length == 0 } String text = ...; if (isEmpty(text)) { System.out.println("Empty text!"); } val text = ... if (text.isEmpty()) { println("Empty text!") }
  • 24. Extension functions: Lambdas fun String.printWithCondition(predicate: (s: String) -> Boolean) { if (predicate(this)) { println(this) } } val text = "Hello" text.printWithCondition { it == "Hello" }
  • 25. Extension functions: Lambdas with inline inline fun String.printWithCondition(predicate: (s: String) -> Boolean) { if (predicate(this)) { println(this) } } val text = "Hello" text.printWithCondition { it == "Hello" } String text = "Hello"; if(Intrinsics.areEqual(text, "Hello")) { System.out.println(text); }
  • 26. Extension functions: AutoCloseable val fos = FileOutputStream("data.txt") val result = fos.use { // do some stuff with stream "result" } println(result) val resultSet: ResultSet? = ... resultSet?.use { println(it.getString(USER_ID)) }
  • 27. Extension functions: Collections val list = listOf("Foo", "Bar", "Buzz") list.filter { it.startsWith("B") } // [Bar, Buzz] list.map { it.toUpperCase() } // [FOO, BAR, BUZZ] list.flatMap { it.asIterable() } // [F, o, o, B, a, r, B, u, z, z] list.groupBy { it.first() } // {F=[Foo], B=[Bar, Buzz]} list.fold(0, { acc, s -> acc + s.length }) // 10 list.reduce { acc, s -> s + acc } // BuzzBarFoo
  • 28. Extension functions: Collections 1. Immutable by default. 2. Mutable collections should be created explicitly, e.g. by mutableListOf(), mutableSetOf(), mutableMapOf() or using constructors. 3. Not lazy by default: each transformation → new collection. 4. Sequences are lazy like a Java 8 Streams. No parallelism. 5. Default implementations: ArrayList, HashSet and HashMap. 6. Array supports same functional operators.
  • 29. Extension functions: Lambda Extensions inline fun String.printWithCondition(predicate: String.() -> Boolean) { if (predicate()) { // predicate(this) println(this) } } val text = "Hello" text.printWithCondition { length > 3 }
  • 30. Extension functions: Standard functions public inline fun <T> T.apply(block: T.() -> Unit): T { block(); return this } val user = User().apply { id = 1L name = "John Smith" } public inline fun <T, R> T.let(block: (T) -> R): R = block(this) user?.let { println(user.name) }
  • 31. Conclusion: tools 1. Dagger 2, Butterknife, RxJava 2, Retrofit 2, Realm, Room... 2. Built-in plugin in AS 3.0. 3. Static analysis - https://github.com/arturbosch/detekt. 4. https://kotlinlang.org/docs/reference/coding-conventions.html. 5. Proguard - ok. 6. Debugging - as java. 7. Compile speed - as java - https://habrahabr.ru/company/badoo/blog/329026/. 8. Java projects: Spring 5, Reactor, Corda, Gradle, Spark. 9. Android projects: Hotellook, Avito.
  • 32. Conclusion: next 1. Intro to Kotlin at Google IO 17: https://youtu.be/X1RVYt2QKQE. 2. Jake Wharton and Christina Lee at Google IO 17: https://youtu.be/fPzxfeDJDzY. 3. https://developer.android.com/kotlin/index.html. 4. https://kotlinlang.org/docs/reference/. 5. 10 Kotlin Tricks in 10 ish minutes by Jake Wharton: https://youtu.be/0sPzDwS55wM. 6. Kotlin in Action: https://www.manning.com/books/kotlin-in-action. 7. Kotlin type system: http://natpryce.com/articles/000818.html. 8. Perfomance: https://goo.gl/J2FVSm and https://goo.gl/CDKxw9.
  • 33. Conclusion: community 1. https://t.me/kotlin_lang @kotlin_lang. 2. http://slack.kotlinlang.org/. 3. http://www.kotlinweekly.net/.