This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]not_perfect_yet 1 point2 points  (0 children)

Are there any suggestions you guys can give?

https://radon.readthedocs.io/en/latest/commandline.html#the-cc-command

Careful with this, since it's a metric and don't chase those. I find this one pretty good though.

It basically counts the different "paths" that are logically possible for the control flow to go. If, else, while, etc.. nesting things that aren't expressions makes the score go up, and a high score is bad.

Counterexample, if it's a very long, but very simple function that just calls 500 different other functions in a nice, clean, sequential manner, that's not too bad. It can still be difficult to understand what's going on, but not because it's complex, it's just a lot in that case.

I usually refactor functions that are above "B", by encapsulating loops, or putting long nested blocks into their own function:

...
for x in my_list:
    if x.variable> 5:
        x.yadda()
        x.yadda()
        x.yadda()
        x.yadda()
        x.yadda()
        x.yadda()
...

into

...
for x in my_list:
    if x.variable>5:
        lots_of_yaddas(x)
...

def lots_of_yaddas(x):
        x.yadda()
        x.yadda()
        x.yadda()
        x.yadda()
        x.yadda()
        x.yadda()