Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is coupling and cohesion ?

 
Sathi Krishna
Ranch Hand
Posts: 51
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am very much confused about these two.

In which real scenarios we'll implement these in our development.

Please provide me the information related to coupling and cohesion.

Thank you
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer can be and is very lengthy and subjective:

1. Coupling is how much does a class know about other class. Cohesion is how well focused a class is: a class having a single(single responsibility pattern) well defined purpose is said to exhibit higher degree of cohesion..

2. Given two lines of code, A and B, they are coupled when B must change behavior only because A changed.
They are cohesive when a change to A allows B to change so that both add new value.

3. Loose coupling and High cohesion is desired.

Further Reading...
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sathi Krishna wrote:In which real scenarios we'll implement these in our development.


Whenever you lay hands on a piece of code, the two are involved.

Cohesion refers to the degree of specific tasks a class has. You always aim for a class that has one single purpose. It does one thing, and this one thing does it well.
A class that does read files, performs database updates and on top of that displays a GUI, is considered to have lousy cohesion.
The result is lousy maintainability and reusability.

Think about it. You would need the file reading capabilities in another project. With a highly cohesive class that only handles the file handling processes, no problem.

Coupling is the degree to which one class exposes itself to another. The aim is to hide as much of the implementation details as possible. In the best case you only expose what is defined in a common interface. The worst case would be having all methods public, even the ones that will (or should) never be used by the implementer. This, again results in lousy maintainability (because you break other peoples code as soon as you change your implementation details).
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cohesion:
Classes should have a small number of instance variables. Each of the methods of a class should manipulate one or more of the instance variables. In general the more variables a method manipulates the more cohesive that method is to its class. A class in which each variable is used by each method is maximally cohesive.

Please note that it is neither advisable nor possible to create such maximally cohesive classes. But when cohesion is high, it means that the methods and variables of the class are co-dependent and hang together as a logical whole.

You can refer to Clean Code by Robert C.Martin for more examples on this topic.

 
Sathi Krishna
Ranch Hand
Posts: 51
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now its clear.
Thank You Soooo Much !!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic