NodeMetadataProxy.get
has a cyclomatic complexity of 16 with "high" risk140 else:
141 return self._metastack
142
143 def get(self, path, default=NO_DEFAULT, _backwards_compatibility_default=True):144 if (
145 default == NO_DEFAULT and
146 _backwards_compatibility_default and
File.validate_attributes
has a cyclomatic complexity of 25 with "high" risk583 return 0, p.decode()
584
585 @classmethod
586 def validate_attributes(cls, bundle, item_id, attributes):587 if attributes.get('delete', False):
588 for attr in attributes.keys():
589 if attr not in ['delete'] + list(BUILTIN_ITEM_ATTRIBUTES.keys()):
run_local
has a cyclomatic complexity of 16 with "high" risk105 return ret
106
107
108def run_local(109 command,
110 data_stdin=None,
111 log_function=None,
_inject_concurrency_blockers
has a cyclomatic complexity of 18 with "high" risk223 items.add(TagFillerItem(bundle, tag, {'tags': {tag}}))
224
225
226def _inject_concurrency_blockers(items, node_os, node_os_version):227 """
228 Looks for items with BLOCK_CONCURRENT set and inserts daisy-chain
229 dependencies to force a sequential apply.
bw_metadata
has a cyclomatic complexity of 30 with "very-high" risk 90
91
92@exit_on_keyboardinterrupt
93def bw_metadata(repo, args): 94 target_nodes = get_target_nodes(repo, args['targets'])
95 key_paths = sorted([
96 tuple(path.strip().split("/")) for path in args['keys'] if path
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:
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")
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])
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. |