Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Klean Code with Kotlin
Search
oshai
November 26, 2019
Technology
80
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
38
KScript
oshai
0
22
JVM languages shootout - Java, Scala & *Kotlin*
oshai
0
67
Koroutinify ; Lessons learned from applying Coroutines in Kotlin Backend ; Deep dive into Coroutines
oshai
1
140
Klean that Code, Boil those Boilerplates
oshai
2
100
Scala vs. Kotlin; Friend or Foe?
oshai
0
180
No forks, One star. Now what?! — How I published my Kotlin Open-Source lib
oshai
0
90
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
63
Other Decks in Technology
See All in Technology
USB PDを使ってスマホを労わりたい
raspython3
0
180
AI時代のPRレビューにどう向き合うか?
kairim0
1
240
Temporal 最高!でも改めて「日付」について調べたら人類の負債すぎる件
riyaamemiya
0
150
20261017福岡devIO 「AIは"良いダッシュボード"を作れるか?
jtakeda
0
160
しろおびから始める!ログ活用の第一歩
shumei_ito
0
950
触りたくないデータに、どう触るか AI時代のRBACアクセスガバナンス
ivry_presentationmaterials
0
270
脆弱性診断って、何をしているの? 〜弱点を見つけて、会社の改善につなげるまで〜
keeg
0
920
TinyGoでオブザーバビリティ?OpenTelemetryはTinyGoで使えるのか / Is OpenTelemetry available in TinyGo environment?
ymotongpoo
2
140
AIにシークレットを見せない、WSL2で安全な開発環境
yazm
0
300
go-sqletch
moznion
0
140
QA4AI 2026-Oct 生成AI時代の品質保証
tokuty
1
860
オントロジーって 結局なにもの? AI エージェントに「意味」を渡す知識設計の話
greenmidori
0
210
Featured
See All Featured
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
600
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
1
570
Designing Dashboards & Data Visualisations in Web Apps
destraynor
232
55k
The Mindset for Success: Future Career Progression
greggifford
PRO
0
540
Fireside Chat
paigeccino
43
4.1k
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
380
Intergalactic Javascript Robots from Outer Space
tanoku
273
28k
Leo the Paperboy
mayatellez
10
2.4k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.6k
Claude Code のすすめ
schroneko
67
230k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
530
Testing 201, or: Great Expectations
jmmastey
46
8.3k
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