Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve cyclomatic complexity #842

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ilya-anashkin
Copy link

@ilya-anashkin ilya-anashkin commented Sep 23, 2022

Hi! Using the example of my PR, I would like to show that the library needs to improve its cyclomatic complexity.
I tested with the utility radon

My changes

SummaryMetricFamily.__init__ - B (7)
to
SummaryMetricFamily.__init__ - B (6)

HistogramMetricFamily - B(7)
to
Deleted from >=B level complexity

HistogramMetricFamily.__init__ - B(7)
to
HistogramMetricFamily.__init__ - B(6)

CollectorRegistry._get_names - B(7)
to
CollectorRegistry._get_names - B(6)
```

```
629 blocks (classes, functions, methods) analyzed.
Average complexity: A (2.1701112877583464)

to

629 blocks (classes, functions, methods) analyzed.
Average complexity: A (2.1701112877583464)
```

Copy link
Member

@csmarchbanks csmarchbanks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution, I am not sure if this is an improvement over the old code, generally lowering cyclomatic complexity isn't a goal in and of itself, the goal is the most readable code. I think some of these changes help with readability, e.g. the change to registry.py, but not all of them.

if labels is None:
labels = []
elif value is not None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the old logic to be clearer. It was clearly a check at the very beginning to make sure the input was valid, then we assigned a default value. Now we are doing both at the same time which IMO is harder to understand.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants