Kotlin Best Practices
Priority: P1 (HIGH)
Engineering standards for clean, maintainable Kotlin systems.
Implementation Guidelines
- •Scope Functions:
- •
apply: Object configuration (returns object). - •
also: Side effects validation/logging (returns object). - •
let: Null checks (?.let) or mapping (returns result). - •
run: Object configuration and mapping (returns result). - •
with: Grouping multiple method calls on an object (returns result).
- •
- •Backing Properties: Use
_prop(private mutable) andprop(public immutable) pattern for encapsulation. - •Collections: Expose
List/Map(read-only interfaces) publicly, keepMutableListinternal. - •Single Expression: Use
runCatchingfor simple error handling overtry/catchblocks. - •Visibility: Default to
privateorinternal. Minimizepublicsurface area. - •Top-Level: Prefer top-level functions/constants over implementation-less
objectsingletons.
Anti-Patterns
- •Nesting Scope Functions: Avoid nesting
let/applymore than 2 levels deep. It destroys readability. - •Mutable Public Props: Avoid
public var. Useprivate setor backing properties. - •Global Mutable State: Avoid top-level mutable variables.
Code
kotlin
// Backing Property Pattern
class ViewModel {
private val _uiState = MutableStateFlow(UiState.Loading)
val uiState: StateFlow<UiState> = _uiState.asStateFlow() // Read-only
fun load() {
// apply for config
val user = User().apply {
name = "John"
age = 30
}
// runCatching
runCatching { api.fetch() }
.onSuccess { _uiState.value = UiState.Success(it) }
.onFailure { logger.error(it) }
}
}
Related Topics
language | coroutines