OOP, cohesion, coupling and heuristics


In the OOP world, we do not need big, God like, classes that do a bit of everything. On the contrary, we need small classes that have specific roles and responsibilities. At the same time, each class has to be as independent as reasonably possible from the others, no matter if they belong in the same module or not.

In the OOP world a module is a package or a namespace, depending on the programming language you are using. The S.O.L.I.D principle that is focused on creating highly cohesive classes is the Single Responsibility Principle.

Think of your codebase as a multinational company, like LexCorp or Wayne Enterprises. You would not assign too many roles to a specific person, but you would not let them go rogue too. It is not that wise. There is always a hierarchy, a structure, a system of reasoning of who does what and why! It is the same in the OOP world. Always reason on how many classes you need and why, and which the role of each class. Try to avoid to be extremely verbal, ergo, do not create classes that have little or no role in your codebase!

Cohesion indicates the classes relationships within the module, while on the other hand coupling indicates the relationships among modules. The concepts of cohesion and coupling are not new at all. They were invented by Larry Constantine, and you can read more about them in the Structured Design book.

Finally, the OO Design Heuristics are a set of guidelines for good object-oriented designs. As Bjarne Stroustrup (father of the C++ programming language) correctly pointed out many years ago, just making a design “object oriented” doesn’t always mean that the design is good.

Continue reading “OOP, cohesion, coupling and heuristics”