Idiomatic Kotlin for Android

Matthew Clarke
Matthew ClarkeSenior Software Developer at Spark New Zealand
Idiomatic Kotlin
(with Android flavouring)
Orion Health Auckland 24th June 2016
Idiomatic:
Pronunciation: /ˌɪdɪəˈmatɪk/
ADJECTIVE
1 Using, containing, or denoting expressions that are
natural to a native speaker
Or: Learning how to write
Kotlin-y Kotlin instead of
Java-y Kotlin
Example:
FragmentPagerAdapter
with 3 static pages
Idiomatic Kotlin for Android
Java version
DRY violation!
Meaningful information
Idiomatic Kotlin for Android
[
]
Objective (pseudo-code)
• Pass just the meaningful info to a new adapter
• Reduce noise/kill boilerplate
• Don’t repeat ourselves
What is this as actual code?
“A list of things that return Fragments”
Java = “A list of objects that return Fragments”
[
]
Java attempt
Introduce a SAM (Single Abstract Method) interface:
DRY
Let’s see the calling code
(e.g. Activity with a
ViewPager)…
Idiomatic Kotlin for Android
• Pass just the meaningful info to a new adapter
• Reduce noise/kill boilerplate
• Don’t repeat ourselves
Java version
✔
✔
❌
Java version
• Achieved DRY but…
• Far more boilerplate than the original!
• And worse, boilerplate is in calling code rather than
hidden in implementation (anonymous inner
classes)
• Conclusion: not worth it. Stick with adapter coupled
to concrete fragments
Kotlin version
What is this as actual code?
“A list of things that return Fragments”
Kotlin = “A list of functions that return Fragments”
[
]
“A list of functions that return
Fragments”
What does
look like in Kotlin?
Function that takes nothing and returns a
Fragment:
List of functions that takes nothing and
return a Fragment:
Idiomatic Kotlin for Android
Lambda syntax to kill more
boilerplate:
Type inference to kill more
boilerplate:
Idiomatic Kotlin for Android
Idiomatic Kotlin for Android
Refactor constructor to
accept a list of funcs
Idiomatic Kotlin for Android
Replaced .get(n) call with index operator [n]
Replaced explicit .invoke() call with parentheses
Curly braces and return statement replaced with =
Return types removed, they can be inferred!
Let’s see the calling code
(e.g. Activity with a
ViewPager)…
Idiomatic Kotlin for Android
• Pass just the meaningful info to a new adapter
• Reduce noise/kill boilerplate
• Don’t repeat ourselves
Kotlin version
✔
✔
✔
Recap
• Function types - passing functions as parameters
• Lambda (function literal) syntax
{ OnboardingFragment() }
• Type inference
• Indexing operator .get(n) -> []
• Invoke operator .invoke() -> ()
Conclusion
• Reduce your (Java-imposed) tolerance to
boilerplate. It’s even more of a code smell in Kotlin!
• Lots of language tools to kill boilerplate!
• Passing functions around is new territory, be
creative
Matthew Clarke

Oblig twitter handle: @kiwiandroiddev



References:

https://kotlinlang.org/docs/reference/operator-
overloading.html
Thank you
1 of 36

Recommended

Take Flight - Using Fly with the Play Framework by
Take Flight - Using Fly with the Play FrameworkTake Flight - Using Fly with the Play Framework
Take Flight - Using Fly with the Play FrameworkAsher Glynn
846 views39 slides
Wrapping java in awesomeness aka condensator by
Wrapping java in awesomeness aka condensatorWrapping java in awesomeness aka condensator
Wrapping java in awesomeness aka condensatorFlowa Oy
1.2K views20 slides
Runtime Bytecode Transformation for Smalltalk by
Runtime Bytecode Transformation for SmalltalkRuntime Bytecode Transformation for Smalltalk
Runtime Bytecode Transformation for SmalltalkESUG
390 views14 slides
Kotlin workshop by
Kotlin workshopKotlin workshop
Kotlin workshopJedsada Tiwongvokul
522 views15 slides
React meets o OCalm by
React meets o OCalmReact meets o OCalm
React meets o OCalmMichał Załęcki
125 views20 slides
Variables in Pharo5 by
Variables in Pharo5Variables in Pharo5
Variables in Pharo5Marcus Denker
2.4K views31 slides

More Related Content

What's hot

Advanced Reflection in Pharo by
Advanced Reflection in PharoAdvanced Reflection in Pharo
Advanced Reflection in PharoMarcus Denker
4.6K views46 slides
[Kotlin Serverless 工作坊] 單元 3 - 實作 JSON API by
[Kotlin Serverless 工作坊] 單元 3 - 實作 JSON API[Kotlin Serverless 工作坊] 單元 3 - 實作 JSON API
[Kotlin Serverless 工作坊] 單元 3 - 實作 JSON APIShengyou Fan
241 views15 slides
TDC2017 | Florianopolis - Trilha DevOps How we figured out we had a SRE team ... by
TDC2017 | Florianopolis - Trilha DevOps How we figured out we had a SRE team ...TDC2017 | Florianopolis - Trilha DevOps How we figured out we had a SRE team ...
TDC2017 | Florianopolis - Trilha DevOps How we figured out we had a SRE team ...tdc-globalcode
47 views38 slides
Python to scala by
Python to scalaPython to scala
Python to scalakao kuo-tung
1.4K views76 slides
Our challenge for Bulkload reliability improvement by
Our challenge for Bulkload reliability  improvementOur challenge for Bulkload reliability  improvement
Our challenge for Bulkload reliability improvementSatoshi Akama
611 views23 slides
Conejo Architecture by
Conejo ArchitectureConejo Architecture
Conejo Architecturepaulosuzart
549 views12 slides

What's hot(19)

Advanced Reflection in Pharo by Marcus Denker
Advanced Reflection in PharoAdvanced Reflection in Pharo
Advanced Reflection in Pharo
Marcus Denker4.6K views
[Kotlin Serverless 工作坊] 單元 3 - 實作 JSON API by Shengyou Fan
[Kotlin Serverless 工作坊] 單元 3 - 實作 JSON API[Kotlin Serverless 工作坊] 單元 3 - 實作 JSON API
[Kotlin Serverless 工作坊] 單元 3 - 實作 JSON API
Shengyou Fan241 views
TDC2017 | Florianopolis - Trilha DevOps How we figured out we had a SRE team ... by tdc-globalcode
TDC2017 | Florianopolis - Trilha DevOps How we figured out we had a SRE team ...TDC2017 | Florianopolis - Trilha DevOps How we figured out we had a SRE team ...
TDC2017 | Florianopolis - Trilha DevOps How we figured out we had a SRE team ...
tdc-globalcode47 views
Our challenge for Bulkload reliability improvement by Satoshi Akama
Our challenge for Bulkload reliability  improvementOur challenge for Bulkload reliability  improvement
Our challenge for Bulkload reliability improvement
Satoshi Akama611 views
Conejo Architecture by paulosuzart
Conejo ArchitectureConejo Architecture
Conejo Architecture
paulosuzart549 views
Embulk at Treasure Data by Satoshi Akama
Embulk at Treasure DataEmbulk at Treasure Data
Embulk at Treasure Data
Satoshi Akama2.8K views
Optimize perl5 code for perfomance freaks by karupanerura
Optimize perl5 code for perfomance freaksOptimize perl5 code for perfomance freaks
Optimize perl5 code for perfomance freaks
karupanerura 5K views
Reflection in Pharo: Beyond Smalltak by Marcus Denker
Reflection in Pharo: Beyond SmalltakReflection in Pharo: Beyond Smalltak
Reflection in Pharo: Beyond Smalltak
Marcus Denker746 views
ElasticSearch Introduction by Minh Hoang
ElasticSearch IntroductionElasticSearch Introduction
ElasticSearch Introduction
Minh Hoang663 views
Patterns and practices for real-world event-driven microservices by Rachel Reese
Patterns and practices for real-world event-driven microservicesPatterns and practices for real-world event-driven microservices
Patterns and practices for real-world event-driven microservices
Rachel Reese3K views
Stuff we noticed while building "Asterisk in the cloud" by troyd
Stuff we noticed while building "Asterisk in the cloud"Stuff we noticed while building "Asterisk in the cloud"
Stuff we noticed while building "Asterisk in the cloud"
troyd670 views
iOS App Development with F# and Xamarin by Rachel Reese
iOS App Development with F# and XamariniOS App Development with F# and Xamarin
iOS App Development with F# and Xamarin
Rachel Reese2K views
java8-patterns by Justin Lin
java8-patternsjava8-patterns
java8-patterns
Justin Lin15.3K views
Reactive Programming in the Browser feat. Scala.js and PureScript by Luka Jacobowitz
Reactive Programming in the Browser feat. Scala.js and PureScriptReactive Programming in the Browser feat. Scala.js and PureScript
Reactive Programming in the Browser feat. Scala.js and PureScript
Luka Jacobowitz846 views
CocoaConf DC - Automate with Swift - Tony Ingraldi by Tony Ingraldi
CocoaConf DC -  Automate with Swift - Tony IngraldiCocoaConf DC -  Automate with Swift - Tony Ingraldi
CocoaConf DC - Automate with Swift - Tony Ingraldi
Tony Ingraldi1.4K views

Similar to Idiomatic Kotlin for Android

Kotlin Coroutines and Android sitting in a tree by
Kotlin Coroutines and Android sitting in a treeKotlin Coroutines and Android sitting in a tree
Kotlin Coroutines and Android sitting in a treeKai Koenig
1.1K views49 slides
Introduction to Koltin for Android Part I by
Introduction to Koltin for Android Part I Introduction to Koltin for Android Part I
Introduction to Koltin for Android Part I Atif AbbAsi
1.2K views38 slides
Introduction to Kotlin - Android KTX by
Introduction to Kotlin - Android KTXIntroduction to Kotlin - Android KTX
Introduction to Kotlin - Android KTXSyed Awais Mazhar Bukhari
677 views42 slides
What's new with Kotlin - Google IO18' extended Covenant University. by
What's new with Kotlin - Google IO18' extended Covenant University.What's new with Kotlin - Google IO18' extended Covenant University.
What's new with Kotlin - Google IO18' extended Covenant University.SimileoluwaAluko
37 views24 slides
Agile JavaScript Testing by
Agile JavaScript TestingAgile JavaScript Testing
Agile JavaScript TestingScott Becker
6.2K views48 slides

Similar to Idiomatic Kotlin for Android(20)

Kotlin Coroutines and Android sitting in a tree by Kai Koenig
Kotlin Coroutines and Android sitting in a treeKotlin Coroutines and Android sitting in a tree
Kotlin Coroutines and Android sitting in a tree
Kai Koenig1.1K views
Introduction to Koltin for Android Part I by Atif AbbAsi
Introduction to Koltin for Android Part I Introduction to Koltin for Android Part I
Introduction to Koltin for Android Part I
Atif AbbAsi1.2K views
What's new with Kotlin - Google IO18' extended Covenant University. by SimileoluwaAluko
What's new with Kotlin - Google IO18' extended Covenant University.What's new with Kotlin - Google IO18' extended Covenant University.
What's new with Kotlin - Google IO18' extended Covenant University.
SimileoluwaAluko37 views
Agile JavaScript Testing by Scott Becker
Agile JavaScript TestingAgile JavaScript Testing
Agile JavaScript Testing
Scott Becker6.2K views
Say Goodbye To Java: Getting Started With Kotlin For Android Development by Adam Magaña
Say Goodbye To Java: Getting Started With Kotlin For Android DevelopmentSay Goodbye To Java: Getting Started With Kotlin For Android Development
Say Goodbye To Java: Getting Started With Kotlin For Android Development
Adam Magaña153 views
Introduction of Java 8 with emphasis on Lambda Expressions and Streams by Emiel Paasschens
Introduction of Java 8 with emphasis on Lambda Expressions and StreamsIntroduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Emiel Paasschens3K views
Kotlin- Basic to Advance by Coder Tech
Kotlin- Basic to Advance Kotlin- Basic to Advance
Kotlin- Basic to Advance
Coder Tech427 views
Rapid Web API development with Kotlin and Ktor by Trayan Iliev
Rapid Web API development with Kotlin and KtorRapid Web API development with Kotlin and Ktor
Rapid Web API development with Kotlin and Ktor
Trayan Iliev499 views
A quick and fast intro to Kotlin by XPeppers
A quick and fast intro to Kotlin A quick and fast intro to Kotlin
A quick and fast intro to Kotlin
XPeppers1.7K views
10 Sets of Best Practices for Java 8 by Garth Gilmour
10 Sets of Best Practices for Java 810 Sets of Best Practices for Java 8
10 Sets of Best Practices for Java 8
Garth Gilmour4.4K views
Writing Rust Command Line Applications by All Things Open
Writing Rust Command Line ApplicationsWriting Rust Command Line Applications
Writing Rust Command Line Applications
All Things Open743 views
Getting Started With Kotlin by Gaurav sharma
Getting Started With KotlinGetting Started With Kotlin
Getting Started With Kotlin
Gaurav sharma270 views
Power Up Your Build - Omer van Kloeten @ Wix 2018-04 by Omer van Kloeten
Power Up Your Build - Omer van Kloeten @ Wix 2018-04Power Up Your Build - Omer van Kloeten @ Wix 2018-04
Power Up Your Build - Omer van Kloeten @ Wix 2018-04
Omer van Kloeten114 views

Recently uploaded

Instrumentation & Control Lab Manual.pdf by
Instrumentation & Control Lab Manual.pdfInstrumentation & Control Lab Manual.pdf
Instrumentation & Control Lab Manual.pdfNTU Faisalabad
5 views63 slides
Generative AI Models & Their Applications by
Generative AI Models & Their ApplicationsGenerative AI Models & Their Applications
Generative AI Models & Their ApplicationsSN
6 views1 slide
DevOps to DevSecOps: Enhancing Software Security Throughout The Development L... by
DevOps to DevSecOps: Enhancing Software Security Throughout The Development L...DevOps to DevSecOps: Enhancing Software Security Throughout The Development L...
DevOps to DevSecOps: Enhancing Software Security Throughout The Development L...Anowar Hossain
12 views34 slides
What is Unit Testing by
What is Unit TestingWhat is Unit Testing
What is Unit TestingSadaaki Emura
23 views25 slides
Saikat Chakraborty Java Oracle Certificate.pdf by
Saikat Chakraborty Java Oracle Certificate.pdfSaikat Chakraborty Java Oracle Certificate.pdf
Saikat Chakraborty Java Oracle Certificate.pdfSaikatChakraborty787148
15 views1 slide
Update 42 models(Diode/General ) in SPICE PARK(DEC2023) by
Update 42 models(Diode/General ) in SPICE PARK(DEC2023)Update 42 models(Diode/General ) in SPICE PARK(DEC2023)
Update 42 models(Diode/General ) in SPICE PARK(DEC2023)Tsuyoshi Horigome
23 views16 slides

Recently uploaded(20)

Instrumentation & Control Lab Manual.pdf by NTU Faisalabad
Instrumentation & Control Lab Manual.pdfInstrumentation & Control Lab Manual.pdf
Instrumentation & Control Lab Manual.pdf
NTU Faisalabad 5 views
Generative AI Models & Their Applications by SN
Generative AI Models & Their ApplicationsGenerative AI Models & Their Applications
Generative AI Models & Their Applications
SN6 views
DevOps to DevSecOps: Enhancing Software Security Throughout The Development L... by Anowar Hossain
DevOps to DevSecOps: Enhancing Software Security Throughout The Development L...DevOps to DevSecOps: Enhancing Software Security Throughout The Development L...
DevOps to DevSecOps: Enhancing Software Security Throughout The Development L...
Anowar Hossain12 views
Update 42 models(Diode/General ) in SPICE PARK(DEC2023) by Tsuyoshi Horigome
Update 42 models(Diode/General ) in SPICE PARK(DEC2023)Update 42 models(Diode/General ) in SPICE PARK(DEC2023)
Update 42 models(Diode/General ) in SPICE PARK(DEC2023)
Thermal aware task assignment for multicore processors using genetic algorithm by IJECEIAES
Thermal aware task assignment for multicore processors using genetic algorithm Thermal aware task assignment for multicore processors using genetic algorithm
Thermal aware task assignment for multicore processors using genetic algorithm
IJECEIAES31 views
A multi-microcontroller-based hardware for deploying Tiny machine learning mo... by IJECEIAES
A multi-microcontroller-based hardware for deploying Tiny machine learning mo...A multi-microcontroller-based hardware for deploying Tiny machine learning mo...
A multi-microcontroller-based hardware for deploying Tiny machine learning mo...
IJECEIAES13 views
NEW SUPPLIERS SUPPLIES (copie).pdf by georgesradjou
NEW SUPPLIERS SUPPLIES (copie).pdfNEW SUPPLIERS SUPPLIES (copie).pdf
NEW SUPPLIERS SUPPLIES (copie).pdf
georgesradjou15 views
Advances in micro milling: From tool fabrication to process outcomes by Shivendra Nandan
Advances in micro milling: From tool fabrication to process outcomesAdvances in micro milling: From tool fabrication to process outcomes
Advances in micro milling: From tool fabrication to process outcomes
Design of machine elements-UNIT 3.pptx by gopinathcreddy
Design of machine elements-UNIT 3.pptxDesign of machine elements-UNIT 3.pptx
Design of machine elements-UNIT 3.pptx
gopinathcreddy32 views
SUMIT SQL PROJECT SUPERSTORE 1.pptx by Sumit Jadhav
SUMIT SQL PROJECT SUPERSTORE 1.pptxSUMIT SQL PROJECT SUPERSTORE 1.pptx
SUMIT SQL PROJECT SUPERSTORE 1.pptx
Sumit Jadhav 12 views
Machine learning in drug supply chain management during disease outbreaks: a ... by IJECEIAES
Machine learning in drug supply chain management during disease outbreaks: a ...Machine learning in drug supply chain management during disease outbreaks: a ...
Machine learning in drug supply chain management during disease outbreaks: a ...
IJECEIAES12 views

Idiomatic Kotlin for Android