Types of Cohesion: From Weak to Strong
You'll learn: The seven levels of cohesion, from the weakest (coincidental) to the strongest (functional), and how to recognize each in real code.
The Cohesion Spectrum
Cohesion measures how closely related the responsibilities within a module are. Not all cohesion is created equal—there's a hierarchy from "barely related" to "perfectly unified."
Coincidental Cohesion (Weakest)
Elements grouped randomly with no meaningful relationship. Like throwing kitchen utensils, car parts, and books into one drawer because "they fit."
Example: A Utilities class containing calculateTax(), formatDate(), and sendEmail().
Logical Cohesion
Elements perform similar categories of operations but aren't actually related. Like a "tools" drawer—hammers and screwdrivers are both tools, but you use them for different jobs.
Example: An InputHandler that processes keyboard, mouse, and file input in one class.
Temporal Cohesion
Elements executed at the same time are grouped together. Like packing everything you need "in the morning" into one bag.
Example: An initialize() function that connects to a database, loads config files, and sets up logging.
Procedural Cohesion
Elements follow a specific sequence of execution. They're grouped because they happen in order, not because they share a goal.
Example: readData(), processData(), saveData() in one module—sequential but serving different purposes.
Communicational Cohesion
Elements operate on the same data but perform different operations.
Example: A module that reads a customer record and then calculates their total purchases from that same record.
Sequential Cohesion
Output from one element becomes input to the next—a pipeline of transformations.
Example: A module that validates user input, then sanitizes it, then formats it.
Functional Cohesion (Strongest)
All elements contribute to a single, well-defined task. Like a can opener—everything in it exists solely to open cans.
Example: A calculateTotalPrice() function that only computes price—nothing else.
Key Takeaway: Aim for functional cohesion where every element serves one clear purpose; avoid coincidental cohesion where unrelated responsibilities clutter a module.