How do you use the complexity metrics?

Hello :wave:,

As a lead dev, I often look at the complexity metric when a dev submit a PR; I take it as a first indicator of: “do I need to go deep in this one or not?” or “am i going to need a lot of energy for this?”.
But apart from that, I don’t know much how to make a better use of it. It’s great to have complexity score for each files but it would be helpful to have hints of which part are problematic, where to start to decrease this score, etc.

How do you use it? Do you have tips for taking the most of it?

2 Likes

Hello Romain!

Codacy calculates complexity using the Cyclomatic Complexity concept.

For your answer, I think this quote (sorry for being a quote but I really like it!!!) is very interesting:

One of McCabe’s original applications was to limit the complexity of routines during program development; he recommended that programmers should count the complexity of the modules they are developing, and split them into smaller modules whenever the cyclomatic complexity of the module exceeded 10

So, when you start seeing your complexity increase, consider creating a different module to support what you are doing (e.g. that delegate class really needs to be inline?).

5 Likes

Hi @romain :slightly_smiling_face:

following Hélio’s answer (thank you @HelioCodacy :muscle:), we also have a blog article explaining code complexity that was written by our CEO @jaime: An In-Depth Explanation of Code Complexity.

Check it out and please let us know if these helped to clarify. Hopefully yes but looking forward to hearing your thoughts on it!

2 Likes

I just wanted to add that I think that @romain was on the spot when describing the pull requests use case: cyclomatic complexity is a great indicator to understand if code quality deteriorating for any given change.
CC can be harder to reason about when looking at or comparing whole modules given its infinite scale and not being related to the module size. However, something that you might find useful is looking at Codacy’s File list sorted by priority, which will help you understand which files are candidates of poor code quality, and then by consequence their modules.

3 Likes

Thanks for your responses (and sorry for mine to be that late).
So if I want to decrease CC, according to:

1 is the base complexity of a function
+1 for each ‘if’, ‘for’, ‘case’, ‘&&’ or ‘||’

I just need to chase if, for, etc.
@rafael I don’t find the priority sorting. In fact, I can’t sort anything on that view (when viewing Files tab on a PR). But maybe you meant “complexity”; it’s in this view that I already look for it. When clicking on a file with a complexity score, I see the code, but it would be great to have highlights on where the complexity is (OK, now I know that I must look for control flow).

1 Like

@romain I believe that Rafael is referring to the Files tab on the right, not the files tab within your PR.

Using the tab on the right you can order your files by the number of issues, complexity, duplication, and coverage (if you have the last one set up). This will allow you to find out which files are the most complex on your repo so you can start tackling them.

2 Likes

@Ivo
Ah OK, I see this one, thanks.

2 Likes