KT.MAGIC_NUMBER
Magic number in the code
This rule detects and reports usages of magic numbers in the code. Prefer defining constants with clear names describing what the magic number means.
Noncompliant Code
class User {
fun checkName(name: String) {
if (name.length > 42) {
throw IllegalArgumentException("username is too long")
}
// ...
}
}
Compliant Code
class User {
fun checkName(name: String) {
if (name.length > MAX_USERNAME_SIZE) {
throw IllegalArgumentException("username is too long")
}
// ...
}
companion object {
private const val MAX_USERNAME_SIZE = 42
}
}
Options
-
excludes
(default:['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/jsTest/**', '**/iosTest/**', '**/*.kts']
)path filter
-
ignoreNumbers
(default:['-1', '0', '1', '2']
)numbers which do not count as magic numbers
-
ignoreHashCodeFunction
(default:True
)whether magic numbers in hashCode functions should be ignored
-
ignorePropertyDeclaration
(default:False
)whether magic numbers in property declarations should be ignored
-
ignoreLocalVariableDeclaration
(default:False
)whether magic numbers in local variable declarations should be ignored
-
ignoreConstantDeclaration
(default:True
)whether magic numbers in constant declarations should be ignored
-
ignoreCompanionObjectPropertyDeclaration
(default:True
)whether magic numbers in companion object declarations should be ignored
-
ignoreAnnotation
(default:False
)whether magic numbers in annotations should be ignored
-
ignoreNamedArgument
(default:True
)whether magic numbers in named arguments should be ignored
-
ignoreEnums
(default:False
)whether magic numbers in enums should be ignored
-
ignoreRanges
(default:False
)whether magic numbers in ranges should be ignored
-
ignoreExtensionFunctions
(default:True
)whether magic numbers as subject of an extension function should be ignored