load_data_wfx
has a cyclomatic complexity of 16 with "high" risk117 )
118
119
120def load_data_wfx(lit: LineIterator) -> dict:121 """Process loaded WFX data."""
122 # get all section labels and required labels for WFX files
123 lbs_str, lbs_int, lbs_float, lbs_aint, lbs_afloat, lbs_other, required_tags = _wfx_labels()
build_obasis
has a cyclomatic complexity of 16 with "high" risk259 )
260
261
262def build_obasis(263 icenters: NDArray[int],
264 type_assignments: NDArray[int],
265 exponents: NDArray[float],
load_one
has a cyclomatic complexity of 20 with "high" risk 50 ],
51 ["athessian"],
52)
53def load_one(lit: LineIterator) -> dict: 54 """Do not edit this docstring. It will be overwritten."""
55 data = load_qchemlog_low(lit)
56
load_one
has a cyclomatic complexity of 22 with "high" risk174 "is raised when no suitable correction can be found."
175 },
176)
177def load_one(lit: LineIterator, norm_threshold: float = 1e-4) -> dict:178 """Do not edit this docstring. It will be overwritten."""
179 charge = None
180 atnums = None
_fix_molden_from_buggy_codes
has a cyclomatic complexity of 20 with "high" risk638 return None
639
640
641def _fix_molden_from_buggy_codes(result: dict, lit: LineIterator, norm_threshold: float = 1e-4):642 """Detect errors in the data loaded from a molden or mkl file and correct.
643
644 This function can recognize erroneous files created by PSI4, ORCA and
load_one
has a cyclomatic complexity of 26 with "very-high" risk371 {},
372 LOAD_ONE_NOTES,
373)
374def load_one(lit: LineIterator) -> dict:375 """Do not edit this docstring. It will be overwritten."""
376 # Find the element number
377 atnum = None
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:
- 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
def number_to_name():
number = input()
if not number.isdigit():
print("Enter a valid number")
return
number = int(number)
if number >= 10:
print("Number is too big")
return
if number == 1:
print("one")
elif number == 2:
print("two")
elif number == 3:
print("three")
elif number == 4:
print("four")
elif number == 5:
print("five")
elif number == 6:
print("six")
elif number == 7:
print("seven")
elif number == 8:
print("eight")
elif number == 9:
print("nine")
Recommended
def number_to_name():
number = input()
if not number.isdigit():
print("Enter a valid number")
return
number = int(number)
if number >= 10:
print("Number is too big")
return
names = {
1: "one",
2: "two",
3: "three",
4: "four",
5: "five",
6: "six",
7: "seven",
8: "eight",
9: "nine",
}
print(names[number])
Issue configuration
Cyclomatic complexity threshold can be configured using the
cyclomatic_complexity_threshold
meta field 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 medium
for the Python 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. |