Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cohesion and Coupling - How does one determine the threshold?

I'm taking further intro java classes at the moment and this is how the class briefly defined this:

Cohesion: Aim for high cohesion, in this case cohesion meaning that a single module is tightly focused on its task.

Coupling: Aim for low coupling, in this case coupling meaning the level of extent in how intertwined two or more modules are.

How does one determine the level of cohesiveness as well as coupling?

For instance, some of my methods call other methods that are in the same class. This means that the method that calls other methods are dependent on the other methods in order for the "calling" method to finish its code block. Does this mean that I have low cohesion and high coupling on the methods of the same class? Or do these concepts refer more to different classes and different packages?

like image 669
simmonson Avatar asked Oct 31 '25 13:10

simmonson


1 Answers

Cohesion and decoupling are important at all levels: each line of code should have a specific meaning and purpose, each method should have a specific meaning and purpose, each class should have a specific meaning and purpose, each package should have a specific meaning and purpose, each code repository should have a specific meaning and purpose.

This doesn't mean that a method shouldn't call another method, that a class shouldn't use another class, etc.; rather, it means that ideally, you should be able to read and understand a method without reading every other method it calls, to read and understand a class without reading every other class it uses, etc.

That said, we expect greater cohesion and decoupling for larger units: classes need to be much more cohesive and much less tightly coupled than methods do, for example, because assuming your classes are a reasonable size, you can much more easily go back and forth between methods than between classes when you're reading and maintaining the code.

like image 121
ruakh Avatar answered Nov 02 '25 03:11

ruakh