Search code examples
javasonarqubejacocolines-of-code

Lines of Code VS Instructions while measuring code quality


I have a projet composed of numerous modules. I am running both JaCoCo for unit tests coverage and Sonar for code quality.

For a technical reason, I can't use JaCoCo reports for one of my modules (GWT erases the target folder and I couldn't go past this issue yet).

Let's say I have 8 modules, from 1 to 8. One of them is for domain objects only, so I don't want to cover it with my tests. Same goes for another one, dedicated for auto-generated classes.

JaCoCo runs on 5 modules, and Sonar on 6 modules.

The total instructions shown by JaCoCo is 145k.

Sonar shows a total of 75k LOC.


Aren't they quite the same ? Did I miss something ? Is JaCoCo taking in account the whole project whatever reports I feed him ? What can possibly explain this gap in measurement ?


Solution

  • Aren't they quite the same ?

    Not at all.

    From http://www.jacoco.org/jacoco/trunk/doc/counters.html :

    The smallest unit JaCoCo counts are single Java byte code instructions.

    comparison of "instructions" with "lines of code" is like comparison of apples and oranges - they don't represent the same thing. Single line of code usually contains many bytecode instructions.

    For example

    System.out.println("Hello, World!");
    

    is a single line, but 3 bytecode instructions as can be seen using javap (Java Class File Disassembler):

         0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
         3: ldc           #3                  // String Hello, World!
         5: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
    

    BTW JaCoCo also counts lines. But while comparing this one with LoC in SonarQube, please take into account that algorithms of calculation are different - JaCoCo computes this number by analyzing information recorded by compiler in bytecode, while SonarQube computes this number by analyzing source code.