What causes high cyclomatic complexity in practice?

273 Views Asked by At

I am trying to understand Cyclomatic complexity - mainly to get my head around if it's useful.

I did a quick check of my codebase and all the functions had a complexity < 6. This led me to try and write a "highly" complex function, but it seems very hard to do. I tried:

def foo(x):
    
    with _ as file:
        for _ in range(500):
            try:
                while x: 
                    if True:
                        pass
                    elif False:
                        pass
                    else:
                        pass
            except:
                pass
            else:
                pass
                
        return True

and this got a score of 6.

I've read guidance that anything with a Cyclomatic complexity under 20 is "fine", but I can't imagine functions (at least in Python) with 10+ complexity.

What causes high Cyclomatic complexity in practice? Or am I missing something?

0

There are 0 best solutions below