Blog Datasheets Home About me Clients My work Services Contact

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:

  1. 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:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// Functional Cohesion in C
#include <stdio.h>

// This function has high functional cohesion
void calculateAndPrintSum(int a, int b) {
    int sum = a + b;
    printf("Sum: %d\n", sum);
}

int main() {
    calculateAndPrintSum(10, 20);
    return 0;
}

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.

  1. Sequential Cohesion: A module exhibits sequential cohesion when the output of one element serves as the input to another element within the module.

  2. 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.

  3. Procedural Cohesion: A module exhibits procedural cohesion when all the elements within the module are related to a specific sequence of actions or steps.

  4. Temporal Cohesion: A module exhibits temporal cohesion when the elements within the module are related by the timing of their execution.

  5. 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:

  1. Data Coupling: Modules are data coupled when they communicate by passing data between them.

  2. Stamp Coupling: Modules are stamp coupled when they communicate by passing large, composite data structures, such as arrays or structures.

  3. Control Coupling: Modules are control coupled when one module controls the behavior of another by passing control information, such as flags or status codes.

  4. External Coupling: Modules are externally coupled when they depend on a common, external entity, such as a global variable or configuration file.

  5. Common Coupling: Modules are commonly coupled when they share global data.

  6. 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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
// Code Cohesion and Coupling in C

// High Cohesion and Low Coupling
#include <stdio.h>

// Utility function with high cohesion
void printMessage(const char* message) {
    printf("%s\n", message);
}

int main() {
    printMessage("Hello, world!");
    return 0;
}

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:

1
2
3
4
5
6
7
8
9
# Code Cohesion and Coupling in Python

# High Cohesion and Low Coupling
def calculate_and_print_sum(a, b):
    # Function with high cohesion
    total = a + b
    print("Sum:", total)

calculate_and_print_sum(10, 20)  # Call to cohesive function

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!


➡️ Virtual destructor in C++


⬅️ Exploring the Builder Pattern in C++ and C


Go back to Posts.