In: Computer Science
Define what is coupling and cohesion. From the design perspective, what should be the goals with regard to coupling and cohesion? Explain with your own examples highlighting the difference between them
Coupling: Coupling is the measure of the degree of interdependence between the modules. A good software will have low coupling.
Types of Coupling:
Cohesion: Cohesion is a measure of the degree to which the elements of the module are functionally related. It is the degree to which all elements directed towards performing a single task are contained in the component. Basically, cohesion is the internal glue that keeps the module together. A good software design will have high cohesion.
Coupling and Cohesion: A View from the Software Designer Perspective:
Software development is time-consuming and expensive. Under the best circumstances, one goes from an idea to requirements, design, coding, testing, deployment, and then a maintenance phase. This is, more or less, the classic software development model. Of course, changing requirements can throw off this entire process. EHR vendors are experiencing this first-hand due to changing MU requirements. The threat of obsolescence due to changes in computing technology may also result in new requirements that alter software development cycles.
Consider how much computing has changed since 2000. In 2000, the Internet was just beginning to come into its own, and LAN-based client/server was still the next big thing. Creating a complex web application such as a content management system was expensive, and the tools to do so were not that great. Java was five years old, Rails was four years in the future, and .Net was still two years away. Look at how much things have changed in just 12 years. Mobile computing is a fact of life; anybody with a web hosting account can launch a content management system-based website; and the cloud and multi-processing are coming to the forefront of software development.
Coupling
Coupling is defined as the degree of interdependence between two or
more classes, modules, or components. Tight coupling is bad, and
loose coupling is good. This will make more sense with
an example.
Let’s say we have a clinical research application that contains a patient information collection form. On this form is a field for the SSN. Whenever a patient enrolls in a study, the form checks the SNN to see whether: 1) it is a valid SSN (i.e., fits the pattern 000-00-0000), and 2) the patient has been a subject in prior studies. In the current version of the application, when the cursor leaves the SSN field, the following procedure runs.
Procedure SSN Check
Begin
If is_Number(Left_Three_Chars)
Then
Result=0
Else
Result =1
If is_Number(Middle_Two_Chars)
Then
Result=0
Else
Result =1
If is_Number(Right_Three_Chars)
Then
Result=0
Else
Result =1
If Result=0
Then
Count = “SELECT SSN
FROM patients
WHERE SocNum=SSN”
Else
Print “This is not a valid SSN”
End
This procedure checks whether each part of the SSN is a number, and if everything is okay, it searches for the SNN in the patients table and returns 1 if the patient has been in a prior study. This type of code is easy (and tempting) to write in form-based development environments. Why is it bad? The Count query makes a direct call to the database. As a result, the SSN text field is tied directly to the database, exhibiting tight coupling. With one form, this is not a huge problem. However, if the application has multiple forms and more than one requires SSN verification, it might become a headache if the form changes, the database changes, or the query is altered. Any of these situations could prove to be costly and messy because developers would have to find every place in the application where a SSN check occurred, change the programming code, and test the final application.
Cohesion
Cohesion is defined as the degree to which all elements of a
module, class, or component work together as a functional unit.
High cohesion is good, and low cohesion is bad. The ideal situation
is one where a module, class, or component provides only one
function or, at most, a very closely related set of
functions. Looking at the above procedure, we see
that it performs two functions. It validates SSNs, and it performs
a database query. These are completely unrelated actions and, thus,
the procedure exhibits low cohesion.
Improving the Design
Moving unrelated functions into their own units (i.e., module,
class, or component) would be a good first-step in improving the
design. One solution would be creating a data access
module then placing all database queries for the entire application
in one location. Consequently, forms would no longer have to know
anything about the database–or even that it exists.
Next, we could do the same with validation. Since any real-world
application is likely to require validation for a range of form
fields (e.g., birthdates, duplicate names, vital signs, etc.) it
makes sense to have one component that handles validation as
well.
Here is the SSN Check procedure after Validation and Data Access modules have been created.
Procedure SSN Check
Begin
Result =Validate (SSN)
If Result=0
Then
Count=Database_look_up(SSN)
Else
Print “This is not a valid SSN”
End
The new version of the SSN Check procedure merely passes information to functions in the Validation and Data Access modules. This procedure is now oblivious as to how validation is performed as well as how SSN numbers are stored. Further, new functions can be added to either module without affecting the SSN Check procedure. The form is now loosely coupled to the database, and the two new components are highly cohesive, each providing a single or closely-related set of functions.