๐ How to fix your Python code with Radon & Xenon before it's too late
You can catch issues with complexity or maintainability with Radon & Xenon and reduce your technical debt.
I've been talking about how to catch code issues when writing or merging pull requests.
Most of the time, you would follow these steps:
Make it work.
Optimize it.
Make it readable.
Make it easy to understand for outsiders.
Iโve been talking about the latter in:
You might have code reviews by another coworker, but some things are hard to catch.
Some questions might come up when running these checks:
How do we measure complexity from an unbiased point of view?
How do you really know if itโs hard or easy to maintain?
There is a way to start.
๐จ๐ปโ๐ป Checking your code with Radon & Xenon
๐ What is Radon?
Radon measures complexity and maintainability. In other words, it will check things like length, nested logics, readability, and how easy it is to maintain it.
It will provide a score for these 2 metrics, both grading ranges from A (simplest) to F (most complex):
Cyclomatic Complexity (CC)
Measures how complex a function is by counting the number of decisions or branches (e.g., if, for, while).
Lower complexity means simpler, easier-to-read code.
Maintainability Index (MI)
Tells you how easy the code is to maintain.
Uses the length of your code, the number of comments, and its complexity to calculate this score.
๐ What is Xenon?
Xenon builds on Radon by enforcing complexity rules.
You set a max complexity (like Grade B). Xenon checks if your code is within that limit.
How do you use them?
Iโve created a Github Repo to simulate the process: ๐จ๐ปโ๐ป Code Quality Checker with Radon and Xenon
๐จ๐ปโ๐ป Locally
Install both packages:
pip install radon xenon
Check the Cyclomatic Complexity (CC) and select the path. In this case, we are using the root:
radon cc ./ -s
This will output the grade of complexity (A-F) and the score for each method/function (1-100):
Check the Maintainability Index (MI). It will grade the maintainability (A-F) of each file:
radon mi ./
This will output the enforced grade. In this case, we will raise anything above B.
xenon --max-absolute B ./
You can modify the complexity threshold for Xenon by changing the --max-absolute
flag in your GitHub Actions workflow or when running it locally.
๐ฌ Docker & Github Actions
Iโve created a Dockerfile and a Github workflow file to showcase it.
This CI/CD snippet could be better. It could parse the output and send it via Slack. But, GitHub notifications might be enough.
I have left a sample pull request with failed checks so you can see how it works!
๐ฅ Bonus: fixing code applying with Radon/Xenon with AI!
Instead of just pasting code in ChatGPT or Claude and asking, "make it more readable", we can try something else.
In this example, I have used Cursor:
This is the answer from Claude 3.5 Sonnet๐ฅฐ:
The result of the code after applying the suggestions:
๐ TL;DR
๐จ๐ปโ๐ป Checking your code with Radon & Xenon
Radon measures complexity and maintainability.
Xenon enforces complexity rules with your own thresolds.
๐ How do you use them?
๐จ๐ปโ๐ป Locally by installing package and running the commands.
๐ฌ Docker & Github Actions to avoid dependencies issues.
๐ฅ Fixing code with Radon/Xenon and AI!
If you enjoyed the content, hit the like โค๏ธ button, share, comment, repost, and all those nice things people do when like stuff these days. Glad to know you made it to this part!
Hi, I am Alejandro Aboy. I am currently working as a Data Engineer. I started in digital marketing at 19. I gained experience in website tracking, advertising, and analytics. I also founded my agency. In 2021, I found my passion for data engineering. So, I shifted my career focus, despite lacking a CS degree. I'm now pursuing this path, leveraging my diverse experience and willingness to learn.