Sawan Kumar
Published © Apache-2.0

Top 20 Daily Used Kotlin Code Snippets

List of daily used Kotlin code snippets which every developer needs to know. It is a collection of random but frequently used idioms.

BeginnerProtip15 minutes3,813
Top 20 Daily Used Kotlin Code Snippets

Things used in this project

Hardware components

Android device
Android device
×1

Software apps and online services

Android Studio
Android Studio

Story

Read more

Custom parts and enclosures

Kotlin Idioms

Code

Code snippet #1

Plain text
Array
(
    [0] => WP_Term Object
        (
            [term_id] => 21
            [name] => Android
            [slug] => android
            [term_group] => 0
            [term_taxonomy_id] => 21
            [taxonomy] => category
            [description] => 
            [parent] => 0
            [count] => 114
            [filter] => raw
            [cat_ID] => 21
            [category_count] => 114
            [category_description] => 
            [cat_name] => Android
            [category_nicename] => android
            [category_parent] => 0
        )

    [1] => WP_Term Object
        (
            [term_id] => 292
            [name] => Android News
            [slug] => android-news
            [term_group] => 0
            [term_taxonomy_id] => 292
            [taxonomy] => category
            [description] => 
            [parent] => 0
            [count] => 26
            [filter] => raw
            [cat_ID] => 292
            [category_count] => 26
            [category_description] => 
            [cat_name] => Android News
            [category_nicename] => android-news
            [category_parent] => 0
        )

    [2] => WP_Term Object
        (
            [term_id] => 2
            [name] => Featured
            [slug] => featured
            [term_group] => 0
            [term_taxonomy_id] => 2
            [taxonomy] => category
            [description] => It is list of posts given by Editorial hand-picked. May be you enjoy all of this articles.
            [parent] => 0
            [count] => 88
            [filter] => raw
            [cat_ID] => 2
            [category_count] => 88
            [category_description] => It is list of posts given by Editorial hand-picked. May be you enjoy all of this articles.
            [cat_name] => Featured
            [category_nicename] => featured
            [category_parent] => 0
        )

    [3] => WP_Term Object
        (
            [term_id] => 20
            [name] => Fun Stuff
            [slug] => fun-stuff
            [term_group] => 0
            [term_taxonomy_id] => 20
            [taxonomy] => category
            [description] => All of this articles are totally enjoyable and you can take a rest from your daily work to read some funny stuffs.
            [parent] => 0
            [count] => 34
            [filter] => raw
            [cat_ID] => 20
            [category_count] => 34
            [category_description] => All of this articles are totally enjoyable and you can take a rest from your daily work to read some funny stuffs.
            [cat_name] => Fun Stuff
            [category_nicename] => fun-stuff
            [category_parent] => 0
        )

    [4] => WP_Term Object
        (
            [term_id] => 348
            [name] => Kotlin
            [slug] => kotlin
            [term_group] => 0
            [term_taxonomy_id] => 348
            [taxonomy] => category
            [description] => 
            [parent] => 0
            [count] => 31
            [filter] => raw
            [cat_ID] => 348
            [category_count] => 31
            [category_description] => 
            [cat_name] => Kotlin
            [category_nicename] => kotlin
            [category_parent] => 0
        )

)

Code snippet #7

Plain text
when (x) {
    is Foo -> ...
    is Bar -> ...
    else   -> ...
}

Code snippet #9

Plain text
for (i in 1..100) { ... }  // closed range: includes 100
for (i in 1 until 100) { ... } // half-open range: does not include 100
for (x in 2..10 step 2) { ... }
for (x in 10 downTo 1) { ... }
if (x in 1..10) { ... }

Code snippet #19

Plain text
val data = ...

data?.let {
    ... // execute this block if not null
}

Code snippet #21

Plain text
fun transform(color: String): Int {
    return when (color) {
        "Red" -> 0
        "Green" -> 1
        "Blue" -> 2
        else -> throw IllegalArgumentException("Invalid color param value")
    }
}

Code snippet #22

Plain text
fun test() {
    val result = try {
        count()
    } catch (e: ArithmeticException) {
        throw IllegalStateException(e)
    }

    // Working with result
}

Code snippet #23

Plain text
fun foo(param: Int) {
    val result = if (param == 1) {
        "one"
    } else if (param == 2) {
        "two"
    } else {
        "three"
    }
}

Code snippet #27

Plain text
fun transform(color: String): Int = when (color) {
    "Red" -> 0
    "Green" -> 1
    "Blue" -> 2
    else -> throw IllegalArgumentException("Invalid color param value")
}

Code snippet #28

Plain text
class Turtle {
    fun penDown()
    fun penUp()
    fun turn(degrees: Double)
    fun forward(pixels: Double)
}

val myTurtle = Turtle()
with(myTurtle) { //draw a 100 pix square
    penDown()
    for(i in 1..4) {
        forward(100.0)
        turn(90.0)
    }
    penUp()
}

Code snippet #30

Plain text
/  public final class Gson {
//     ...
//     public <T> T fromJson(JsonElement json, Class<T> classOfT) throws JsonSyntaxException {
//     ...

inline fun <reified T: Any> Gson.fromJson(json: JsonElement): T = this.fromJson(json, T::class.java)

Code snippet #31

Plain text
val b: Boolean? = ...
if (b == true) {
    ...
} else {
    // `b` is false or null
}

Kotlin Idioms

Credits

Sawan Kumar

Sawan Kumar

1 project • 3 followers
TellMeHow is a technology blog with Android and Java tutorial. Daily update with latest tutorials, tips and tricks

Comments