What is a line of code and how Highlight counts them

Sometimes, this question is raised by new users: how CAST Highlight counts lines of code compared to classic static code analysis tools and how to explain possible differences. While there is no truer or better methodology than another for counting lines of code, the most important in order to get consistent results is to use the same approach to compare metrics over time and across applications, projects, etc.

In Highlight, the analyzers count physical lines of code where non-commented instructions can be found. Below are the different possible cases.

  • var foo = 42; => 1 line of code
  • var foo = 42; foo += 12; => 1 line of code
  • var foo = 42; // some comment => 1 line of code
  • { => 1 line of code
  • } => 1 line of code
  • { var foo = 42; } => 1 line of code
  • // some comment => 0 line of code
  • // var foo = 42 => 0 line of code
  • BLANK/EMPTY LINE => 0 line of code

Some basic examples

7 lines of JS code
// this is some JS code
var foo = 42; // my favorite number
var misc = 4;

while(misc > 0) {
   // foo = foo – misc;
   foo -= misc;
   misc -= 1;
}

/**
The result should be something like 32
**/

alert(foo);

10 lines of PHP code
<?php // account for 1 line of code

include(‘functions.php’);

/**
THIS IS A PHP SCRIPT
**/

$foo = 42; $i = 10;

while($i > 0)
{
   $foo = $foo – $i;
   $i = $i – 1;
}
?>
<div>
  <strong><?=$foo?></strong>
</div>