JavaNCSS - Specification

Specification

Non Commenting Source Statements (NCSS)

Statements for JavaNCSS are not statements as specified in the Java Language Specification but include all kinds of declarations too.
Roughly spoken, NCSS is approximately equivalent to counting ';' and '{' characters in Java source files.

Actually, the NCSS counter gets incremented by one for each:
 
 

Examples Comment
Package declaration package java.lang;
Import declaration import java.awt.*;
Class declaration - public class Foo {
- public class Foo extends Bla {
Interface declaration public interface Able {
Field declaration - int a;
- int a, b, c = 5, d = 6;
No matter how many fields get actually declared through a comma separated list, and no matter if these fields get actually initialized, only one statement is counted. So "int a, b, c = 5, d = 6;" gets only +1 count, not four or even six (let me know if there is good reason to count it differently).
Method declaration - public void cry();
- public void gib() throws DeadException {
Constructor declaration public Foo() {
Constructor invocation - this();
- super();
Statement - i = 0;
- if (ok)
- if (exit) {
- if (3 == 4);
- if (4 == 4) { ; }
- } else {
expression, if, else, while, do, for, switch, break, continue, return, throw, synchronized, catch, finally
Label fine : normal, case, default
 
Not counted are empty statements, empty blocks or semicolons after closing brackets. Of course, comments don't get counted too. Closing brackets also never get counted, the same applies to blocks in general.

In some cases consecutive semicolons are illegal according to the JLS but JavaNCSS still tolerates them (thought JavaNCSS is still more strict as 'javac'). Nevertheless they are never counted as two statements.

Cyclomatic Complexity Number (CCN)

CCN is also know as McCabe Metric. There exists a much hyped theory behind it based on graph theory, but it all comes down to simply counting 'if', 'for', 'while' statements etc. in a method. Whenever the control flow of a method splits, the "CCN counter" gets incremented by one.

Each method has a minimum value of 1 per default. For each of the following Java keywords/statements this value gets incremented by one:

  • if
  • for
  • while
  • case
  • catch
  • &&
  • ||
  • ?
Note that else, default, and finally don't increment the CCN value any further. On the other hand, a simple method with a switch statement and a huge block of case statements can have a surprisingly high CCN value (still it has the same value when converting a switch block to an equivalent sequence of if statements).