Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a standard way to count statements in C#

I was looking at some code length metrics other than Lines of Code. Something that Source Monitor reports is statements. This seemed like a valuable thing to know, but the way Source Monitor counted some things seemed unintuitive. For example, a for statement is one statement, even though it contains a variable definition, a condition, and an increment statement. And if a method call is nested in an argument list to another method, the whole thing is considered one statement.

Is there a standard way that statements are counted and are their rules governing such a thing?

like image 771
epotter Avatar asked Dec 04 '22 13:12

epotter


1 Answers

The first rule of metrics is "be careful what you measure". You ask for a count of statements, that's what you're going to get. As you note, that figure is perhaps not actually relevant.

If you're interested in other measures, like how "complex" code is, consider looking into other code metrics, like cyclometric complexity.

http://en.wikipedia.org/wiki/Cyclomatic_complexity

UPDATE: Re: your comment

I agree that "doing too much" is an interesting metric. My rule of thumb is that one statement should have one side effect (usually a "local" side effect like mutating a local variable, but sometimes a visible side effect, like writing to a file) and therefore "number of statements" should be roughly correlated with how much the method is "doing" in terms of its number of side effects.

In practice, of course no one's code, my own included, actually meets that bar all the time. You might consider a metric for "how much the method is doing" to count not just statements but also, say, method calls.

To actually answer your question: I'm not aware of any industry standard that regulates what "number of statements" is. The C# specification certainly defines what a "statement" is lexically, but then of course you have to do some interpretation to do a count. For example:

  void M()
  {
    try
    {
      if (blah)
      {
        Frob();
        Blob();
      }
    }
    catch(Exception ex)
    { /* eat it */ }
    finally
    {
      Grob();
    }
  }

How many statements are there in M? Well, the body of M consists of one statement, a try-catch-finally. So is the answer one? The body of the try contains one statement, an "if" statement. The consequence of the "if" contains one statement -- remember, a block is a statement. The block contains two statements. The finally contains one statement. The catch block contains no statements -- a catch block is not a statement, lexically -- but it certainly is highly relevant to the operation of the method!

So how many statements is that altogether? One could make a reasonable case for any number from one to six, depending on whether you count blocks as "real" statements, whether you consider child statements as in addition to their parent statement or not, and so on. There is no standards body which regulates the answer to this question that I'm aware of.

like image 142
Eric Lippert Avatar answered Dec 24 '22 10:12

Eric Lippert