High Cyclomatic complexity score
Running radon on Linchpin code show bad complexity scores:
➜ radon cc linchpin/ -nc -s
linchpin/__init__.py
M 460:4 LinchpinAPI.do_action - E (38)
M 805:4 LinchpinAPI.get_run_data - C (18)
M 392:4 LinchpinAPI.get_pf_data_from_rundb - C (17)
M 239:4 LinchpinAPI.lp_journal - C (14)
M 936:4 LinchpinAPI._invoke_playbooks - C (13)
linchpin/InventoryFilters/JSONInventoryFormatter.py
M 48:4 JSONInventoryFormatter.add_ips_to_groups - C (11)
linchpin/cli/__init__.py
M 247:4 LinchpinCli._write_distilled_context - D (24)
M 128:4 LinchpinCli._write_to_inventory - C (16)
M 526:4 LinchpinCli._execute_action - C (14)
linchpin/shell/__init__.py
F 436:0 journal - D (29)
F 26:0 _handle_results - C (13)
F 229:0 up - C (12)
F 321:0 destroy - C (11)
linchpin/provision/library/async_status_custom.py
F 58:0 main - C (11)
linchpin/provision/library/bkr_server.py
M 47:4 BkrFactory.provision - D (29)
linchpin/provision/library/rundb.py
F 85:0 main - D (23)
linchpin/provision/roles/ovirt/files/inventory.py
M 53:4 Inventory.config_value_helper - C (13)
linchpin/provision/roles/openstack/files/inventory.py
C 8:0 Inventory - C (13)
M 11:4 Inventory.get_host_data - C (13)
linchpin/validator/__init__.py
M 64:4 Validator.validate_pretty - C (17)
M 321:4 Validator._convert_topology - C (13)
linchpin/validator/anyofvalidator.py
M 10:4 AnyofValidator._validate_anyof - C (11)
linchpin/fetch/fetch.py
M 62:4 Fetch.copy_dir - C (18)
| Complexity Number | Meaning |
|---|---|
| 1-10 | Structured and well written code, High Testability Cost and Effort is less |
| 10-20 | Complex Code Medium, Testability Cost and effort is Medium |
| 20-40 | Very complex Code Low, Testability Cost and Effort are high |
| over 40 | Not at all testable, Very high Cost and Effort |
reference: https://www.guru99.com/cyclomatic-complexity.html https://en.wikipedia.org/wiki/Cyclomatic_complexity https://github.com/rubik/radon/
Breaking down by levels:
E
linchpin/init.py M 460:4 LinchpinAPI.do_action - E
D
linchpin/cli/init.py M 247:4 LinchpinCli._write_distilled_context - D linchpin/shell/init.py F 436:0 journal - D linchpin/provision/library/bkr_server.py M 47:4 BkrFactory.provision - D linchpin/provision/library/rundb.py F 85:0 main - D
C
linchpin/init.py M 805:4 LinchpinAPI.get_run_data - C M 392:4 LinchpinAPI.get_pf_data_from_rundb - C M 239:4 LinchpinAPI.lp_journal - C M 936:4 LinchpinAPI._invoke_playbooks - C linchpin/InventoryFilters/JSONInventoryFormatter.py M 48:4 JSONInventoryFormatter.add_ips_to_groups - C linchpin/cli/init.py M 128:4 LinchpinCli._write_to_inventory - C M 526:4 LinchpinCli._execute_action - C linchpin/shell/init.py F 26:0 _handle_results - C F 229:0 up - C F 321:0 destroy - C linchpin/provision/library/async_status_custom.py F 58:0 main - C linchpin/provision/roles/ovirt/files/inventory.py M 53:4 Inventory.config_value_helper - C linchpin/provision/roles/openstack/files/inventory.py C 8:0 Inventory - C M 11:4 Inventory.get_host_data - C linchpin/validator/init.py M 64:4 Validator.validate_pretty - C M 321:4 Validator._convert_topology - C linchpin/validator/anyofvalidator.py M 10:4 AnyofValidator._validate_anyof - C linchpin/fetch/fetch.py M 62:4 Fetch.copy_dir - C
Fixing this will probably require a few PRs. The first priority is probably to break up do_action() into multiple functions
Also, as you reduce the complexity, can you put together a guide of how to write "good" functions/code? I know some projects are pretty strict about limiting the amount of indentation or lines in a function but I don't know too much about best practices
Cool tool! Curious if you include a -a to the above command, whats' the overall score? Carbon ended up with a C.
23 blocks (classes, functions, methods) analyzed. Average complexity: C (17.0)
@14rcole, I can look for reference but what I like about this tool is that it will give absolute feedback (not an opinion).
@14rcole Add issues on reducing complexity.