A function with high cyclomatic complexity can be hard to understand and maintain. Cyclomatic complexity is a software metric that measures the number of independent paths through a function. 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:
use anyhow::{anyhow, Context, Result};
use std::io::{Lines, StdinLock};
trait InputHandle {
type Input<'a>
where
Self: 'a;
fn get_input<'a>(&self) -> Self::Input<'a>;
fn process_input<'a, T>(&self, f: impl FnOnce(Self::Input<'a>) -> Result<T>) -> Result<T>
where
Self: 'a;
}
struct StdioInput;
impl InputHandle for StdioInput {
type Input<'a> = Lines<StdinLock<'a>>;
fn get_input<'a>(&self) -> Self::Input<'a> {
std::io::stdin().lines()
}
fn process_input<'a, T>(&self, f: impl FnOnce(Self::Input<'a>) -> Result<T>) -> Result<T>
where
Self: 'a,
{
f(self.get_input())
}
}
fn main() -> Result<()> {
// cc = 1 (default)
let index: usize = StdioInput.process_input(|mut input| {
// cc = 3 (?, ?)
str::parse(input.next().context("no input from stdin")??.as_str()).context("parse failed")
})?; // cc = 4 (?)
// cc = 5 (for)
for i in 0..index {
let fizzbuzz = match i % 15 {
// cc = 6 (match_arm 0)
0 => "fizzbuzz".into(),
// cc = 9 (match_arm 3 | 6 | 9)
3 | 6 | 9 | 12 => "fizz".into(),
// cc = 11 (match_arm 5 | 10)
5 | 10 => "buzz".into(),
x => format!("{x}"),
};
println!("{i}: {fizzbuzz}");
}
Ok(())
}
use anyhow::{anyhow, Context, Result};
use std::io::{Lines, StdinLock};
trait InputHandle {
type Input<'a>
where
Self: 'a;
fn get_input<'a>(&self) -> Self::Input<'a>;
fn process_input<'a, T>(&self, f: impl FnOnce(Self::Input<'a>) -> Result<T>) -> Result<T>
where
Self: 'a;
}
struct StdioInput;
impl InputHandle for StdioInput {
type Input<'a> = Lines<StdinLock<'a>>;
fn get_input<'a>(&self) -> Self::Input<'a> {
std::io::stdin().lines()
}
fn process_input<'a, T>(&self, f: impl FnOnce(Self::Input<'a>) -> Result<T>) -> Result<T>
where
Self: 'a,
{
f(self.get_input())
}
}
fn fizzbuzz(u: usize) -> String {
// cc = 1 (default)
match u % 15 {
// cc = 2 (match_arm 0)
0 => "fizzbuzz".into(),
// cc = 9 (match_arm 3 | 6 | 9)
3 | 6 | 9 | 12 => "fizz".into(),
// cc = 11 (match_arm 5 | 10)
5 | 10 => "buzz".into(),
x => format!("{x}"),
}
} // total cc =
fn main() -> Result<()> {
// cc = 1 (default)
let index: usize = StdioInput.process_input(|mut input| {
// cc = 3 (?, ?)
str::parse(input.next().context("no input from stdin")??.as_str()).context("parse failed")
})?; // cc = 4 (?)
// cc = 5 (for)
for i in 0..index {
let val = fizzbuzz(i);
println!("{i}: {val}");
}
Ok(())
}
Cyclomatic complexity threshold can be configured using the
cyclomatic_complexity_threshold
(docs) in the
.deepsource.toml
config file.
Configuring this is optional. If you don't provide a value, the Analyzer will
raise issues for functions with complexity higher than the default threshold,
which is high
(only raise issues for >25) for the Rust Analyzer.
Here's the mapping of the risk category to the 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. Recommended to add comments if the function is absolutely needed to be kept as it is. |
very-high. | 26-50 | Refactor to reduce the complexity. |
critical | >50 | Must refactor this. This can make the code untestable and very difficult to understand. |