Writing simpler and more maintainable Python 

Python Wily

Your code is read by humans and Python makes it especially easy for people to understand. You should always aim to make what you are trying to achieve with that function easy to understand not just when it comes to code comments, but also in your code structure. In this article, we will focus on Python code complexity and how to ensure your code is as readable as possible.

What is Python code complexity?

To understand Python code complexity we can take a look at Cyclomatic Complexity (proposed by Tomas McCabe in 1976), a metric used to calculate it. This is a measure of the linearly independent paths computed using the control-flow graph of your code. The control-flow graph is a representation of all paths that might be traversed through a program during its execution.

Is my Python code complicated?

The benefits of measuring software complexity

1. Better tests

By knowing how many code paths there are, you can know how many paths you have to test. As a result, you’ll have an idea of how many tests are required, at a minimum, to ensure that the code’s covered.

2. Reduced Risk

  1. It’s harder to read code than to write it and code is read far more than it is written.
  2. A good software developer should never be assessed by the lines of code they’ve written (or changed), but by the quality of the code they’ve maintained.

3. Lower costs

The Relative cost of fixing bugs

How do we measure Python code complexity?

Well, here is were Wily comes in handy. 

But let’s understand how to measure it first…

Cyclomatic complexity equals the number of decisions in the source code. The higher the count, the more complex the code. 

Let’s take a look at an example in this code


Every time there is an if block in Python, code complexity increases by 1. This code has 3, which means it is not that complex. Another way to see Python code complexity is by turning your code on its side and drawing a line around the white spaces, which will look like this:

code block in Python

Basically, the higher the curves, the higher the Python code complexity.

Let’s take a look at some math that Wily applies to better understand the metrics:

Operands = number of values + number of variables:

Python code complexity

Operators = number of built-in syntax:

Python code complexity

Length = number of operands + number of operators:

Python code complexity

Vocabulary = unique operands + unique operators:

Python code complexity

Volume = length * Log(Vocabulary): (better indicator than Lines of code)

Python code complexity

Difficulty (D):

Python code complexity

We can obtain all these metrics from our code just by using Radon

Radon command

And finally, by combining all of these numbers, we can obtain our maintainability index: 

Maintainability index

The maintainability index is the “rank” of how maintainable each file in your code is, and you should aim to improve this number to reduce the costs of maintaining your code.

Willy will save you time and money by presenting you with cool reports like this one:

Python code report

Moreover, Wily will help you see and track the evolution of your project’s complexity index with your version control systems so you can keep your code clean and simple.  

Python code complexity: Writing simpler and more maintainable Python

Final thoughts on Python code complexity

As you can see, Wily is a very powerful tool that will help you write maintainable code, make other programmers happy, and make you a happier programmer when it comes to reading your and your team’s code. It is definitely worth a try.