Cohesion is not ensured when bundling data and operations

Cohesion is not ensured when bundling data and operations

Cohesion is not ensured when bundling data and operations in a class. However, bundling data and functionality is all about cohesion. Where does it go wrong?

Ok, let’s start from the beginning: What is cohesion? Cohesion is an expression of the degree in which the elements of a unit of code (you know, like a class ) fit together. Cohesion answers the question: in what level do these data fields and methods belong together? Just bundling some data and some methods inside a class doesn’t make your class ‘cohesive’. The trick is to bundle together what belongs together. So yes, making units of code cohesive requires actual mental effort! You need to think it through :).

Cohesion is not ensured because bundling data and operations is a choice you have to make as a developer and as the designer of the class. You can choose to separate your data and the methods and go for anemic classes. However, you build classes for a particular reason, for a particular goal (if you are a good programmer, each class only has one goal). Cohesive classes make code easier to understand and also a bit easier to test. That is a good thing for everybody who will ever need to work on that code. That includes your future self by the way!

Cohesion is not simple though. That is another reason why it is not ensured by just doing encapsulation. One of the biggest caveats I see when people make classes is not considering the context for that class. They create a model or a class that would fit more than one situation. For example, imagine you creating an online business platform. Your platform deals with customers or potential customers. But your platform also has users, such as admins and content managers.

So you might be tempted to create a ‘User’ class, because…. you know… customers ‘use’ your platform as well!

public class User {
    
    private String firstName;
    private String lastName;
    private String email;
    private String streetName;
    private String houseNumber;
    private String city;
    private Integer loyaltyPoints;
    private Order lastPurchase;
    private String[] roles;
    private Audit lastChanges;
    private String username;
    private String department;

    public void addToBasket(){}
    public Integer viewLoyaltyPoints(){}
    public void saveContent(){}
    public void deletePage(){}
}

That ‘User’ class is not very cohesive. Even though we can easily imagine a user having a firstname and an address, it is not needed for a user of the platform. A username is enough. It is the same thing for the saveContent method on a customer. It means nothing to them. That makes it so that the saveContent method and the name fields have absolutely nothing to do with each other.  That is because the purpose of an actual customer on the platform is totally different from the purpose of a content manager on the platform. In this case, the purpose is the context. Because of this difference in purpose, it is better to make 2 classes, a ‘User’ and a ‘Customer’ class, who are more cohesive.

Bundling data and methods/operations is a big and important part of encapsulation as a design technique. Want to know more about it? Check this video here!