Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference Between Cohesion and Coupling

What is the difference between cohesion and coupling?

How can coupling and cohesion lead to either good or poor software design?

What are some examples that outline the difference between the two, and their impact on overall code quality?

like image 923
JavaUser Avatar asked Jun 21 '10 14:06

JavaUser


People also ask

Is cohesion related to coupling?

Cohesion and coupling are related to each other. Each can affect the level of the other. High cohesion correlates with loose coupling. A module having its elements tightly related to each other and serving a single purpose would sparingly interact and depend on other modules.

What is the relationship between cohesion and coupling of software components?

Cohesion is the degree to which the elements inside a module belong together. Coupling is the degree of interdependence between software modules. High cohesion and loose coupling are the most important principles in software engineering. They manifest themselves everywhere from code to team organization.

What is cohesion and coupling in Java example?

High cohesion within modules and low coupling between modules are often regarded as related to high quality in OO programming languages. For example, the code inside each Java class must have high internal cohesion, but be as loosely coupled as possible to the code in other Java classes.


1 Answers

Cohesion refers to what the class (or module) can do. Low cohesion would mean that the class does a great variety of actions - it is broad, unfocused on what it should do. High cohesion means that the class is focused on what it should be doing, i.e. only methods relating to the intention of the class.

Example of Low Cohesion:

------------------- | Staff           | ------------------- | checkEmail()    | | sendEmail()     | | emailValidate() | | PrintLetter()   | ------------------- 

Example of High Cohesion:

---------------------------- | Staff                   | ---------------------------- | -salary                 | | -emailAddr              | ---------------------------- | setSalary(newSalary)    | | getSalary()             | | setEmailAddr(newEmail)  | | getEmailAddr()          | ---------------------------- 

As for coupling, it refers to how related or dependent two classes/modules are toward each other. For low coupled classes, changing something major in one class should not affect the other. High coupling would make it difficult to change and maintain your code; since classes are closely knit together, making a change could require an entire system revamp.

Good software design has high cohesion and low coupling.

like image 176
mauris Avatar answered Oct 05 '22 16:10

mauris