Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

communitycreator

Software Engineering Principle — Coupling and Cohesion

Mohammad Quanit

Overview

In this shot, we’ll be discussing the importance of coupling & cohesion in software engineering. But before moving into this topic, we should know what modularization is so we can grasp the concept of coupling easily.

Modularization

Modularization is the process of breaking a software into multiple small modules, where each module works independently. The main advantage of modularization is that it makes it easy to understand the software, makes it reusable, and it can be tested easily.

What is coupling in software engineering?

Coupling is the inter-dependency or degree of relationship between multiple modules/packages/components. Coupling is also called Inter-module Binding.

Multiple modules/packages/components that are highly coupled are strongly dependent on each other. Multiple modules/packages/components that are loosely coupled are not or somehow dependent on each other.

Note: Good software is always loosely coupled so it is considered as best practice to make your modules/packages/components loosely coupled or interdependent so that they can be tested and managed to maintain easily.

The higher the number of calls between modules, the more error prone it will be. 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.

Coupling

Types of coupling

  1. Data Coupling: When modules share primitive data between them.

  2. Stamp Coupling: When modules share composite or structural data between them. They must be a non-global data structure. For example, passing an object or a structure variable in react components.

  3. Control Coupling: When data from one module is used to direct the structure of instruction execution in another.

  4. External Coupling: When two modules shared externally imposed data type that is external to the software, like communication protocols and device interfaces.

  5. Common Coupling: When two modules share the same global data & dependent on them, such as state management in JavaScript frameworks.

  6. Content Coupling: When two modules share code and can modify the data of another module, which is a poor way of coupling and should be avoided.

There is another concept that is used with coupling known as cohesion.

What is cohesion in Software engineering?

Cohesion refers to what a module can do, internally. It is also called Intra-Module binding as it measures the strength of the relationship of functionalities inside a module/package/component. Cohesion should always be high means that a module/package/component is focused on what it should be doing, such as only methods relating to the intention of the class.

Example of low cohesion:

-------------------
|Add To Cart module|
-------------------
| login()              |
| selectProduct()      |
| getShippingDetails() |
| PrintReceipt()       |
-------------------

Example of high cohesion:

----------------------------
| Add To Cart module      |
----------------------------
| selectProduct()         |
| getShippingDetails()    |
| calculatePrice()  |     |
----------------------------

In the above example, we see that the login function is not relatable to add to cart module, which is low cohesion which is considered as bad in software engineering.

Types of cohesion

  1. Functional Cohesion: The execution of the task related to the problem is the only concern from all the elements inside the module.

  2. Sequential Cohesion: The output of an element is the input of another element in a module, such as data flow between the parts.

  3. Communicational Cohesion: Multiple elements in a module operate on the same input data and produce the same output data.

  4. Procedural Cohesion: The activities in a module are related by sequence, otherwise they are not related.

  5. Coincidental Cohesion: The activities with a meaningless relationships with one another are contributed by the elements in the module.

Note: Good Software has always low coupling with high cohesion.

RELATED TAGS

communitycreator
RELATED COURSES

View all Courses

Keep Exploring