Do you have any clear reason/explanation for the default maximum maximum value of 15?
Hello,
I guess you’re hoping for some scientific formula. I can’t give you one.
When we first implemented the Cognitive Complexity rule, we borrowed the default threshold of 10 from Cyclomatic Complexity. Then we ran the rule against a lot of open source projects and looked at the results. Ten seemed to be a “noisy” threshold, so we bumped it up until we got results we were more comfortable with, finally landing at 15.
Also, it’s worth noting that when we went through this same process for C, C++ (and Objective-C) we landed at a default threshold of 25.
HTH,
Ann
Can this score (default value) be increased (cannot change it globally for all teams and projects)? I have a lof of false postivies for methods that can’t be made shorter.
It also doesn’t help that Sonar doesn’t understand C# local functions (which exists since 2017, so no excuses).
It calculates the store of the whole methods, even if some parts have been rewritten as local functions to reduce the cognitive load. It only lowers the score if those local functions are changed into private methods. However, having private methods that are only ever used by a single public methods is bad for maintenance.
So in short, this rule is working against its own goal. And the fact that is high means we can’t even ignore it. It breaks our quality gates in almost every project.
Here is an example. it is a sanitize method to remove control characters.
internal static string RemoveControlCharactersAndTruncate(string text, bool removeControlCharacters, int maxLength)
{
var builder = new StringBuilder(Math.Min(text.Length, maxLength));
var pendingHighSurrogate = default(char?);
foreach (var c in text)
{
if (removeControlCharacters && char.IsControl(c))
{
continue;
}
if (pendingHighSurrogate is { } high)
{
pendingHighSurrogate = null;
if (char.IsLowSurrogate(c))
{
if (builder.Length + 2 > maxLength)
{
break;
}
builder.Append(high).Append(c);
continue;
}
}
if (char.IsHighSurrogate(c))
{
pendingHighSurrogate = c;
continue;
}
if (char.IsLowSurrogate(c))
{
continue;
}
if (builder.Length >= maxLength)
{
break;
}
builder.Append(c);
}
return builder.ToString();
}
Sonar reports a complexity of 19.
Rewritten with local functions:
internal static string RemoveControlCharactersAndTruncate(string text, bool removeControlCharacters, int maxLength)
{
var builder = new StringBuilder(Math.Min(text.Length, maxLength));
var pendingHighSurrogate = default(char?);
foreach (var c in text)
{
if (removeControlCharacters && char.IsControl(c))
{
continue;
}
if (!TryAppend(c))
{
break;
}
}
return builder.ToString();
bool TryAppend(char c)
{
var high = pendingHighSurrogate;
pendingHighSurrogate = null;
if (high is { } pairStart && char.IsLowSurrogate(c))
{
return TryAppendPair(pairStart, c);
}
if (char.IsHighSurrogate(c))
{
pendingHighSurrogate = c;
return true;
}
if (char.IsLowSurrogate(c))
{
return true;
}
return TryAppendCharacter(c);
}
bool TryAppendPair(char high, char low)
{
if (builder.Length + 2 > maxLength)
{
return false;
}
builder.Append(high).Append(low);
return true;
}
bool TryAppendCharacter(char c)
{
if (builder.Length >= maxLength)
{
return false;
}
builder.Append(c);
return true;
}
}
Scores only lowered to 17. Still > 15.
Same code written with private methods (which is functionaly and cognitively equivalent):
internal static string RemoveControlCharactersAndTruncate(string text, bool removeControlCharacters, int maxLength)
{
var builder = new StringBuilder(Math.Min(text.Length, maxLength));
var pendingHighSurrogate = default(char?);
foreach (var c in text)
{
if (removeControlCharacters && char.IsControl(c))
{
continue;
}
if (!TryAppend(builder, ref pendingHighSurrogate, c, maxLength))
{
break;
}
}
return builder.ToString();
}
private static bool TryAppend(StringBuilder builder, ref char? pendingHighSurrogate, char c, int maxLength)
{
var high = pendingHighSurrogate;
pendingHighSurrogate = null;
if (high is { } pairStart && char.IsLowSurrogate(c))
{
return TryAppendPair(builder, pairStart, c, maxLength);
}
if (char.IsHighSurrogate(c))
{
pendingHighSurrogate = c;
return true;
}
if (char.IsLowSurrogate(c))
{
return true;
}
return TryAppendCharacter(builder, c, maxLength);
}
private static bool TryAppendPair(StringBuilder builder, char high, char low, int maxLength)
{
if (builder.Length + 2 > maxLength)
{
return false;
}
builder.Append(high).Append(low);
return true;
}
private static bool TryAppendCharacter(StringBuilder builder, char c, int maxLength)
{
if (builder.Length >= maxLength)
{
return false;
}
builder.Append(c);
return true;
}
Score lowered to 5 (by my account), even though the code has the exact same shape. I would even say that the cognitive complexity should be lower for local functions since it helps understanding it to see that it can only be called by the method.
Hi @nmusset,
You can absolutely change the threshold value in your quality profile, assuming you’re not using a Sonar way profile. Unfortunately, Sonar way isn’t directly editable, so your best option in that case would be to create a second profile that inherits from Sonar way (so you’ll pick up changes, going forward) and edit that.
HTH,
Ann
Hi @nmusset,
Local functions in C# are scored as follows:
-
Non-static local functions count as part of the enclosing method, one nesting level deeper. This is the same treatment lambdas get, because they can capture variables
-
Static local functions don’t count toward the enclosing method. Each is analyzed on its own and checked against the threshold.
So if your helpers don’t capture locals, try marking them static.
I’ll discuss internally if we should change how to handle this and keep you posted.
Regarding the threshold as @ganncamp mentioned you can raise it in a profile that inherits from Sonar way.
EDIT: I’ve added a backlog ticket (internal only) to count also the non-static functions as single units.
Ann’s explanation that the threshold came from tuning against open source projects rather than a formula makes sense, though it leaves teams like yours with little formal basis to argue for a project-specific value. The C# local function point is the more concrete issue: if the analyzer folds their complexity into the enclosing method, rewriting for readability won’t lower the score, so the rule punishes the refactoring it should reward. That does sound like a mismatch worth raising as a language-specific analyzer issue, separate from the default value debate.
Hello @nicholasrobinson
First of all welcome to the forum!
Indeed I agree with you. We discussed this internally yesterday and we agreed to keep all local functions (static and non static) as separate from their enclose method in terms of complexity. We do have a language specific issue for this in our backlog (internal only).
Thank you!