platform/build-scripts/kotlin-style-recommendations.md
@JvmField for internal data class fields - improves Java interop performance by avoiding getter/setter generation// Preferred
internal data class CachedModuleInfo(
@JvmField val name: String,
@JvmField val loading: ModuleLoadingRuleValue?,
@JvmField val sourceModuleSet: String,
)
// Avoid
internal data class CachedModuleInfo(
val name: String,
val loading: ModuleLoadingRuleValue?,
val sourceModuleSet: String,
)
.put()/.get() instead of [] operator - explicit method calls are preferred// Preferred
result.put(key, value)
result.get(key)
// Avoid
result[key] = value
result[key]
LinkedHashMap/LinkedHashSet or HashMap/HashSet instead of mutableMapOf()/mutableSetOf()// Preferred
val result = LinkedHashMap<String, CachedModuleInfo>()
val seen = HashSet<String>()
// Avoid
val result = mutableMapOf<String, CachedModuleInfo>()
val seen = mutableSetOf<String>()
return instead.
Exception: an expression body that is a single runBlocking(Dispatchers.Default) { ... } wrapper is allowed.// Preferred
fun process(items: List<Item>): Result {
return items
.filter { it.isValid }
.map { transform(it) }
.toResult()
}
// Avoid
fun process(items: List<Item>): Result =
items
.filter { it.isValid }
.map { transform(it) }
.toResult()
Allowed pattern (intentional):
fun computeInBackground(): Result = runBlocking(Dispatchers.Default) {
val value = loadValue()
transform(value)
}
@JvmField eliminates property accessor overhead when accessed from Java code.put()/.get() makes the intent clearer and avoids ambiguity with nullable returnsreturn is more readable for multiline expressions and makes debugging easier