/no-exclamations

Android Lint rule that forbids kotlin !! operator

Primary LanguageKotlinApache License 2.0Apache-2.0

No exclamations!!

This is an Android lint rule that forbids the use of the !! operator in Kotlin.

Download

How to use it

Import with gradle:

repositories {
    maven {
        url  "https://dl.bintray.com/dananas/android" 
    }
}

dependencies {
    lintChecks "com.github.dananas:no-exclamations:0.1.0"
}

The code won't compile if abortOnError is set to true.

android {
    lintOptions {
        abortOnError true
    }
}

Why !! is bad

Unlike Java, Kotlin natively supports nullable types. By using !! not only you throw away this functionality, your code becomes error prone and sometimes non-obvious, because one needs to know the whole lifecycle of the instances !! was used for.

Consider using TheGood way:

class TheGood {
    private var name: String? = null
    fun setup() {
        name = "Clint Eastwood"
    }
    override fun toString(): String {
        return requireNotNull(name) { "Name is not set!" }
    }
}
class TheBad {
    private var name: String? = null
    fun setup() {
        name = "Lee Van Cleef"
    }
    override fun toString(): String {
        return name!!
    }
}
class TheUgly {
    private lateinit var name: String
    fun setup() {
        name = "Eli Wallach"
    }
    override fun toString(): String {
        return name
    }
}

License

Apache-2.0