Use Kotlin-specific constructs and syntax patterns instead of Java-style code to improve readability and maintainability. This includes leveraging Kotlin's expressive syntax features and standard library functions.
Use Kotlin-specific constructs and syntax patterns instead of Java-style code to improve readability and maintainability. This includes leveraging Kotlin’s expressive syntax features and standard library functions.
Key practices to follow:
Expression body syntax for simple functions:
// Instead of
override fun contentType(): MediaType? {
return responseBody.contentType()
}
// Use
override fun contentType(): MediaType? = responseBody.contentType()
Kotlin collection builders:
// Instead of
ArrayList<JavaModuleWrapper>().apply { ... }
// Use
buildList { ... } or mutableListOf()
When expressions for complex conditionals:
// Instead of multiple if-else chains
return when (value) {
null -> ReadableType.Null
is Boolean -> ReadableType.Boolean
is Number -> ReadableType.Number
else -> ReadableType.Null
}
Properties over getter methods:
// Instead of
fun getLifecycleState(): LifecycleState = state
// Use
val lifecycleState: LifecycleState
get() = state
Functional interfaces:
// Use fun interface to enable lambda syntax
public fun interface BatchEventDispatchedListener {
fun onBatchEventDispatched()
}
Kotlin string builders:
// Instead of StringBuilder
// Use
buildString { ... }
These patterns make code more concise, readable, and leverage Kotlin’s strengths while reducing boilerplate compared to Java-style implementations.
Enter the URL of a public GitHub repository