G2Labs Grzegorz Grzęda
Mastering code cohesion and coupling for cleaner designs
May 5, 2023
Mastering Code Cohesion and Coupling for Cleaner Designs
As a programmer, you strive to write clean, maintainable, and scalable code. To achieve this, it’s essential to understand and master the concepts of code cohesion and coupling. These concepts play a significant role in determining the quality and design of your code. In this post, we’ll explore code cohesion and coupling in detail and provide extensive examples in both C and Python.
Understanding Code Cohesion
Code cohesion refers to the degree to which the elements within a module (functions, classes, etc.) are related to each other. A highly cohesive module performs a single, well-defined task, while a low cohesive module may perform multiple unrelated tasks.
Types of Cohesion
There are several types of cohesion:
- Functional Cohesion: A module exhibits functional cohesion when all the elements within the module contribute to a single, well-defined task. Let’s consider a C example:
In this example, the calculateAndPrintSum
function has high functional cohesion as it performs a single task of calculating and printing the sum of two numbers.
Sequential Cohesion: A module exhibits sequential cohesion when the output of one element serves as the input to another element within the module.
Communicational Cohesion: A module exhibits communicational cohesion when all the elements within the module operate on the same input data or share the same output data.
Procedural Cohesion: A module exhibits procedural cohesion when all the elements within the module are related to a specific sequence of actions or steps.
Temporal Cohesion: A module exhibits temporal cohesion when the elements within the module are related by the timing of their execution.
Logical Cohesion: A module exhibits logical cohesion when the elements within the module are logically related but do not form a single, well-defined task.
Improving Cohesion
To improve cohesion, it’s important to refactor the code by grouping related elements together and removing any unrelated or duplicated code. This can lead to clearer and more maintainable code.
Understanding Code Coupling
Code coupling refers to the degree of interconnectedness between modules. A high level of coupling can lead to a lack of reusability and maintainability, while low coupling promotes better modularity and easier maintenance.
Types of Coupling
There are several types of coupling:
Data Coupling: Modules are data coupled when they communicate by passing data between them.
Stamp Coupling: Modules are stamp coupled when they communicate by passing large, composite data structures, such as arrays or structures.
Control Coupling: Modules are control coupled when one module controls the behavior of another by passing control information, such as flags or status codes.
External Coupling: Modules are externally coupled when they depend on a common, external entity, such as a global variable or configuration file.
Common Coupling: Modules are commonly coupled when they share global data.
Content Coupling: Modules are content coupled when one module directly modifies or relies on the internal workings of another module.
Examples in C and Python
Let’s consider an example of code cohesion and coupling in both C and Python.
C Example:
In this C example, the printMessage
function exhibits high cohesion as it performs a single, well-defined task of printing a message. Additionally, the main
function is loosely coupled to the printMessage
function as it only calls the function without passing any data or having direct control dependency.
Python Example:
In this Python example, the calculate_and_print_sum
function exhibits high cohesion as it performs a single, well-defined task of calculating and printing the sum. Similar to the C example, the main code is loosely coupled to the function as it only makes a call without passing data or having a direct control dependency.
Summary
Understanding and mastering code cohesion and coupling are crucial for achieving cleaner designs and maintainable code. By focusing on improving cohesion and reducing coupling, you can create code that is more modular, reusable, and easier to maintain. By utilizing the examples provided in C and Python, you can apply these concepts to your own programming projects and strive for cleaner and more efficient designs.
Remember, the key to mastering code cohesion and coupling lies in continuous practice and refinement of your coding practices.
Happy coding!