Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cyclomatic complexity metric practices for Python

I have a relatively large Python project that I work on, and we don't have any cyclomatic complexity tools as a part of our automated test and deployment process.

How important are cyclomatic complexity tools in Python? Do you or your project use them and find them effective? I'd like a nice before/after story if anyone has one so we can take a bit of the subjectiveness out of the answers (i.e. before we didn't have a cyclo-comp tool either, and after we introduced it, good thing A happened, bad thing B happened, etc). There are a lot of other general answers to this type of question, but I didn't find one for Python projects in particular.

I'm ultimately trying to decide whether or not it's worth it for me to add it to our processes, and what particular metric and tool/library is best for large Python projects. One of our major goals is long term maintenance.

like image 491
Matt Messersmith Avatar asked Jul 13 '16 14:07

Matt Messersmith


People also ask

How do you check complexity in Python?

To understand Python code complexity we can take a look at Cyclomatic Complexity (proposed by Tomas McCabe in 1976), a metric used to calculate it. This is a measure of the linearly independent paths computed using the control-flow graph of your code.

What is cyclomatic complexity in code metrics?

Cyclomatic complexity (CYC) is a software metric used to determine the complexity of a program. It is a count of the number of decisions in the source code. The higher the count, the more complex the code.

Is cyclomatic complexity a good metric?

Cyclomatic Complexity is a poor Predictor of Code Complexity Cyclomatic Complexity was introduced back in 1976 as a quantitative metric of code complexity. Basically, cyclomatic complexity counts the number of logical paths through a function.


2 Answers

We used the RADON tool in one of our projects which is related to Test Automation.

RADON

Depending on new features and requirements, we need to add/modify/update/delete codes in that project. Also, almost 4-5 people were working on this. So, as a part of review process, we identified and used RADON tools since we want our code maintainable and readable.

Depend on the RADON tool output, there were several times we re-factored our code, added more methods and modified the looping.

Please let me know if this is useful to you.

like image 155
Dinesh Pundkar Avatar answered Sep 23 '22 13:09

Dinesh Pundkar


Python isn't special when it comes to cyclomatic complexity. CC measures how much branching logic is in a chunk of code.

Experience shows that when the branching is "high", that code is harder to understand and change reliably than code in which the branching is lower.

With metrics, it typically isn't absolute values that matter; it is relative values as experienced by your organization. What you should do is to measure various metrics (CC is one) and look for a knee in the curve that relates that metric to bugs-found-in-code. Once you know where the knee is, ask coders to write modules whose complexity is below the knee. This is the connection to long-term maintenance.

What you don't measure, you can't control.

like image 20
Ira Baxter Avatar answered Sep 23 '22 13:09

Ira Baxter