LLOC

Calculate logical lines of code (LLOC) incrementally.

Usage

var incrlloc = require( '@stdlib/_tools/static-analysis/js/incr/lloc' );

incrlloc()

Returns an accumulator function for incrementally calculating logical lines of code (LLOC).

var accumulator = incrlloc();

accumulator( [input] )

If provided an input program, the accumulator function returns an updated logical lines of code (LLOC) statistical summary. If not provided an input program, the accumulator function returns the current LLOC summary state. The input program may be either a string or a Buffer.

var lloc = accumulator();
// returns {}

lloc = accumulator( 'var x = 5;' );
// returns {...}

lloc = accumulator( 'var y = foo( 3.14 );' );
// returns {...}

lloc = accumulator( 'if ( x < y ) {\n\tconsole.log( "x < y" );\n}\n' );
// returns {...}

lloc = accumulator();
/* e.g., returns
  {
    "count": 3,        // number of programs
    "min": 1,          // minimum LLOC in a program
    "max": 2,          // maximum LLOC in a program
    "range": 1,        // max - min
    "midrange": 1.5,   // (max-min)/2
    "sum": 4,          // total LLOC
    "mean": ~1.33,     // average LLOC per program
    ...
  }
*/

Notes

  • Logical lines of code (LLOC) is defined as the number of executable statements. In comparison to the metric source lines of code (SLOC), calculating LLOC is not sensitive to stylistic forms. In the following example,

    var x = -5.0;
    
    // Shorthand:
    if ( x < 0.0 ) x *= -1;
    
    // "Verbose":
    if ( x < 0.0 ) {
        x *= -1;
    }
    

    the shorthand and "verbose" form both correspond to 2 LLOC (1 LLOC for the if statement and 1 LLOC for the if block content).

  • For further details regarding how LLOC is calculated, see program-summary.

  • For each input program, the accumulator function generates a program summary for that program. Upon generating a program summary, the accumulator extracts LLOC results and updates the accumulator state.

  • If provided a malformed input program, the accumulator will throw an Error.

  • Summary statistics are computed via incrsummary.

Examples

var incrlloc = require( '@stdlib/_tools/static-analysis/js/incr/lloc' );

var prog;
var acc;

// Initialize an accumulator:
acc = incrlloc();

// Analyze a series of programs:
prog = 'var x = 5;\n';
acc( prog );

prog = 'var y = "beep";\n';
acc( prog );

prog = 'var z = {\n"foo": "bar"\n};\n';
acc( prog );

prog = 'function add( x, y ) {\n\treturn x + y;\n}\n';
acc( prog );

prog = 'for ( i = 0; i < 5; i++ ) {\n\tconsole.log( i );\n}\n';
acc( prog );

prog = 'if ( x < y ) {\n\tconsole.log( "x < y" );\n} else {\n\tconsole.log( "x >= y" );\n}\n';
acc( prog );

// Print the current state of the accumulator:
console.log( JSON.stringify( acc() ) );