We’ve all been there. You inherit a project, or maybe you’re just looking back at your own code from six months ago, and you think, “What on earth was I thinking?” It’s a tangled mess of logic, a digital plate of spaghetti where pulling on one noodle seems to move the entire dish. I call it The Great Unraveling.
For a long time, my assessment of “messy code” was purely a gut feeling. It was this vague, anxious sensation I’d get when opening a certain file. I knew it was hard to work with, a breeding ground for bugs, but I couldn’t prove it. I couldn’t point to a number and say, “See? This is the problem.”
So, a couple of weekends ago, fueled by coffee and a stubborn desire for clarity, I decided to build myself a little watchdog. A tool that could sniff through my Python projects and bark when it found something overly complicated. My goal wasn’t to build a giant, enterprise-level static analysis suite, but a simple script that could give me a quick health check.

My weapon of choice? Python, of course, and a neat little concept I’d read about called McCabe’s Cyclomatic Complexity.
Now, before you run for the hills, it’s not as scary as it sounds. Honestly. Forget the complicated formulas for a second. Think of it like this:
Imagine a function in your code is a simple road trip. If it’s just a straight shot down the highway from Point A to Point B, its complexity is 1. Super easy to follow.
But now imagine that road trip is through a busy city. Every time you hit an intersection where you have to make a decision (if this, while that, for each of these), the trip gets more complex. You have more possible paths you could take. McCabe’s metric basically just counts these decision points. A low number (like 1-5) is that simple highway. A high number (say, 15 or 20+) is a chaotic downtown core at rush hour. It’s a head-scratcher waiting to happen.
With this idea in mind, my plan was simple. I wanted my script to:
- Go on a scavenger hunt through a project directory and find all the Python files.
- For each file, read the code inside.
- This is the magic part: Hand that code over to a specialized library that understands the McCabe metric. I didn’t have to reinvent the wheel and parse the code myself (thank goodness). This library would act as my “complexity calculator.”
- The calculator would then spit back a list of all the functions in that file and a complexity score for each one.
- Finally, my script would just print out a nice, clean report. I decided to add a little flair and have it flag any function with a score over 10, just to draw my attention to the real troublemakers.
The first time I ran it on an old personal project was a revelation.
It was an instant “Aha!” moment. A function I absolutely dreaded touching, the one that always seemed to break in weird ways, lit up like a Christmas tree. Its complexity score was 28. Twentyeight! No wonder I couldn’t keep the logic straight in my head. Seeing that number validated all my gut feelings. It wasn’t just me; the code was objectively complicated.
But the most interesting part wasn’t just shaming my past self. It was seeing the patterns. The functions with scores of 1 or 2 were the simple, reliable helpers that did one thing and did it well. They were the dependable workhorses. The ones with scores of 10, 15, or that monstrous 28 were the ones trying to be a hero, juggling way too many responsibilities at once.
This little weekend project has genuinely changed how I approach my code. It’s not about achieving a perfect score of 1 everywhere—that’s not realistic. It’s about having a conversation with my code. When I see a high number now, I ask, “Can this be simpler? Could I break this massive function down into a few smaller, more manageable ones?”
It’s a tool, not a judge. It gives me a starting point for refactoring and a way to keep technical debt in check before it spirals out of control. If you’ve ever felt that sinking feeling when looking at a complex chunk of code, maybe it’s time to build your own little watchdog. It’s surprisingly satisfying to turn that vague feeling of “this is a mess” into a concrete, actionable number.
No Middlemen, No Miscounts: Building a Decentralized Crowdfunding Platform on Cardano with React