In the programming world, after the advent of object-oriented programming, classes and objects came into existence and became the most preferred type of programming. Though the programming language would differ (Java, C#, etc…,) the underlying concept always remained as object-oriented programming.

Here, classes, objects, methods play a crucial role in setting up the structure of the code. Initially, the three-tier architecture pattern was followed and then later moved on to MVC. However, in both these patterns, classes were created based on roles and responsibilities. Say, for example, a class could be created to do DB operations, similarly one for the business layer, one for external calls or any specific functions, and so on.

There will be a necessity for the business layer to connect to the database layer, and similarly the presentation layer to interact with the business layer. In these cases, the classes have to communicate with each other. Ultimately, the object of the classes will be used to call the exposed methods.

How the classes are connected is referred to as Coupling. There are two ways of coupling in C#. Let’s see in detail about them in this blog along with a brief understanding of what is coupling.

Table of contents

  1. What is Coupling?
  2. Tightly Coupled
  3. Loosely Coupled?
  4. Conclusion

What is Coupling?

Here is the classic definition of coupling — It is the degree to which the software components depend on each other.

Coupling in C## describes the relationship between modules in C#, how the classes and objects are connected, and also how dependent they are on each other. This also describes the flexibility and reusability part of the code.

A coupling as such is needed for successful communication between modules. But, how the modules are coupled determine the standard of the code. Let’s see in detail the two types of coupling now.

Tightly Coupled

Think of it this way — your fingers are tightly coupled with the hand, which makes it hard to change the position of the fingers and their location. This means that changing the position of fingers is close to impossible and requires redesigning of the hand. This is an example of being tightly coupled.

The classes and objects are very much interdependent, so it becomes difficult to reuse the code. When there are many dependent components, then it becomes difficult to maintain the code for enhancements.

Let us take an example, to understand things better. Go to ParTech to see this article and the example.

In the example code on the ParTech website, we have a Program class from where the code begins to execute. Here the program class has a main method and it holds the code to create an object for filereader which expects a file name. And inside the filereader class, there are two methods, one to read the data from the file and another one to save the data to a file. The save method creates an object for another class which is then used to save the data to a different file.

Since the two classes are concrete or tightly coupled, and as they know the entire implementation of the class, it is difficult to make additional changes. This is simply because the changes have to be done at a lot of places. Imagine a similar kind of coupling in a much larger application. It will be extremely tricky to maintain and support the code.

#coupling #csharp #c

 Coupling in C-Sharp
2.05 GEEK