Cohesion and Coupling: How a Program can be Like a Bad Movie
Especially in object oriented programming, the concept of Cohesion is a measurement of how well defined and contained a module or subroutine of a program is. This is similar to an essay, where there is a thesis statement, analogous to the core function of the program, and each paragraph of the essay would have a topic sentence, with all other elements of the paragraph written in support of that topic sentence. In an application module, there is a core purpose to the module, its raison d’ĂȘtre, and Cohesion is the measure of how closely all elements within the module adhere to this core purpose. Cohesion is discussed in terms of high and low Cohesion. A module with low Cohesion is written to carry out a number of different tasks within the same module. Low Cohesion, while it might be expedient to write it this way if the programmer is looking to quickly complete an application that is relatively simple in design, results in a module that has little ability to be reused. A module that does many things within itself is much harder to reuse than a module that has a singular purpose, like a high cohesion module does.
Coupling is a measure of how reliant and dependant each module is on the other modules in an application. Think of it like a complex character drama, where the characters may be heavily dependant, or codependent on each other. As with Cohesion, Coupling is also measured in terms of high or low Coupling. While a program with high Coupling between modules my be seen as a program with an intricate design, in reality it is a program with modules that are only useable within that single application due to the high level of interdependence of the modules. Additionally, when any one module is changed for any reason, a high level of Coupling results in a large amount of effort needed to follow linkages between the modules to change all interdependent links. This is the source of many application bugs that come about as the result of an upgrade process.
Something to take note of: There is no accepted standard for cohesion and coupling. These are terms of academic analysis that are used in computer science theory to understand the relationships between and within modules of a program. You are not going to be able to submit a sample application and get a quantitative score for Coupling and Cohesion of an application. A manager who tells you to go back and increase Cohesion by 20% probably read about computers in an airline magazine.
To understand the concepts of Coupling and Cohesion better, think about a drama student writing a screenplay about a group of high school students that all have unique dysfunctionalities; yet together they are able to overcome a major obstacle as part of the central plot. The closer to an archetype that each character is, the more they could be re-used in other plots (though they might be less interesting they might end up being to the audience). Maybe one character is the inveterate drunken athlete, while another character would be the insecure promiscuous cheerleader. If these characters are pure to their archetype and lack complexity of character, though they might be two-dimensional, they would nonetheless be considered to have high Cohesion. The sad dynamic the drama student writes into the play where the abusive nature of the drunken athlete creates a tragic attraction for the insecure cheerleader is an example of their high level of dysfunction and interdependence. If these characters were believable because the broken places in the souls fit each other like a puzzle, then they would be exhibiting high cohesion.
No comments:
Post a Comment