Cognitive Complexity for Python vs C++


Just wondering. Why is the Cognitive Complexity allowed for Python 15, but is 25 for C++? The nesting allowed is also different. For C++ it’s 3, but for Python just seems to go by the 15, regardless of nesting. Just curious why it’s different.


Hello @Mel_W and welcome to our community!

Python and C++ are two different languages that provide different language features. So writing the same functionality can be more complex in C++ than in Python, or, in other words, having a complexity of 15 will be easier to reach in Python than in C++.
Having said that, even though Cognitive Complexity is language neutral, the threshold is quite arbitrary. Internally speaking, the default value of Cognitive Complexity is 15 for all languages. But we considered that this value was too low for C++, because too many issues were raised. So we increased the value to 25.
And the same reasoning can be applied for nesting :slightly_smiling_face:

Feel free to modify these thresholds at your convenience.

1 Like

Hi Amélie,

Thanks for the response.

Yes, Python is more succinct than C++. However, deep nesting in any language can be challening to follow, and most likely the code can be written more simply. Given that the nesting depth is a configurable threshold, and that the lack of statement block delimiters in Python gives me a headache, I’ll likely limit it to something reasonable.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.