Your most unhappy customers are your greatest source of learning.

What is LCOM4 ?

Hi Everybody,

If you want to know about the LCOM4(Lack of Cohesion of Methods) which sonar having , please see below example:

Cohesion is the degree to which the methods of a single class are tight together. When two methods in a class do not use a common attribute or a common method, it means that they share nothing and should probably not belong to the same class to respect the Single Responsibility Principle. In other words you can split your class into several new classes to gain modularity at class level.

LCOM4 lack of cohesion example

  • LCOM4=1 indicates a cohesive class, which is the "good" class.
  • LCOM4>=2 indicates a problem. The class should be split into so many smaller classes.
  • LCOM4=0 happens when there are no methods in a class. This is also a "bad" class.
There is not any magic here, only common sense. Let’s take a simple example with class Driver. This class has two fields : Car and Brain, and five methods : drive(), goTo(), stop(), getAngry() and drinkCoffee(). Here is the dependency graph between those components. There are three blocks of related components, so LCOM4 = 3, so the class seems to have three different responsibilities and breaks the Single Responsibility Principle.


Hope this will helpful to you to understand the LCOM4 concept which can be used for any programming language.

No comments:

Post a Comment

Most Reading

 

Like Me & Share

Buy Websites PRchecker.info

Members

Ranks