QR code

Object Cohesion: Why It Matters

  • Moscow, Russia
  • comments

OOPoop

You most probably know about Elegant Objects (EO), an alternative object-oriented paradigm, which claims that objects must be immutable, have no static methods, never use NULL in their code, use no annotations, and so on. We, the EO adepts, claim many things, but not so many people believe us. Those non-believers say that we are trolls, at best. Their main argument is: everybody works differently, why should we listen to you? I have no answer for them… well I had no answer, until I created jPeek and started researching object cohesion.

Кин-дза-дза! by Георгий Данелия
Кин-дза-дза! by Георгий Данелия

Let me explain how cohesion can help us, EO adepts, to prove some of our assumptions.

Cohesion, as a characteristic of a software module, was invented by Larry Constantine when I didn’t even exist yet, in 1974. Here is what it means; take a look at this simple Java class:

class Books {
  private List<String> titles;
  private List<Integer> prices;
  void addTitle(String title) {
    this.titles.add(title);
  }
  void addPrice(Integer price) {
    this.prices.add(price);
  }
}

There are two attributes and two methods. The method addTitle() works with the attribute titles, while the method addPrice() works only with the attribute prices. The cohesion is low in this class, because the attributes titles and prices are not related to each other in any way. We can easily break this class into two pieces without losing anything:

class Books1 {
  private List<String> titles;
  void addTitle(String title) {
    this.titles.add(title);
  }
}
class Books2 {
  private List<Integer> prices;
  void addPrice(Integer price) {
    this.prices.add(price);
  }
}

Now, we have two much more cohesive classes: their attributes and methods are related to each other. We can’t break Books1 anymore, since each attribute is needed by each method.

Here is yet another example of a highly cohesive class:

class Books {
  private List<String> titles;
  void add(String title) {
    this.titles.add(title);
  }
  void delete(int id) {
    this.titles.remove(id);
  }
}

Can we break it into smaller pieces? No, we can’t. We can’t take any part of the class out. The attribute titles and both of the methods must stay together. This means that the class is highly cohesive.

It was demonstrated a long time ago that more cohesive classes are better, in terms of their error-proneness, than the ones with low cohesion, for example by Victor R. Basili et al. in their study A Validation of Object-Oriented Design Metrics as Quality Indicators.

Now, if we can empirically prove that, for example, classes without static methods are, on average, more cohesive than their static-rich fellows, we can say that the claim that “static methods are evil” (postulated by Elegant Objects) is scientifically validated. We can take a large set of random Java classes and calculate their cohesion. Then, we can separate those with static methods from those without them. Next, we can calculate which group has higher average cohesion. If the group without static methods wins, the assumption would be valid.

Of course, another random set of Java classes may produce different results, but this is how empirical science works: we can’t prove the theorem other than by some experiments.

badge

I created an open source software library to help me do these experiments and called it jPeek.org. It is a calculator of cohesion metrics for Java code. No surprise, there are many metrics we can use to calculate cohesion. At least thirty of them are published, while only a few of them were properly implemented. In jPeek, thanks to its contributors, we managed to implement over a dozen.

Using this tool we can empirically prove some of the key points of EO. For example, we can prove that mutable classes are less cohesive, annotations negatively affect cohesion, DTOs are low-cohesion creatures, and many other things. Thus cohesion will become the vehicle which will drive Elegant Objects to a place where most of its claims will be scientifically proven. Give us a few more years and we will have very interesting results.

sixnines availability badge   GitHub stars