Main Content

Comment Ratio

Ratio of number of lines that contain comments to the total number of lines in a file, expressed as percentage

Description

This metric calculates the ratio of lines containing comments to total number lines in a file, expressed as a percentage.

This metric is equivalent to the Comment Frequency metric defined by European Cooperation for Space Standardization (ECSS). As defined by EECS, Polyspace® calculates the Comment ratio metric using this formula

Comment ratio = 100 * Number of lines containing comments / (Number of code lines without comments + Number of lines containing comments)
A line containing any syntactical token is considered a code line. Consider this code:
int foo() {
    // Comment line 1
    // Comment Line2
    // Comment line 3
    
    int x = 0; // Inline comment ignored for comment density but considered for comment ratio

    return x;
}
This code contains three code lines without comment:

  • int foo(){

  • return x

  • }

There are four lines containing comment:

  • // Comment line 1

  • // Comment line 2

  • // Comment line 3

  • int x = 0; // Inline comment ignored for comment density but considered for comment ratio

Polyspace calculates the Comment Frequency as 4/(4+3) × 100% or 57%

This metric is calculated differently than the Comment Density metric, which calculates comment density as defined by the Hersteller Initiative Software (HIS) code complexity standard:

  • Polyspace calculates the comment density after the preprocessing stage whereas the comment ratio is calculated before preprocessing.

  • When calculating the comment density, Polyspace counts the three consecutive comments in foo() as a single comment. When calculating comment ratio, each line of comment is counted separately.

  • When calculating comment density, Polyspace ignores comments that are appended to lines of code. Such comments are counted when calculating comment ratio.

Examples

expand all

This example has two functions foo() and bar(). The code has 21 lines that contain comments, and 11 code lines that do not contain any comments.

/*=============================*/
/* external linkage functions  */
/*=============================*/

// routine foo
/* arg1:
 *
 * arg2:
 */
// return a negative in case of error
// Polyspace counts these consecutive lines of comments as a single comment for comment density, but counts them separately for comment ratio.


int /* return int type, counted as comment line
     counted as comment line*/ foo (
         /* void */ // counted as comment line
         ) { // counted as comment line 
    /* local variables */ // counted as comment line 
    int
        x; /* uninitailized at the declaration */ // counted as comment line 

    int r =
#if 1
        0;
#else
    -1;
#endif

    return r;
    /* same line as '}' counted as comment line   */ } /* this line still considered as containing comment
                                                       * As is this line
                                                       */
			 /* This is counted as a comment line 
                                        * even if not correctly indented. */
int bar() {
    return 1;
}

The comment ratio is therefore 100 × (21/(21+11)) = 66%.

Metric Information

Group: File
Acronym: COMR
HIS Metric : No