Bestpractices
Composite Recipes
Recipes that include further recipes, often including the individual recipes below.
- Apply Kotlin best-practice idioms
- Apply Kotlin best-practice rewrites
- Collapse redundant collection / string conversions
- Find Boolean-conditional smells
- Find best-practice candidates
- Find class-declaration smells
- Find equality / comparison smells
- Find exception-handling smells
- Find function-declaration smells
- Find lambda / functional smells
- Find scope-function smells
- Find string-construction smells
- Find
when-statement smells - Use stdlib accessors for size / first
Recipes
- Drop redundant
String.toString() - Find
!!inside a?.let \{ \}body - Find
!x.isBlank()calls - Find
!x.isEmpty()calls - Find
!x.isNotBlank()calls - Find
!x.isNotEmpty()calls - Find
"" + xpatterns - Find
.also \{ println(it) \}debug patterns - Find
===/!==referential-equality comparisons - Find
Throwable.printStackTrace()calls - Find
abstract classdeclarations without abstract members - Find anonymous
object : Interface \{ override fun … \}with a single override - Find
b == true/b == falsecomparisons - Find chained
!!assertions in a single expression - Find
class Foo : Any()declarations - Find
class Foo \{\}declarations with an empty body - Find classes that could be
data class - Find classes with manual
toString/equals/hashCodeoverrides - Find classes with multiple overloaded secondary constructors
- Find
data classdeclarations withvarproperties - Find empty
companion objectdeclarations - Find empty
init \{ \}blocks - Find
forEach \{ it.toString() \}patterns - Find
forEach \{ println(it) \}patterns - Find
fun f(): Boolean = true|falseliteral-returning functions - Find
fun foo(): T \{ return x \}block bodies - Find functions declared with
: Nothingreturn type - Find functions returning
Pair<A, B> - Find functions returning
Triple<A, B, C> - Find functions with explicit
: Unitreturn type - Find
if (x != null) x.foo()patterns - Find
if (x == null) null else x.foo()patterns - Find
if (x) false else truepatterns - Find
if (x) true else falsepatterns - Find
if (x) \{ return … \} else \{ … \}patterns - Find
lazy \{ \}calls without an explicitLazyThreadSafetyMode - Find
map \{ it.toString() \}/map \{ x -> x.toString() \}patterns - Find marker
objectdeclarations that could bedata object - Find
mutableListOf<T>().also \{ it.add(x) \}patterns - Find
mutableMapOf<K,V>().also \{ it.put(...) \}patterns - Find
mutableSetOf<T>().also \{ it.add(x) \}patterns - Find named
companion object Constantspatterns - Find
objectdeclarations withvarproperties - Find
open classdeclarations without overridable members - Find
return Unit/return kotlin.Unitstatements - Find
run \{ \}calls with an empty body - Find
sealed classdeclarations that could besealed interface - Find
suspend fundeclarations returningJob/Deferred - Find
throw Exception("…")calls - Find
throw RuntimeException("…")calls - Find top-level
run \{ … \}whose body never usesthis - Find trivial
String.format("%s", x)calls - Find
try \{ … \} catch (e: Exception) \{ null \}patterns - Find
when (b: Boolean)selectors - Find
when (x)expressions without anelsebranch - Find
when (x) \{ A -> … \}with a single branch - Find
when (x) \{ … \}used as a statement - Find
whenwith two or more branches having identical bodies - Find wildcard
import a.b.*statements - Find
x + ""patterns - Find
x.also \{ \}calls with an empty body - Find
x.compareTo(y) <op> 0patterns - Find
x.isNotEmpty()onStringwhereisNotBlank()might be wanted - Find
xs.forEach \{ ys.add(it) \}patterns - Find
xs.toList().forEach \{ … \}patterns - Use
distinct()instead oftoSet().toList() - Use
first()instead ofget(0) - Use
lengthinstead ofString.count() - Use
sizeinstead ofCollection.count() - Use
toList()instead oftoMutableList().toList() - Use
toSet()instead ofdistinct().toSet() - Use
toSet()instead oftoList().toSet() - Use
toTypedArray()instead oftoList().toTypedArray() - Use
trim()instead oftrimEnd().trimStart() - Use
trim()instead oftrimStart().trimEnd()