Coupling and Cohesion: A View of Software Design from the Inside Out

by Jerome Carter on November 12, 2012 · 1 comment

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.

In previous posts, I have discussed basic software design principles that help to address the problem of change.   Concepts such as separation of concerns, single responsibility principle, don’t repeat yourself, and avoiding big design up front essentially acknowledge change as a constant that must be addressed via development practices and software architecture/design choices.    Happily, developing a web application while studying object-oriented analysis and design (OOA&D) has allowed me to see the practical value of these design principles.   Cohesion and coupling are my latest discoveries.

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.

Layering, components and/or modules help developers achieve loosely-coupled and highly-cohesive designs.  A typical application might have three layers: presentation (user interaction), business logic, and a data access layer, which controls interaction with the database.   As illustrated below, within each layer there may be one or more modules or components that handle specific tasks.

Applications that are difficult to alter and extend may be the result of software designs that ignore the principles of coupling and cohesion.  For example, when a relatively minor feature change requires a significant amount of  programming, tight coupling and low cohesion may be contributing factors.  Change is inevitable, and future-minded software designs do not simply acknowledge this fact, they embrace it.

Clinical Swift – Clinical software design with Swift!

Facebooktwitterpinterestlinkedinmail

Leave a Comment

{ 1 comment… read it below or add one }

Jerome Carter December 7, 2012 at 5:20 PM

Interesting observations. WF as a service makes sense. After all, there is no need to reinvent the wheel in every business. As I see things, HIT systems designed as fat-client front-ends to databases are a significant barrier to WF system adoption.. Modular architectures such as SMART are a step in the right direction.

Reply

Previous post:

Next post: