SlideShare a Scribd company logo
Getting Started with Kotlin
Development
Rivu Chakraborty (http://www.rivuchk.com)
Sr. Software Engineer – Indus Net Technologies Pvt. Ltd.
Instructor – Caster.io
Author – Reactive Programming in Kotlin
Author – Functional Kotlin (Co-Author – Mario Arias)
Author – Coroutines for Android Developers (Co-Author – Ravindra Kumar)
Co-Founder, Organiser – Kotlin Kolkata User Group
Core Organising Team Member – Google Developers Group (GDG) Kolkata
@rivuchakraborty
Sponsored by AndroidStarters (http://androidstarters.com/)
Getting Started with Kotlin
and Android Development
Java Developers - Bangalore Meetup, BoJUG
Meetup #15 – 24th March, 2018
Rivu Chakraborty (http://www.rivuchk.com)
Sponsored by AndroidStarters (http://androidstarters.com/)
Books by me (more coming up)
https://www.packtpub.com/application-
development/reactive-programming-kotlin
https://www.packtpub.com/application-
development/functional-kotlin
https://leanpub.com/coroutines-for-
android-developers
Introduction to Kotlin
• Kotlin is a Statically Types Programming Language that is inspired by some of
other great languages like Java, Scala, C#, Groovy and more
• Kotlin Targets – JVM, Android, JavaScript, Native (LLVM)
• Officially supported by Google as Android Native App development language
• Supported by Pivotal – Spring
• As per March 2018, Kotlin is ranked at 38 in TIOBE Language Index
Kotlin is
• Started by JetBrains in 2010
• A language that is
• Safe and approachable
• More Practical and Sensible
• Easy to learn for developers with or without any experience, easier to use
• Offers the best combination of OOP and FP
Best Features of Kotlin (in my opinion)
• More Support for Immutability
• Nullability?
• Language level support for Delegation
• Support for Functional Programming, but letting the developer choose between
FP and OOP
• Interoperability with Java
• Multiplatform
Let’s Get Started
Conventions
• Follows Java Programming Language Coding Conventions
• Types in Uppercase
• Methods and Properties in lower camelCase
• Semicolons optional*
• Packages follows reverse notation
• Multiple classes per file allowed
• Package does not have to match folder names
Variables and Data types
● var for variables that may change value (mutable), val for final variables
● Do we really need to declare Datatype explicitly?
● Unit and Any
● Explicit type casting
● Pair
val myInt:Int = 15
val name = “Rivu”
var myLong:Long = myInt.toLong()
var (name,age) = Pair(“Indranil”,24)
val myLazyVal by lazy {
“Lazyly initialized variable”
}
Kotlin Types
Any
String Customer. . .
Nothing
Any?
String? Customer?. . .
Nothing?
Functions are finally fun
● Default value
● Named arguments
● Local functions
● Generic functions
Use `when` as expression body
fun parseNum(number: String): Int? {
when (number) {
"one" -> return 1
"two" -> return 2
else -> return null
}
}
fun parseNum(number: String) =
when (number) {
"one" -> 1
"two" -> 2
else -> null
}
Use `try` as expression body
fun tryParse(number: String): Int? {
try {
return Integer.parseInt(number)
}
catch (e: NumberFormatException) {
return null
}
}
fun tryParse(number: String) =
try {
Integer.parseInt(number)
}
catch (e: NumberFormatException) {
null
}
Use `try` as expression
fun tryParse(number: String): Int? {
try {
return Integer.parseInt(number)
}
catch (e: NumberFormatException) {
return null
}
}
fun tryParse(number: String): Int? {
val n = try {
Integer.parseInt(number)
} catch (e: NumberFormatException) {
null
}
println(n)
return n
}
Use `elvis` with `return` and `throw`
fun processPerson(person: Person) {
val name = person.name
if (name == null)
throw IllegalArgumentException(
"Named required")
val age = person.age
if (age == null) return
println("$name: $age")
}
fun processPerson(person: Person) {
val name = person.name ?:
throw IllegalArgumentException(
"Name required")
val age = person.age ?: return
println("$name: $age")
}
class Person(val name: String?,
val age: Int?)
Use range checks instead of comparison pairs
fun isLatinUppercase(c: Char) =
c >= 'A' && c <= 'Z'
fun isLatinUppercase(c: Char) =
c in 'A'..'Z'
Don’t necessarily create classes just to hold functions
class StringUtils {
companion object {
fun isPhoneNumber(s: String) =
s.length == 7 &&
s.all { it.isDigit() }
}
} fun isPhoneNumber(s: String) =
s.length == 7 &&
s.all { it.isDigit() }
object StringUtils {
fun isPhoneNumber(s: String) =
s.length == 7 &&
s.all { it.isDigit() }
}
Use extension functions copiously
fun isPhoneNumber(s: String) =
s.length == 7 &&
s.all { it.isDigit() }
fun String.isPhoneNumber() =
length == 7 &&
all { it.isDigit() }
Avoid using member extension functions (except for DSLs)
class PhoneBook {
fun String.isPhoneNumber() =
length == 7 &&
all { it.isDigit() }
}
class PhoneBook {
}
private fun String.isPhoneNumber() =
length == 7 &&
all { it.isDigit() }
Takeaway Tips
• Try to use val more than var
• Instead of custom getter and setter, consider Delegates like
Delegates.Observable, Delegates.Vetoble etc.
• Never use !!
• Avoid using companion objects
• Use const val for Constants like BASE_URL etc and wherever
possible.
• Use Mutable Lists, Maps and Arrays where you don’t need to modify
• Use Lambda, High Order Functions cautiously
Thank you Java Dev Meetup
Bangalore & BoJUG for inviting
me to give this talk
Thank you Android Starters
for sponsoring my travel

More Related Content

What's hot (20)

PPTX
Kotlin
Rory Preddy
 
PDF
Swift and Kotlin Presentation
Andrzej Sitek
 
ODP
JavaScript Object Oriented Programming Cheat Sheet
HDR1001
 
PDF
Scala the-good-parts
Fuqiang Wang
 
PDF
Kotlin - Better Java
Dariusz Lorenc
 
PDF
Kotlin for Android Development
Speck&Tech
 
PDF
Ruby and rails - Advanced Training (Cybage)
Gautam Rege
 
KEY
SOLID Ruby, SOLID Rails
Jens-Christian Fischer
 
ODP
From Perl To Elixir
Ruben Amortegui
 
PPTX
Introduction to Kotlin Language and its application to Android platform
EastBanc Tachnologies
 
PDF
Continuations in scala (incomplete version)
Fuqiang Wang
 
PDF
Effective Scala: Programming Patterns
Vasil Remeniuk
 
PDF
Object Oriented Programming in JavaScript
zand3rs
 
PPTX
Building Mobile Apps with Android
Kurt Renzo Acosta
 
PPT
A Deeper look into Javascript Basics
Mindfire Solutions
 
PPTX
Scala - the good, the bad and the very ugly
Bozhidar Bozhanov
 
PPTX
Groovy Programming Language
Aniruddha Chakrabarti
 
PDF
Scala on-android
lifecoder
 
PDF
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...
Tudor Dragan
 
PDF
Kotlin in action
Ciro Rizzo
 
Kotlin
Rory Preddy
 
Swift and Kotlin Presentation
Andrzej Sitek
 
JavaScript Object Oriented Programming Cheat Sheet
HDR1001
 
Scala the-good-parts
Fuqiang Wang
 
Kotlin - Better Java
Dariusz Lorenc
 
Kotlin for Android Development
Speck&Tech
 
Ruby and rails - Advanced Training (Cybage)
Gautam Rege
 
SOLID Ruby, SOLID Rails
Jens-Christian Fischer
 
From Perl To Elixir
Ruben Amortegui
 
Introduction to Kotlin Language and its application to Android platform
EastBanc Tachnologies
 
Continuations in scala (incomplete version)
Fuqiang Wang
 
Effective Scala: Programming Patterns
Vasil Remeniuk
 
Object Oriented Programming in JavaScript
zand3rs
 
Building Mobile Apps with Android
Kurt Renzo Acosta
 
A Deeper look into Javascript Basics
Mindfire Solutions
 
Scala - the good, the bad and the very ugly
Bozhidar Bozhanov
 
Groovy Programming Language
Aniruddha Chakrabarti
 
Scala on-android
lifecoder
 
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...
Tudor Dragan
 
Kotlin in action
Ciro Rizzo
 

Similar to Getting Started With Kotlin Development - Rivu (20)

PDF
Kotlin: A pragmatic language by JetBrains
Jigar Gosar
 
PDF
Introduction to kotlin for android app development gdg ahmedabad dev fest 2017
Hardik Trivedi
 
PDF
Idiomatic Kotlin
intelliyole
 
PPTX
Hello kotlin | An Event by DSC Unideb
Muhammad Raza
 
PPTX
Exploring Kotlin language basics for Android App development
Jayaprakash R
 
PPTX
Android & Kotlin - The code awakens #03
Omar Miatello
 
PDF
Be More Productive with Kotlin
Brandon Wever
 
PDF
Kotlin Introduction with Android applications
Thao Huynh Quang
 
PDF
Kotlin for Android devs
Adit Lal
 
PDF
Privet Kotlin (Windy City DevFest)
Cody Engel
 
PDF
Intro to Kotlin
Magda Miu
 
PPTX
Kotlin Basic & Android Programming
Kongu Engineering College, Perundurai, Erode
 
PPTX
Introduction to kotlin + spring boot demo
Muhammad Abdullah
 
PDF
Let's fly to the Kotlin Island. Just an introduction to Kotlin
Aliaksei Zhynhiarouski
 
PDF
Kotlin what_you_need_to_know-converted event 4 with nigerians
junaidhasan17
 
PPTX
Kotlin : Happy Development
Md Sazzad Islam
 
PDF
Kotlin/Everywhere GDG Bhubaneswar 2019
Sriyank Siddhartha
 
PDF
Exploring Kotlin
Johan Haleby
 
PDF
Lightning talk: Kotlin
Evolve
 
PDF
Having Fun with Kotlin Android - DILo Surabaya
DILo Surabaya
 
Kotlin: A pragmatic language by JetBrains
Jigar Gosar
 
Introduction to kotlin for android app development gdg ahmedabad dev fest 2017
Hardik Trivedi
 
Idiomatic Kotlin
intelliyole
 
Hello kotlin | An Event by DSC Unideb
Muhammad Raza
 
Exploring Kotlin language basics for Android App development
Jayaprakash R
 
Android & Kotlin - The code awakens #03
Omar Miatello
 
Be More Productive with Kotlin
Brandon Wever
 
Kotlin Introduction with Android applications
Thao Huynh Quang
 
Kotlin for Android devs
Adit Lal
 
Privet Kotlin (Windy City DevFest)
Cody Engel
 
Intro to Kotlin
Magda Miu
 
Kotlin Basic & Android Programming
Kongu Engineering College, Perundurai, Erode
 
Introduction to kotlin + spring boot demo
Muhammad Abdullah
 
Let's fly to the Kotlin Island. Just an introduction to Kotlin
Aliaksei Zhynhiarouski
 
Kotlin what_you_need_to_know-converted event 4 with nigerians
junaidhasan17
 
Kotlin : Happy Development
Md Sazzad Islam
 
Kotlin/Everywhere GDG Bhubaneswar 2019
Sriyank Siddhartha
 
Exploring Kotlin
Johan Haleby
 
Lightning talk: Kotlin
Evolve
 
Having Fun with Kotlin Android - DILo Surabaya
DILo Surabaya
 
Ad

More from CodeOps Technologies LLP (20)

PDF
AWS Serverless Event-driven Architecture - in lastminute.com meetup
CodeOps Technologies LLP
 
PPTX
Understanding azure batch service
CodeOps Technologies LLP
 
PDF
DEVOPS AND MACHINE LEARNING
CodeOps Technologies LLP
 
PDF
SERVERLESS MIDDLEWARE IN AZURE FUNCTIONS
CodeOps Technologies LLP
 
PPT
BUILDING SERVERLESS SOLUTIONS WITH AZURE FUNCTIONS
CodeOps Technologies LLP
 
PPTX
APPLYING DEVOPS STRATEGIES ON SCALE USING AZURE DEVOPS SERVICES
CodeOps Technologies LLP
 
PPTX
BUILD, TEST & DEPLOY .NET CORE APPS IN AZURE DEVOPS
CodeOps Technologies LLP
 
PPTX
CREATE RELIABLE AND LOW-CODE APPLICATION IN SERVERLESS MANNER
CodeOps Technologies LLP
 
PPTX
CREATING REAL TIME DASHBOARD WITH BLAZOR, AZURE FUNCTION COSMOS DB AN AZURE S...
CodeOps Technologies LLP
 
PPTX
WRITE SCALABLE COMMUNICATION APPLICATION WITH POWER OF SERVERLESS
CodeOps Technologies LLP
 
PPTX
Training And Serving ML Model Using Kubeflow by Jayesh Sharma
CodeOps Technologies LLP
 
PPTX
Deploy Microservices To Kubernetes Without Secrets by Reenu Saluja
CodeOps Technologies LLP
 
PDF
Leverage Azure Tech stack for any Kubernetes cluster via Azure Arc by Saiyam ...
CodeOps Technologies LLP
 
PDF
YAML Tips For Kubernetes by Neependra Khare
CodeOps Technologies LLP
 
PDF
Must Know Azure Kubernetes Best Practices And Features For Better Resiliency ...
CodeOps Technologies LLP
 
PPTX
Monitor Azure Kubernetes Cluster With Prometheus by Mamta Jha
CodeOps Technologies LLP
 
PDF
Jet brains space intro presentation
CodeOps Technologies LLP
 
PDF
Functional Programming in Java 8 - Lambdas and Streams
CodeOps Technologies LLP
 
PPTX
Distributed Tracing: New DevOps Foundation
CodeOps Technologies LLP
 
PDF
"Distributed Tracing: New DevOps Foundation" by Jayesh Ahire
CodeOps Technologies LLP
 
AWS Serverless Event-driven Architecture - in lastminute.com meetup
CodeOps Technologies LLP
 
Understanding azure batch service
CodeOps Technologies LLP
 
DEVOPS AND MACHINE LEARNING
CodeOps Technologies LLP
 
SERVERLESS MIDDLEWARE IN AZURE FUNCTIONS
CodeOps Technologies LLP
 
BUILDING SERVERLESS SOLUTIONS WITH AZURE FUNCTIONS
CodeOps Technologies LLP
 
APPLYING DEVOPS STRATEGIES ON SCALE USING AZURE DEVOPS SERVICES
CodeOps Technologies LLP
 
BUILD, TEST & DEPLOY .NET CORE APPS IN AZURE DEVOPS
CodeOps Technologies LLP
 
CREATE RELIABLE AND LOW-CODE APPLICATION IN SERVERLESS MANNER
CodeOps Technologies LLP
 
CREATING REAL TIME DASHBOARD WITH BLAZOR, AZURE FUNCTION COSMOS DB AN AZURE S...
CodeOps Technologies LLP
 
WRITE SCALABLE COMMUNICATION APPLICATION WITH POWER OF SERVERLESS
CodeOps Technologies LLP
 
Training And Serving ML Model Using Kubeflow by Jayesh Sharma
CodeOps Technologies LLP
 
Deploy Microservices To Kubernetes Without Secrets by Reenu Saluja
CodeOps Technologies LLP
 
Leverage Azure Tech stack for any Kubernetes cluster via Azure Arc by Saiyam ...
CodeOps Technologies LLP
 
YAML Tips For Kubernetes by Neependra Khare
CodeOps Technologies LLP
 
Must Know Azure Kubernetes Best Practices And Features For Better Resiliency ...
CodeOps Technologies LLP
 
Monitor Azure Kubernetes Cluster With Prometheus by Mamta Jha
CodeOps Technologies LLP
 
Jet brains space intro presentation
CodeOps Technologies LLP
 
Functional Programming in Java 8 - Lambdas and Streams
CodeOps Technologies LLP
 
Distributed Tracing: New DevOps Foundation
CodeOps Technologies LLP
 
"Distributed Tracing: New DevOps Foundation" by Jayesh Ahire
CodeOps Technologies LLP
 
Ad

Recently uploaded (20)

PDF
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
PDF
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PPTX
Tally software_Introduction_Presentation
AditiBansal54083
 
PDF
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
PPTX
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
PPTX
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
PPTX
Revolutionizing Code Modernization with AI
KrzysztofKkol1
 
PDF
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
PPTX
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
 
PPTX
Engineering the Java Web Application (MVC)
abhishekoza1981
 
PDF
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
PPTX
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
 
PPTX
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
PPTX
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
PPTX
The Role of a PHP Development Company in Modern Web Development
SEO Company for School in Delhi NCR
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PDF
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
Tally software_Introduction_Presentation
AditiBansal54083
 
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
Revolutionizing Code Modernization with AI
KrzysztofKkol1
 
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
 
Engineering the Java Web Application (MVC)
abhishekoza1981
 
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
The Role of a PHP Development Company in Modern Web Development
SEO Company for School in Delhi NCR
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 

Getting Started With Kotlin Development - Rivu

  • 1. Getting Started with Kotlin Development Rivu Chakraborty (http://www.rivuchk.com) Sr. Software Engineer – Indus Net Technologies Pvt. Ltd. Instructor – Caster.io Author – Reactive Programming in Kotlin Author – Functional Kotlin (Co-Author – Mario Arias) Author – Coroutines for Android Developers (Co-Author – Ravindra Kumar) Co-Founder, Organiser – Kotlin Kolkata User Group Core Organising Team Member – Google Developers Group (GDG) Kolkata @rivuchakraborty Sponsored by AndroidStarters (http://androidstarters.com/)
  • 2. Getting Started with Kotlin and Android Development Java Developers - Bangalore Meetup, BoJUG Meetup #15 – 24th March, 2018 Rivu Chakraborty (http://www.rivuchk.com) Sponsored by AndroidStarters (http://androidstarters.com/)
  • 3. Books by me (more coming up) https://www.packtpub.com/application- development/reactive-programming-kotlin https://www.packtpub.com/application- development/functional-kotlin https://leanpub.com/coroutines-for- android-developers
  • 4. Introduction to Kotlin • Kotlin is a Statically Types Programming Language that is inspired by some of other great languages like Java, Scala, C#, Groovy and more • Kotlin Targets – JVM, Android, JavaScript, Native (LLVM) • Officially supported by Google as Android Native App development language • Supported by Pivotal – Spring • As per March 2018, Kotlin is ranked at 38 in TIOBE Language Index
  • 5. Kotlin is • Started by JetBrains in 2010 • A language that is • Safe and approachable • More Practical and Sensible • Easy to learn for developers with or without any experience, easier to use • Offers the best combination of OOP and FP
  • 6. Best Features of Kotlin (in my opinion) • More Support for Immutability • Nullability? • Language level support for Delegation • Support for Functional Programming, but letting the developer choose between FP and OOP • Interoperability with Java • Multiplatform
  • 8. Conventions • Follows Java Programming Language Coding Conventions • Types in Uppercase • Methods and Properties in lower camelCase • Semicolons optional* • Packages follows reverse notation • Multiple classes per file allowed • Package does not have to match folder names
  • 9. Variables and Data types ● var for variables that may change value (mutable), val for final variables ● Do we really need to declare Datatype explicitly? ● Unit and Any ● Explicit type casting ● Pair val myInt:Int = 15 val name = “Rivu” var myLong:Long = myInt.toLong() var (name,age) = Pair(“Indranil”,24) val myLazyVal by lazy { “Lazyly initialized variable” }
  • 10. Kotlin Types Any String Customer. . . Nothing Any? String? Customer?. . . Nothing?
  • 11. Functions are finally fun ● Default value ● Named arguments ● Local functions ● Generic functions
  • 12. Use `when` as expression body fun parseNum(number: String): Int? { when (number) { "one" -> return 1 "two" -> return 2 else -> return null } } fun parseNum(number: String) = when (number) { "one" -> 1 "two" -> 2 else -> null }
  • 13. Use `try` as expression body fun tryParse(number: String): Int? { try { return Integer.parseInt(number) } catch (e: NumberFormatException) { return null } } fun tryParse(number: String) = try { Integer.parseInt(number) } catch (e: NumberFormatException) { null }
  • 14. Use `try` as expression fun tryParse(number: String): Int? { try { return Integer.parseInt(number) } catch (e: NumberFormatException) { return null } } fun tryParse(number: String): Int? { val n = try { Integer.parseInt(number) } catch (e: NumberFormatException) { null } println(n) return n }
  • 15. Use `elvis` with `return` and `throw` fun processPerson(person: Person) { val name = person.name if (name == null) throw IllegalArgumentException( "Named required") val age = person.age if (age == null) return println("$name: $age") } fun processPerson(person: Person) { val name = person.name ?: throw IllegalArgumentException( "Name required") val age = person.age ?: return println("$name: $age") } class Person(val name: String?, val age: Int?)
  • 16. Use range checks instead of comparison pairs fun isLatinUppercase(c: Char) = c >= 'A' && c <= 'Z' fun isLatinUppercase(c: Char) = c in 'A'..'Z'
  • 17. Don’t necessarily create classes just to hold functions class StringUtils { companion object { fun isPhoneNumber(s: String) = s.length == 7 && s.all { it.isDigit() } } } fun isPhoneNumber(s: String) = s.length == 7 && s.all { it.isDigit() } object StringUtils { fun isPhoneNumber(s: String) = s.length == 7 && s.all { it.isDigit() } }
  • 18. Use extension functions copiously fun isPhoneNumber(s: String) = s.length == 7 && s.all { it.isDigit() } fun String.isPhoneNumber() = length == 7 && all { it.isDigit() }
  • 19. Avoid using member extension functions (except for DSLs) class PhoneBook { fun String.isPhoneNumber() = length == 7 && all { it.isDigit() } } class PhoneBook { } private fun String.isPhoneNumber() = length == 7 && all { it.isDigit() }
  • 20. Takeaway Tips • Try to use val more than var • Instead of custom getter and setter, consider Delegates like Delegates.Observable, Delegates.Vetoble etc. • Never use !! • Avoid using companion objects • Use const val for Constants like BASE_URL etc and wherever possible. • Use Mutable Lists, Maps and Arrays where you don’t need to modify • Use Lambda, High Order Functions cautiously
  • 21. Thank you Java Dev Meetup Bangalore & BoJUG for inviting me to give this talk Thank you Android Starters for sponsoring my travel