Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Klean Code with Kotlin
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
oshai
November 26, 2019
Technology
77
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Klean Code with Kotlin
oshai
November 26, 2019
More Decks by oshai
See All by oshai
Maintaining an Open Source The Good, Bad & Ugly
oshai
0
37
KScript
oshai
0
21
JVM languages shootout - Java, Scala & *Kotlin*
oshai
0
63
Koroutinify ; Lessons learned from applying Coroutines in Kotlin Backend ; Deep dive into Coroutines
oshai
1
130
Klean that Code, Boil those Boilerplates
oshai
2
93
Scala vs. Kotlin; Friend or Foe?
oshai
0
170
No forks, One star. Now what?! — How I published my Kotlin Open-Source lib
oshai
0
81
Scala--pack your Future[T]; Kotlin is coming! (Kotlin TLV)
oshai
0
210
X tips [X==9] for building a Bulletproof Deployment Pipeline with Jenkins
oshai
0
61
Other Decks in Technology
See All in Technology
Guerilla InnerSource in enterprises, during the AI hype
onenashev
PRO
0
130
Claude Codeで開発以外の業務も爆速化しよう!
minorun365
PRO
12
9.4k
Introduction to Bill One Development Engineer
sansan33
PRO
0
490
Bill One 開発エンジニア 紹介資料
sansan33
PRO
7
20k
【5分でわかる】セーフィー エンジニア向け会社紹介
safie_recruit
0
55k
Does an AI Watermark Survive Translation?
machinetranslation
0
510
20260903 Tokyo Jazug Night #62 | Azure エンジニアよ、 その環境は本当にセキュアか?
olivia_0707
1
260
少人数データチームのDevin活用実践事例
runandy16
2
140
AI時代におけるプロダクト横断勉強会の設計
zozotech
PRO
0
100
AWSとGitHub Actionsの責任境界と 組織で安全に使用する取り組み
nealle
1
240
DGX Sparkを2台使って いろいろ動かす話
sonoda_mj
1
100
PdMをやめて、 "プロダクトビルダー"という 働き方に変えました / PdM to Product Builder
shikichee
2
610
Featured
See All Featured
The agentic SEO stack - context over prompts
schlessera
0
880
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.8k
The untapped power of vector embeddings
frankvandijk
2
1.9k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
430
Product Roadmaps are Hard
iamctodd
55
13k
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
6
1.2k
The Language of Interfaces
destraynor
162
27k
[SF Ruby Conf 2025] Rails X
palkan
2
1.3k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
What's in a price? How to price your products and services
michaelherold
247
13k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
680
Transcript
Klean Code with Kotlin
IS THERE A DOCTOR IN THE AUDIENCE?
IS THERE A DOCTOR JAVA DEVELOPER IN THE AUDIENCE?
Should I start using Kotlin?
TL;DL* - YES! *Too Long; Didn’t Listen
• Look at trends • Compare Kotlin to Java •
More Kotlin features Plan B
Google Trends - Kotlin
None
None
None
None
None
Why do developers ❤ Kotlin?
None
Boilerplate In computer programming, boilerplate code or boilerplate refers to
sections of code that have to be included in many places with little or no alteration. It is often used when referring to languages that are considered verbose, i.e. the programmer must write a lot of code to do minimal jobs.
Boilerplate boilerplate - little or no alteration. verbose a lot
of code to do minimal jobs.
Java is Verbose
IDE
Hello Kotlin! Java public class Hello { public static void
main(String[] args) { System.out.println("Hello!"); } } Kotlin fun main() { println("Hello!") }
POJO Class Java public class AdCpcData { private int adId;
private double cpc; }
POJO Class Java public class AdCpcData { private int adId;
private double cpc; public AdCpcData(final int adId, final double cpc) { this.adId = adId; this.cpc = cpc; } public double getCpc() { return cpc; } public int getAdId() { return adId; } public void setAdId(final int adId) { this.adId = adId; } public void setCpc(final double cpc) { this.cpc = cpc; } }
POJO Class Java public class AdCpcData { private int adId;
private double cpc; public AdCpcData(final int adId, final double cpc) { this.adId = adId; this.cpc = cpc; } public double getCpc() { return cpc; } public int getAdId() { return adId; } public void setAdId(final int adId) { this.adId = adId; } public void setCpc(final double cpc) { this.cpc = cpc; } } equals(), hashcode(), toString()…
POJO Class Java public class AdCpcData { private int adId;
private double cpc; public AdCpcData(final int adId, final double cpc) { this.adId = adId; this.cpc = cpc; } public double getCpc() { return cpc; } public int getAdId() { return adId; } public void setAdId(final int adId) { this.adId = adId; } public void setCpc(final double cpc) { this.cpc = cpc; } @Override public String toString() { return com.google.common.base.Objects.toStringHelper(this) .add("adId", adId) .add("cpc", cpc) .toString(); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; AdCpcData adCpcData = (AdCpcData) o; return adId == adCpcData.adId && Double.compare(adCpcData.cpc, cpc) == 0; } @Override public int hashCode() { return Objects.hash(adId, cpc); } } equals(), hashcode(), toString()…
POJO Class Java public class AdCpcData { private int adId;
private double cpc; public AdCpcData(final int adId, final double cpc) { this.adId = adId; this.cpc = cpc; } public double getCpc() { return cpc; } public int getAdId() { return adId; } public void setAdId(final int adId) { this.adId = adId; } public void setCpc(final double cpc) { this.cpc = cpc; } @Override public String toString() { return com.google.common.base.Objects.toStringHelper(this) .add("adId", adId) .add("cpc", cpc) .toString(); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; AdCpcData adCpcData = (AdCpcData) o; return adId == adCpcData.adId && Double.compare(adCpcData.cpc, cpc) == 0; } @Override public int hashCode() { return Objects.hash(adId, cpc); } } equals(), hashcode(), toString()… 57 LOC
Data Class Java public class AdCpcData { private int adId;
private double cpc; public AdCpcData(final int adId, final double cpc) { this.adId = adId; this.cpc = cpc; } public double getCpc() { return cpc; } public int getAdId() { return adId; } public void setAdId(final int adId) { this.adId = adId; } public void setCpc(final double cpc) { this.cpc = cpc; } @Override public String toString() { return com.google.common.base.Objects.toStringHelper(this) .add("adId", adId) .add("cpc", cpc) .toString(); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; AdCpcData adCpcData = (AdCpcData) o; return adId == adCpcData.adId && Double.compare(adCpcData.cpc, cpc) == 0; } @Override public int hashCode() { return Objects.hash(adId, cpc); } } Kotlin data class AdCpcData( var adId: Int, var cpc: Double )
Variable declaration Java private final Map<String, String> myMap = new
HashMap<>(); myMap.put("Hi", "There"); Kotlin val myMap = mapOf("Hi" to "There")
Testing Java @Test public void testShouldProcessCampaign_WhenFlyToInRange() throws Exception { …
} Kotlin @Test fun `when "fly to" is in range - should process the campaign`() { TODO() }
Java vs Kotlin • Many more boilerplate examples… • But
Java do make some progress…
None
• We saw some statistics • We learned about boilerplate
Recap
Some more cool features
Extension method fun Int.hours() = TimeUnit.HOURS.toMillis(this.toLong()) … 5.hours()
Collections val fruits = listOf("banana", "avocado", "apple", "kiwi") fruits .filter
{ it.startsWith("a") } .sortedBy { it } .map { it.toUpperCase() } .forEach { println(it) }
When Expression when (object) { 1 -> "One" "Hello" ->
"Greeting" is Long -> "Long" in 2..10 -> "obj is in the range" else -> "Unknown" }
Inter-Op with Java Java adCpcData.getAdId() Kotlin adCpcData.adId
Null Safety val world: String? = null println(world.toUpperCase()) // In
Java you will get NPE // In Kotlin you will get compilation error
Null Safety val world: String? = null if (world !=
null) { println(world.toUpperCase()) } // In Java you will get NPE // In Kotlin you will get compilation error
Many More Features • String Interpolation • Named Parameters •
Smart Casting • Contracts • Multiline Strings
Why Kotlin? • Concise • Safe • Interoperable • Tool-friendly
• Practical
None
⌘ + ⌥ + ⇧ + K
Questions?
Thank You! @OhadShai