"Functions should not have too many lines of code" doesn't work

vsts

(De V1 L) #1

Hello!
I enabled the rule for C# “Functions should not have too many lines of code”. The rule has status “Activate In Quality Profile: The rule is already activated on all available quality profiles”.

I’m trying to check it by the code:

   TestSonar();
private static int TestSonar()
{
    int a1 = 1;
    a1++;
    a1++;
    a1++;
    a1++;
    a1++;
    a1++;
    a1++;
    a1++;
    a1++;
    a1++;
    a1++;
    a1++;
    a1++;
    a1++;
    a1++;
    a1++;
    a1++;
    a1++;
    a1++;
    a1++;
    a1++;
    a1++;
    a1++;
    a1++;
    a1++;
    a1++;
    a1++;
    a1++;
    a1++;
    a1++;
    a1++;
    return a1;
}

I see that Quality Gate and Quality Profile are used.

But the rule doesn’t work. What I did wrong?


(Adam Gabryś) #2

I read a little bit about SonarScanner for MSBuild and found interesting sentence:

Between the “begin” and “end” steps, you need to build your project, execute tests and generate code coverage data. This part is specific to your needs and it is not detailed here.

As you see SonarC# plugin needs compiled code. My assumtpion is that this rule verifies binary code. Your example method executes the same operation many times and I’m sure that compiler optimizes it. Please try to add more logic which cannot be replaced with one instruction (in your case return 32;).


(Amir Shiri) #3

Hello Adam,

Are you sure that is the designed behavior - examining the compiled code and not code written by developer? It sounds strange to me because Sonar can warn me about TODO comments for example.

Anyway, we configured the rule to 1 line max and copy and pasted one of our methods, but still not there is no warning for too long functions, however issues caused by other sources do appear:


(Amaury Levé) #4

Hi there,

This is a parameterized rule (i.e. you need to set the threshold on server side) with a default value of 80. As your methods contains less than 80 lines of code, no issue is raised on it.

Cheers,
Amaury


(De V1 L) #5

Hi,
I set it for 1 line, but there is no result.


(Amaury Levé) #6

Hi again,

Looking at the scanner logs you should see something like Invalid rule parameter: maximum number of lines = 1. Must be at least 2. I cannot tell you why this limit was set but you have to give a value greater than or equals to 2.

Let me know if there is still some issue.


(De V1 L) #7

Do you mean “Code Analysis” in the VSTS build logs?


(Amaury Levé) #8

If you look at the build step logs of your vsts build you should be able to find the message I mentioned (it should be prefixed by something like AD0001 - Analyzer).


(De V1 L) #9

I’ve set “Maximum authorized lines of code in a method” as 5 and rerun pull request.

But I don’t see anything similar on you’ve mentioned :hushed:
12.txt (581.6 KB)