Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count number of branches in Java class

Tags:

java

Is there a tool or a way to count the number of branches in a given Java class? For example, I want to count the number of branches in the following simple Java class:

public class Testt {

    public boolean getTest(int x) {
        if (x > 5) {
            return true;
        } else {
            return false;
        }
    }

    public int getTest1(int x) {
        int t = 0;
        if (x == 10) {
            t = 1;
        } else if (x == 8) {
            t = 3;
        } else {
            t = 11;
        }
        return t;
    }
}
like image 467
Nasser Avatar asked Jan 29 '23 04:01

Nasser


1 Answers

The term you are looking for is "cyclomatic complexity".

Cyclomatic complexity is a software metric (measurement), used to indicate the complexity of a program. It is a quantitative measure of the number of linearly independent paths through a program's source code.

If you are using Eclipse as your IDE there is a plugin called Eclipse Metrics that can give this kind of information.

like image 52
bhspencer Avatar answered Feb 02 '23 10:02

bhspencer