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
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
64
Koroutinify ; Lessons learned from applying Coroutines in Kotlin Backend ; Deep dive into Coroutines
oshai
1
130
Klean that Code, Boil those Boilerplates
oshai
2
97
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
87
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
62
Other Decks in Technology
See All in Technology
リアーキテクチャ後の障害ゼロを目指したShadow Testingの取り組み
nihonbuson
PRO
1
180
AIに任せた品質は、誰が見立てるのか - AI時代のテストマネジメント
nakanao
3
3k
Goodbye ShellScript, Hello File-based App
shunsock
0
950
白金鉱業Meetup Vol.25 アウトカムが二値のデータに対するCausal Impact
brainpadpr
0
290
エージェントはローカル、検証はMicroVM — Lambda MicroVMsでつくるServerless CI
fujioka6789
3
780
Kiro Meetup #8 Kiro アップデート (2026/3/21〜2026/9/24)
katzueno
1
190
Harness Engineering on Rails
joelq
0
450
AI Agent入門〜今更聞けないAgentの話〜
hiromimaganuma
0
110
Deployment の 先にある AI Agent 基盤 - kagent vNext、Agent Substrate、Hermes から読み解く Agent Runtime の現在地 / k8s-matsuri-2-ai-agent-platform-amsy810
masayaaoyama
5
730
The Knowledge Spine: A Machine-Executable Ontology for Governed Marketing Activation
vananth22
0
130
Oracle Base Database Service 技術詳細
oracle4engineer
PRO
16
120k
品質と信頼性を地続きにする
grimoh
2
990
Featured
See All Featured
My Coaching Mixtape
mlcsv
0
320
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.8k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
Bootstrapping a Software Product
garrettdimon
PRO
306
120k
Darren the Foodie - Storyboard
khoart
PRO
4
3.9k
How Software Deployment tools have changed in the past 20 years
geshan
1
34k
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
2.2k
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
520
A Tale of Four Properties
chriscoyier
163
24k
The Language of Interfaces
destraynor
162
27k
Joys of Absence: A Defence of Solitary Play
codingconduct
1
530
Mind Mapping
helmedeiros
1
370
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