Function with cyclomatic complexity higher than threshold foundKT-R1006
Cyclomatic complexity is a software metric that measures the number of independent paths through a function. A function with high cyclomatic complexity can be hard to understand and maintain. A higher cyclomatic complexity indicates that the function has more decision points and is more complex.
Functions with high cyclomatic complexity are more likely to have bugs and be harder to test. They may lead to reduced code maintainability and increased development time.
To reduce the cyclomatic complexity of a function, you can:
- Break the function into smaller, more manageable functions.
- Refactor complex logic into separate functions or classes.
- Avoid multiple return paths and deeply nested control expressions.
Bad practice
import java.io.IOException
import java.nio.file.Path
import kotlin.io.path.reader
fun fizzbuzz(inputFile: Path?) { // 1
if (inputFile == null) return // +1
val max = try {
val reader = inputFile.reader()
reader.readText().toInt()
} catch (e: IOException) { // +1
println("An IO error occurred: ${e.message}")
return
} catch (e: NumberFormatException) { // +1
println("Bad number in input file!")
return
}
var i = 0
while (i < max) { // +1
val printStr = when (i % 15) {
0 -> "fizzbuzz" // +1
3, 6, 9, 12 -> "fizz" // +4
5, 10 -> "buzz" // +2
else -> i
}
println(printStr)
i += 1
}
}
Recommended
Break up the logic into smaller pieces.
import java.io.IOException
import java.nio.file.Path
import kotlin.io.path.reader
fun fizzbuzz(inputFile: Path?) { // 1
if (inputFile == null) return // +1
val max = readMaxFromFile(inputFile) ?: return
var i = 0
while (i < max) { // +1
printFizzBuzz(i)
i += 1
}
}
fun readMaxFromFile(inputFile: Path): Int? {
val max = try {
val reader = inputFile.reader()
reader.readText().toInt()
} catch (e: IOException) { // +1
println("An IO error occurred: ${e.message}")
null
} catch (e: NumberFormatException) { // +1
println("Bad number in input file!")
null
}
return max
}
fun printFizzBuzz(i: Int) {
val divBy3 = i % 3 == 0
val divBy5 = i % 5 == 0
val printStr = when {
divBy3 && divBy5 -> "fizzbuzz" // +2
divBy3 -> "fizz" // +1
divBy5 -> "buzz" // +1
else -> i.toString()
}
println(printStr)
}
Issue configuration
Cyclomatic complexity threshold can be configured using the
cyclomatic_complexity_threshold
meta field in your repository's
.deepsource.toml
config file.
Configuring this issue is optional. If you don't provide a value, the Kotlin analyzer will
raise issues for functions with complexity higher than the default threshold,
which is 15
(Medium) for Kotlin.
Here's a mapping of risk category to cyclomatic complexity score to help you configure this better:
Risk category | Cyclomatic complexity range | Recommended action |
---|---|---|
low | 1-5 | No action needed. |
medium | 6-15 | Review and monitor. |
high | 16-25 | Review and refactor. It is recommended to add explanatory comments if the function absolutely cannot be changed. |
very-high | 26-50 | Refactor to reduce the complexity. |
critical | >50 | The function must be refactored. Such high complexity can harm testability and readability. |