๐ป Software Module Cohesion & Coupling: Core Principles of Good Code
Summary
Understand the core principles of software design: Cohesion and Coupling. Learn the 7 types of cohesion and 6 types of coupling with examples. Concludes with practice questions for certification exams.
๐ก The key to good software design is to maintain High Cohesion and Low Coupling.
โ๏ธIt's important to memorize the characteristics and order of each type of cohesion and coupling.
๐ Cohesion and Coupling at a Glance
Two important metrics that determine software quality are Cohesion and Coupling.
- Cohesion: Indicates how closely related the elements within a single module are. Higher is better.
- Coupling: Represents the degree of interdependence between modules. Lower is better.
Cohesion (Higher is better) | Coupling (Worst to Best) |
---|---|
Functional | Content |
Sequential | Common |
Communicational | External |
Procedural | Control |
Temporal | Stamp |
Logical | Data |
Coincidental (Accidental) |
1. ๐งฉ Module Cohesion
Cohesion is a measure of how closely the components of a module work together to perform a single function. High cohesion increases a module's independence, making it easier to reuse and maintain.
Types of Cohesion (Worst to Best)
-
Coincidental Cohesion (Accidental)
- The components within the module have no relationship. This is the lowest level of cohesion.
- Example: A
Utils
module that contains unrelated functions likesendEmail
,calculateSum
, andprintReport
.
-
Logical Cohesion
- Functions of a similar nature or belonging to a specific category are grouped into one module.
- Example: A module that handles all types of
input
. Mouse, keyboard, and touchscreen inputs are all handled within one function using a case statement.
-
Temporal Cohesion
- Functions that must be executed at the same time are grouped together.
- Example: An
initialize
module that groups initialization codes that must run at program start (variable initialization, environment setup, opening log files).
-
Procedural Cohesion
- Functions within a module are grouped because they must be executed in a specific order.
- Example: A
process_user_data
module that callsread_data()
,validate_data()
, andsave_data()
in sequence.
-
Communicational Cohesion
- Functions that use the same input data or produce the same output data are grouped together.
- Example: A
print_student_info
module takes a 'student ID' as input and performsget_name()
,get_grades()
, andget_address()
functions to output the information.
-
Sequential Cohesion
- The output of one function serves as the input for another function in a sequential relationship.
- Example: In a
file_processing
module, the output (file content) of theread_file()
function is used as the input for thecompress_data()
function.
-
Functional Cohesion
- All functions within the module are organized to perform a single, well-defined purpose. This is the most ideal cohesion.
- Example: A
calculate_circle_area
module only performs the single function of calculating the area of a circle given its radius.
2. ๐ Module Coupling
Coupling is a measure of how much modules depend on each other. Lower coupling reduces the impact of changes in one module on others, improving the system's maintainability and scalability.
Types of Coupling (Worst to Best)
-
Content Coupling
- One module directly references or modifies the internal functions or data of another module. This is the highest level of coupling.
- Example: Module A branches into the middle of Module B's code or directly uses a local variable inside Module B.
-
Common Coupling
- Multiple modules share a global variable or a common data area.
- Example: Several modules read and modify settings from a global
GlobalConfig
object.
-
External Coupling
- Two modules share an externally defined data format, communication protocol, or specific hardware.
- Example: Two modules share a specific file format to read and write data.
-
Control Coupling
- One module passes a control signal (Flag, Switch) to control the behavior of another module.
- Example: Module A calls Module B, passing a
sort_method
flag that determines whether Module B will operate in ascending or descending order.
-
Stamp Coupling
- Two modules pass a data structure (object, record, etc.) between them, but only a portion of the data is used.
- Example: A
Student
object (containing ID, name, address, phone number) is passed to aprint_address
module, but the module only uses theaddress
field.
-
Data Coupling
- Two modules exchange only the minimum necessary data (simple parameters). This is the most ideal coupling.
- Example: An
add
module takes two numbers,a
andb
, as parameters and returns their sum.
๐ Practice Questions for Certification Exam
Problem | What is the measure of how closely the elements within a module are related to a single purpose? |
Your Answer | |
Correct Answer | Reveal Answer |
Problem | What represents the degree of interdependence between modules, where a lower value indicates a better design? |
Your Answer | |
Correct Answer | Reveal Answer |
Problem | Among the types of cohesion, which is considered the most ideal, where all functions in the module serve a single purpose? |
Your Answer | |
Correct Answer | Reveal Answer |
Problem | Among the types of coupling, which is considered the most desirable, where two modules exchange only the minimum necessary data as parameters? |
Your Answer | |
Correct Answer | Reveal Answer |
Problem | List the types of cohesion from lowest to highest, and the types of coupling from highest to lowest. |
Your Answer | |
Correct Answer | Reveal Answer |