LCOM is always 1 in JArchitect and Metrics Reloaded

219 Views Asked by At

So, I've been studying design patterns and in the context of the Single Responsibility Principle I tried to calculate the Lack of Cohesion of methods (LCOM) in Java using Metrics Reloaded and JArchitect. Both programs always calculate LCOM to be 1 although in some cases it's clearly not. Even the below standard example of low cohesion has an LCOM of 1 in these programs:

package com.StyleM;

public class NumberManipulator {
    private int number;

    public int numberValue() {
        return number;
    }
    public void addOne() {
        number++;
    }
    public void subtractOne() {
        number--;
    }
}

To my understanding the LCOM in this example should be 1-(3/4) = 0.25, because there are in total 4 methods (including the constructor) and 3 of them use the number field. What am I doing wrong?

1

There are 1 best solutions below

0
On

For the calculation of LCOM the operation is: number of incoherent pairs - number of coherent pairs. In your example so we would say that the result is 0-3=-3.
In general, it is common not to appear negative values ​​for LCOM as a result and so in case of a negative number as result, we say LCOM = 0 which is the optimal.
Regarding the result that results from the programs, I would say that they just have a different implementation for its calculation.