0.8.0 • Published 5 years ago

java-complexity v0.8.0

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

java-complexity

This tool approximates the cyclomatic complexity of a snippet of Java code, based loosely on these rules.

const javaComplexity = require('java-complexity');

const input = `
void main() {
    int number = 5;
    double pi = 22.0 / 7.0;

    if (number < 0) return;

    switch (number) {
    case 'a': break;
    case 2: break;
    case 5:
        System.out.println("pi is " + (pi > 3 ? "higher" : "lower") + " than 3.");
        break;
    default: break;
    }
}
`

const complexity = javaComplexity(input) // => { complexityValues: { main: 6 }, total: 6 }

When invoked from the command line, java-complexity reads input from stdin.

API

javaComplexity(input, suppressErrors, root)

input is a string of Java code to process. suppressErrors is an optional flag that will suppress error logging. root is an optional (string) root grammar rule.

This function returns a map of method names to cyclomatic complexity values. Unless root is specified, it attempts to parse the input as

  • an entire compilation unit (i.e package declaration, imports and class definitions)
  • the inside of a class body
  • the inside of a method

If all of these fail, the function returns null.

Invoking from the command-line

java-complexity [-s] [input]

The -s flag suppresses error logging. If input is not provided as an argument, it is read from stdin. This program exits with code 1 if it fails to parse the input string.

0.8.0

5 years ago

0.7.0

5 years ago

0.6.0

5 years ago

0.5.0

5 years ago

0.4.0

5 years ago

0.3.4

5 years ago

0.3.3

5 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago