Coupling and cohesion
Asked Answered
A

6

77

I'm trying to boil down the concepts of coupling and cohesion to a concise definition. Can someone give me a short and understandable explanation (shorter than the definitions on Wikipedia here and here)? How do they interact?

Thanks.

Anybody have a good, short example?

Azaleeazan answered 2/9, 2008 at 16:24 Comment(2)
One distinction that escaped me at first: coupling is between classes while cohesion is within a single class.Confident
Does this answer your question? Difference Between Cohesion and CouplingAverir
M
233

Coupling

  • Loose: You and the guy at the convenience store. You communicate through a well-defined protocol to achieve your respective goals - you pay money, he lets you walk out with the bag of Cheetos. Either one of you can be replaced without disrupting the system.

  • Tight: You and your wife.

Cohesion

  • Low: The convenience store. You go there for everything from gas to milk to ATM banking. Products and services have little in common, and the convenience of having them all in one place may not be enough to offset the resulting increase in cost and decrease in quality.

  • High: The cheese store. They sell cheese. Nothing else. Can't beat 'em when it comes to cheese though.

Marilou answered 2/9, 2008 at 16:37 Comment(3)
Humm, I was anticipating a wife example for high cohesion, I have to say, I'm a bit disappointed :)Louisiana
High: A jewelry store one may buy their wife's anniversary gift at - they deal only in items on the topic of jewelry & are specialized in it, so their items are higher quality & all gathered in one tidy, well-maintained store with nice style; those item traits help shoppers specifically wanting jewelry to complete their jewelry tasks (possibly including cleaning jewelry too) in a predictable/efficient experience. All pluses there are less-so at a jewelry counter in a department store with many unrelated departments as it's less specialized, & hard to find past other departments walking throughMicroelectronics
btw - I guess this Answer may also assume monogamy. And maybe also no divorcing or re-marrying (even after widowed).Microelectronics
B
71

Coupling - A measure of how much a module (package, class, method) relies on other modules. It is desirable to reduce coupling, or reduce the amount that a given module relies on the other modules of a system.

Cohesion - A measure of how closely related the members (classes, methods, functionality within a method) of a module are to the other members of the same module. It is desirable to increase cohesion as that indicates that a module has a very specific task and does only that task.

Bureaucracy answered 2/9, 2008 at 16:27 Comment(0)
S
30

Coupling means dependency on others.
Cohesion means completeness with itself.

Sopher answered 19/2, 2010 at 8:3 Comment(0)
N
27

One of the best comprehensive discussions of software design concepts related to OO (including these ones) is Bertrand Meyer's Object Oriented Software Construction.

Regarding 'coupling', he gives his Weak Coupling / Small Interfaces rule as follows:

If two modules communicate, they should exchange as little information as possible.

Meyer's material related to cohesion isn't ever boiled down to a single pithy statement, but I think this sentence from Steve McConnell's Code Complete sums it up pretty well:

Cohesion refers to how closely all the routines in a class or all the code in a routine support a central purpose

Numeration answered 2/9, 2008 at 16:46 Comment(0)
A
4

A quick-and-dirty way to measure coupling is to measure your import (or similar) statements.

Apia answered 2/9, 2008 at 16:34 Comment(0)
B
2

"Coupling is a measure of interdependencies between modules, which should be minimized" "cohesion, a quality to be maximized, focuses on the relationships between the activities performed by each module."

quoted from this paper: http://steve.vinoski.net/pdf/IEEE-Old_Measures_for_New_Services.pdf

Blare answered 2/9, 2008 at 16:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.