Home » Software Development » Object Cohesion: Why It Matters

About Yegor Bugayenko

Yegor Bugayenko
Yegor Bugayenko is an Oracle certified Java architect, CEO of Zerocracy, author of Elegant Objects book series about object-oriented programing, lead architect and founder of Cactoos, Takes, Rultor and Jcabi, and a big fan of test automation.

Object Cohesion: Why It Matters

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.

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:

01
02
03
04
05
06
07
08
09
10
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:

01
02
03
04
05
06
07
08
09
10
11
12
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:

1
2
3
4
5
6
7
8
9
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.

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.

Published on Java Code Geeks with permission by Yegor Bugayenko, partner at our JCG program. See the original article here: Object Cohesion: Why It Matters

Opinions expressed by Java Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of