Customize C# LOC counter

Is there any way to customize the way lines of code are counted and reported? I am analyzing C# code and see that lines with curly brackets { } are being counted. We’ve run estimates based on a different tools LOC (using UCC) and want to switch to SonarQube. However, the differences in counts is big and we believe if these weren’t counted then we’d be aligned pretty well.

Hi,

There’s no way to customize that. Would you mind detailing what changes you think should be made?

 
Ann

Yes, I don’t think lines with opening and closing curly brackets should be counted.
{
}
There may be a reason for that (like coverage counters) but we did a quick comparison to UCC results and found this to be the biggest difference. It bloats your code counts quite a bit too, so if you use LOC for an estimates (yes this is a horrible practice but common) you can’t compare results.

Maybe a few basic options on counting logic.

Hi,

Thanks for getting back to me. To be honest, this isn’t likely to change, and certainly not in the near term. AND I’m passing this on internally.

 
Thanks!
Ann

Hello Danny,

We can debate and at the same time our definition of Lines of Code is clear in our documentation:

Lines of code ( ncloc )
Number of physical lines that contain at least one character which is neither a whitespace nor a tabulation nor part of a comment.

So it is expected that curly braces are considered as part of Lines of Code because they are not whitespace, not a tabulation, nor part of a comment.
Without the curly braces, the C# code won’t compile so they are part of the required lines to make the code do what it is supposed to do.

FYI, here how we compute Executable Lines. This “Executable Lines” metric (EL) is used in the computation of the Coverage and Line Coverage metrics.

Why do you absolutely need to be compliant with how UCC define LOC? What is your final use case?

Alex

These lines are counted for the license. There are other languages that don’t use brackets, so these have an advantage and can use a cheaper licence for the same amount of executable lines of code.

Microsoft provides a plugin in Visual Studio to shrink empty lines (Shrink Empty Lines - Visual Studio Marketplace), here they shrink “lines that contain neither letters nor numbers”.

As a comparison, the same code in VB.NET (14 lines), C# (25 lines) and python (10 lines):

Public Sub Test()
  Try
    If _doTest Then
      Select _version
        Case 1:
          ExecuteTest()
        Case Else:
          DoNothing()
      End Select
    End If
  Catch ex As Exception
    Log(ex)
  End Try
End Sub
public void Test()
{
  try
  {
    if (_doTest)
    {
      switch (_version)
      {
        case 1:
        {
          ExecuteTest()
          break;
        }
        default:
        {
          DoNothing();
          break;
        }
      }
    }
  catch (Exception ex)
  {
    Log(ex)
  }
}
def Test():
    try:
        if _doTest:
            match_version:
              case 1:
                ExecuteTest()
              case _:
                DoNothing()
    except Exception as ex:
        Log(ex)

Not counting the brackets in C# will produce 12 lines of code, right in between VB.NET (14) and Python (10). With the current counting, it will report 25 lines, what is more than double of both other languages. Especially with the licence based on lines of code, that means that everyone that uses C# will pay double for the same capabilities as other languages.