分析軟體複雜度的 Cyclomatic complexity

在「decomplexification continued」這邊看到很久前有讀過,但已經忘記又重新看到的東西:「Cyclomatic complexity」。 cURL 這邊拿來當作目標,但看起來應該不是直接 optimize 這個指標,而是透過這個指標來找哪些 function 過於複雜的問題 (這邊有個經典的 Goodhart's law 可以參考)。 去年開始看 Cyclomatic complexity 的時候有超過 100 的 function: In May 2025 I had ju...

Gea-Suan Lin's BLOG

So I've created a #wily report for one thousand Black (the Python formatter) commits.

You can see that there was a huge (and complex), 7k lines __init__.py that got broken up around May 2021.

There's a trend of increasing LOC and complexity.

This report shows me there's a lot to improve in wily's code. Hopefully I'm back at that.

@hugovk @ambv

#Python #Black #formatter #CodeStyle #Cyclomatic #Complexity #Halstead #codequality #codemetrics

https://devdanzin.github.io/black/

Wily Reports

Adding JSON support for #wily: now the results of diff, index, rank and report can be output as JSON.

If you can help with code review or tips on how to improve the output, I'm open to all suggestions.

Wily is a CLI application for tracking, reporting on complexity of Python code. It uses git to go through each revision and run complexity and code-analysis metrics over the code.

#python #Cyclomatic #Complexity #Halstead

https://github.com/tonybaloney/wily/pull/194

Generate JSON output by devdanzin · Pull Request #194 · tonybaloney/wily

This proof of concept adds a --json option to diff, index, rank and report, allowing to generate output in JSON format. Example output (excerpt from wily report -n 11 --json src\wily\commands\repo...

GitHub

Mike Driscoll: Learning About Code Metrics in #Python with Radon

Nice introduction to #radon:

"""
You can use Radon to help you find complex code in your code base.
[...]
Here are the four commands that radon currently can use:

cc: compute #Cyclomatic #Complexity
raw: compute raw metrics
mi: compute #Maintainability Index
hal: compute #Halstead complexity metrics
[...]
"""

#codequality #codemetrics

https://www.blog.pythonlibrary.org/2023/09/20/learning-about-code-metrics-in-python-with-radon/

Learning About Code Metrics in Python with Radon - Mouse Vs Python

There are many different tools that you can install to help you be a better Python programmer. For example, you might install pytest so that you can do

Mouse Vs Python

#radon calculates various metrics from #Python code, including Lines of Code (LOC) and number of comments for whole files.

For some other metrics, like #Cyclomatic #Complexity, it can also calculate values per function/method. So having LOC and other raw metrics for each function would be a nice new feature.

However, simple AST visitors don't work for that, as comments get lost when retrieving source code from AST with ast.get_source_segment().

1/n

#codequality

https://github.com/rubik/radon

At last, submitted a proposal for a #Python source code annotator for #wily, by @tonybaloney.

It allows inline visualization of code metrics in source code, inspired by @coveragepy

Uses #radon to calculate #Cyclomatic #Complexity and #Halstead metrics, then adds color coded metric values to a #Pygments highlighted source listing and allows changing the metrics for visualization.

The code works, there are screenshots and an example in the linked issue.

#codequality

https://github.com/tonybaloney/wily/issues/217

Generate annotated source listings with inline visualization of code metrics · Issue #217 · tonybaloney/wily

Wily should be able to generate color coded source listings with inline visualization of code metrics from detailed metric results. The inspiration for this comes from #88 (which has an early previ...

GitHub

Today I wrote some trivial #python code to convert .gitignore rules to #radon exclude/ignore patterns. It turns out it was completely unnecessary for our project (#wily), because we already pass a tight list of targets for radon to scan.

But it might be useful for upstream radon users, as an option to gather exclude/ignore rules from .gitignore. So I'll propose that as a new feature and see how it goes.

#codequality #Cyclomatic #Complexity #Halstead #gitignore

https://github.com/rubik/radon

GitHub - rubik/radon: Various code metrics for Python code

Various code metrics for Python code. Contribute to rubik/radon development by creating an account on GitHub.

GitHub

Proposed 2 simple PRs to #radon, adding class names to method names in Halstead reports. A third is in the works, adding line numbers to #Halstead metrics.

"Radon is a #Python tool that computes various metrics from the source code. Radon can compute:

#McCabe's complexity, i.e. #cyclomatic #complexity

raw metrics (these include SLOC, comment lines, blank lines, &c.)

Halstead metrics (all of them)

#Maintainability Index (the one used in Visual Studio)"

#codequality

https://github.com/rubik/radon

GitHub - rubik/radon: Various code metrics for Python code

Various code metrics for Python code. Contribute to rubik/radon development by creating an account on GitHub.

GitHub

Working on a #Python source code annotator for #wily, which uses #radon to calculate #Cyclomatic #Complexity and #Halstead metrics. It adds metric values to a #Pygments highlighted source listing and allows changing the metrics for visualization.

Because radon doesn't record line numbers for Halstead metrics, we do a little song and dance to add them. Will probably offer a patch to include them upstream.

#codequality 🐍

https://devdanzin.github.io/wily/annotated_src.annotator.py.html

Annotated source for src\annotator.py

Some more #Python code quality visualization improvements for #wily: it's now possible to annotate source code with #Halstead metrics (#Cyclomatic #Complexity was already working) and select which one to display and use to color code blocks.

See a sample report here: https://devdanzin.github.io/wily/ (click the filenames for the annotated source code).

Code for the annotator currently lives at https://github.com/devdanzin/wily/tree/annotated_source, I hope to submit it for upstreaming soon-ish.

#CodeQuality

Wily Reports