Cohesion and coupling are two important factors that gauge the functional interdependence of a module in software engineering. They are essential principles we should always have in mind when we build our applications.
Coupling represents the relationships between modules, and cohesion defines the relationships within them. The coupling measure defines the degree of interdependence between the modules. A lower degree of coupling is considered to be good for software; the lower the degree of coupling better the quality of the software.
Where cohesion is a measure of the degree to which the elements of the module are functionally related in the software. As the name indicates, it is the degree to which all elements directed towards performing a single task are contained in the component. Cohesion functions like the internal glue that keeps the modules together. This means that all the elements should be associated with a single task to achieve a functional good design. On the contrary to coupling, a good software design must have higher cohesion.
Cohesion refers to the degree to which the elements inside a module/components belong together, directed towards performing a single task. Where a module could be a class or a package, or even a microservice in software design. In simple words, it is the degree to which the elements of the module are functionally related.
A module with high cohesion encompasses elements tightly related to each other and united in their purpose. A module is said to have low cohesion if it contains unrelated elements, and if the module has all related elements, it is considered to have high cohesion. A good software design will have high cohesion. The concept of cohesion is closely related to the Single Responsibility Principle, where a class should only have one responsibility or should perform a single task. The modules following the SRP are likely to have high cohesion as they are meant to perform a single task in the software.
1. Functional Cohesion
Functional cohesion refers to the condition where the execution of the task related to the problem is the only concern of all the elements inside the module. In simple words, every element in the component is designed to perform a single task. They are focused on performing the necessary activities and functions. For instance, reading transaction records, cosine angle computation, seat assignment to an airline passenger, etc are nothing but functional cohesion.
2. Sequential Cohesion
When the data output of one element becomes the input for the subsequent element is called sequential cohesion. These elements are designed in such a way that they pass on the output data and create a level of involvement in software engineering. A few examples of sequential cohesion are cross-validated records and formatting of a module, raw records usage, formatting of raw records, cross-validation of fields in raw records, and return of formatted cross-validated records.
3. Communicational Cohesion
In communicational cohesion, two elements/inputs are used as inputs to generate a single output. Communicational cohesion consists of the links because which the activities affect each other and get interlinked. For example, customer details determining modules, usage of a customer account number, returning loan balance, finding the name of the customer; finding the loan balance of the customer, and the name of the customer.
4. Procedural Cohesion
The elements of procedural cohesion in the module are related to a sequence of procedural cohesion. Procedural cohesion and sequential cohesion are like each other, unlike the fact that the elements in the module of procedural cohesion are not related. Read, write, edit of the module, record use out, writing out the record, reading the record, zero padding to the numeric fields, returning records, etc, are a few examples of procedural cohesion.
5. Temporal Cohesion
Temporal cohesion is found in the modules of initialization and termination, where the elements are related by their timing involved. All the tasks must be executed in the same time span in temporal cohesion. Initialization of the module, setting the counter to zero, opening the student file, clearing the variables of error messages, initializing the array, etc, are a few examples of temporal cohesion.
6. Logical Cohesion
In logical cohesion, the elements are related to each other logically and not functionally. It consists of control coupling as one of the activities will be selected and all the code for these functions is in the same component. An example of LC is when a component reads inputs from tape, disk, and network.
7. Coincidental Cohesion
The elements in coincidental cohesion are not related to each other. The elements in the components have no conceptual relationship other than their location in the source code. Module for miscellaneous functions, customer record usage, displaying of customer records, calculation of total sales, and reading the transaction record are a few examples of coincidental cohesion.
Coupling refers to the degree of interdependence between software modules in software engineering. Two modules are said to have high coupling if they are closely connected. In simple words, coupling is not just about modules, but the connection between modules and the degree of interaction or interdependence between the two modules. If two modules contain a good amount of data, then they are highly interdependent.
The degree of which depends on their interface complexity, which is determined by the types of parameters that are interchanged while invoking the functions of the module. if the connection between components is strong, we speak about strongly coupled modules; when the connection is weak, we speak about the loosely coupled module.
1. Data Coupling
The modules are dependent on each other based on the fact that they communicate by passing only data; then the modules are said to be data coupled. Nevertheless, the components in data coupling are independent of each other and communicate through a data flow.
2. Stamp Coupling
When two modules share a compilation of an already structured and intricate set of data, it's called stamp coupling. In other words, the complete data structure/information set is passed from one module to another module. As the data and elements are pre-organized and well-placed beforehand, no junk or unused data is shared or passed between the two coupling modules which improves the efficiency of the modules.
3. Control Coupling
Control coupling is a functional flow of the software where the modules communicate by passing control information. The impact of control coupling on the software is solely based on the extension of control shared between the modules. When functionality is allowed for reusability it is seen as a positive impact on the software.
4. External Coupling
External coupling can be described as a situation where modules depend on other modules, external to the software being developed or to a particular type of hardware.
5. Common Coupling
In the software system, if any two modules share the overall constraints or share data such as global data structures, it is called a common coupling. The changes in global data get reflected back to all modules which access that data, which can be useful for evaluating the effect of the change.
6. Content Coupling
When one module in the software affects the functionality of the other module by sharing data and control flow, it is called content coupling. When a change is made to one module, the other gets automatically updated, and if they are not in sync it can lead to functional misbehaviour. Hence, software developers avoid using content coupling for software design.