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 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.
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.
Data Coupling: When modules share primitive data between them.
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.
Control Coupling: When data from one module is used to direct the structure of instruction execution in another.
External Coupling: When two modules shared externally imposed data type that is external to the software, like communication protocols and device interfaces.
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.
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.
Functional Cohesion: The execution of the task related to the problem is the only concern from all the elements inside the module.
Sequential Cohesion: The output of an element is the input of another element in a module, such as data flow between the parts.
Communicational Cohesion: Multiple elements in a module operate on the same input data and produce the same output data.
Procedural Cohesion: The activities in a module are related by sequence, otherwise they are not related.
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.
View all Courses