This is the start of a series where I describe and demonstrate different techniques for reducing coupling in programming code. This first part starts off in setting the stage by describing the nature of coupling.
While the concept and techniques are equally applicable in other languages, the code samples will be shown in VB.NET. I will be continuing the trend of dissecting and refactoring my ongoing WADMT project – check my archives for more posts on related subjects.
What is Coupling?
Coupling is where one type depends on another type. Some amount of coupling is unavoidable, particularly with low-level types, such as
int. Tight coupling occurs when the type of a variable is not abstract, and cannot be changed without having to change other code accordingly. Loose coupling leads to code that is isolated, in the sense that changing one piece of code is less likely to affect another section of code. When coupling is tight, changing code in one location can cause a ripple effect that affects code elsewhere.
Another benefit of loose coupling is that code can be easier to set up and execute in unit tests. Loosely coupled code will make test setup much easier than code that is tightly coupled.
Achieving Loose Coupling
A common practice is to program to interfaces. Doing so ensures that code works against a contract, without knowing how that contract is implemented. That is abstraction in action.
Building on that, there are three ways to ensure loose coupling in your code, as listed below.
They are somewhat related, but each warrant a separate discussion. They will be covered in the order they are listed, which I believe is a logical progression.
I will link each item as the matching post is completed.
I’ve given a broad overview of what coupling is, and why it is desirable to keep it low if possible. The following discussion mentioned different techniques to achieve this. Those techniques will be covered in some depth in future articles.