[ANN] CodeComplexity.jl – Cyclomatic complexity for Julia

[ANN] CodeComplexity.jl – Cyclomatic complexity for Julia

I’ve released CodeComplexity.jl, a small package to measure cyclomatic complexity of Julia code. It helps find overly complex functions, set complexity limits in tests or CI, and scan files, directories, or whole packages.

What it does

  • Per-function complexity – Counts decision points (if/elseif, for/while, try/catch, short-circuit &&/||, ternary, @goto) plus one.

  • Scopes – Single expression, code string, file, directory (recursive), or package (by name or loaded module).

  • CI-friendlycheck_complexity(pkg; max_complexity=10) throws if any function exceeds the limit, so you can fail tests or CI when complexity gets too high.

  • Pairs well with LLMs – When using AI coding assistants, complexity metrics give you a concrete threshold to enforce: reject or refactor generated code that exceeds your limit, and use per-function reports to prompt “simplify this” where it matters.

Quick start


using Pkg; Pkg.add("CodeComplexity")

using CodeComplexity

# Per-function report from a file

fc = file_complexity("src/MyModule.jl")

# Scan a package and enforce a limit (e.g. in tests)

using MyPackage

check_complexity(MyPackage; max_complexity=10)

Links

If you care about keeping functions understandable and testable, or want a single check to gate complexity in CI, CodeComplexity.jl is there. Feedback and contributions welcome.

27 Likes

Nice! A quick question, have you considered using JuliaSyntax.jl over Meta.parseall?

4 Likes

Yeah, that’d probably be better. Honestly, I had looked at the implementation in complex-structure (C901) | Ruff (source), and just asked Opus 4.6 to do this. It was able to one-shot a solution, but took an extra iteration to process Julia Base–and there are some pretty complex functions in there!

CodeComplexity.directory_complexity("../julia/base"; max_complexity=100)
2-element Vector{FileComplexity}:
 FileComplexity("../julia/base/precompilation.jl", FunctionComplexity[FunctionComplexity("_precompilepkgs", complexity=176 (line 578))], 176)
 FileComplexity("../julia/base/show.jl", FunctionComplexity[FunctionComplexity("show_unquoted", complexity=272 (line 1916))], 272)

I’m too busy to sink much time into this, but I might be able to ask Opus to see if there’s refactoring opportunity with JuliaSyntax. If you don’t see something in the next month, feel free to open a PR!

1 Like

That was easy, commit

1 Like