Translation.validate_new_unit_data
has a cyclomatic complexity of 20 with "high" risk1626 skip_existing=True,
1627 )
1628
1629 def validate_new_unit_data(1630 self,
1631 context: str,
1632 source: str | list[str],
Translation.add_unit
has a cyclomatic complexity of 45 with "very-high" risk1370 return result
1371
1372 @transaction.atomic
1373 def add_unit( # noqa: C9011374 self,
1375 request,
1376 context: str,
Translation.handle_upload
has a cyclomatic complexity of 20 with "high" risk1179 return (0, skipped, accepted, len(store.content_units))
1180
1181 @transaction.atomic
1182 def handle_upload( # noqa: C9011183 self,
1184 request,
1185 fileobj: BinaryIO,
Translation.check_sync
has a cyclomatic complexity of 25 with "high" risk 318 # Store current unit ID
319 updated[id_hash] = newunit
320
321 def check_sync(self, force=False, request=None, change=None) -> None: # noqa: C901 322 """Check whether database is in sync with git and possibly updates."""
323 with sentry_sdk.start_span(op="check_sync", description=self.full_slug):
324 if change is 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. |