Coding Best Practice - Loose Coupling

In a programmer's life "CHANGE is INEVITABLE".  This practice shall help you a lot if you want to reuse, maintain an existing code or modify/update an existing code.

Let's try to understand with an example. We have a simple program to access a student database as per requirement of  school (i.e. class School).  Here MySQL databased is used. Let's says School usually reads few information 10 times to perform certain operation and write some information 2 times on that database using  MySQL read and write APIs.

Now management has come to the decision that MySQL is not affordable, switch to oracle now.  Programmer shall make an effort to change code as per new requirement and he/she will make change at-least (10 read & 2 write) places in the code. This is called tightly coupled code. There are chances that existing logic may be affected during modification of code.

TIGHTHLY-COUPLED CODE:- Directly calling 3rd party Library or API,[any other class] from our main-logic code section

If code would have been loosely-coupled with the help of Wrapper Class/Function  then it would be easier, faster and economical to switch to oracle. Because code should be changed at 2-Places 1-Read API and 1 Write API

Loose-Coupling provides flexibility to a Class [Layer of code] that if there is change in other class it should not impact much. 

LOOSELY-COUPLED CODE:- No direct Call of 3rd party Library or API,[any other class] from our main-logic code section. There are various ways to implement it some to them are shown below

Loose and Tight Coupling of code

Similarly, if we have one client-server application that is interacting over UPD and later we came with requirement that transport should be the TCP or HTTP then loosely coupled code is easier to manage.

Replace UDP with TCP in loosely coupled code.

Where loosely coupled code can be helpful?

Following are some of the places where one should use loose coupling.

  • Once calling 3rd-Party APIs.
  • While implementing any IPC mechanism in your project.
  • Calling method of any other module of the same project.
  • Calling transport layer (HTTP, UDP, SCTP etc.) related APIs in your project.
  • Once trying to call database related APIs.
  • If you have parsing of API such as parsing JSON, XML or any text format.
  • and many more ....
Let check with example Code.

Tightly Coupled Code

Loosely Coupled Code

class Client

{
public:
    connectToServer()
    {
        //Create Linux UDP Socket
    }
    sendFirstMessage()
    {
    //Send Message Using Linux UDP APIs
    }
    sendHelloMessage()
    {
    //Send Message Using Linux UDP APIs
    }
    sendReportMessage()
    {
    //Send Message Using Linux UDP APIs
    }
    sendFeedBackMessage()
    {
    //Send Message Using Linux UDP APIs
    }
};
class Client
{
public:
    connectToServer()
    {
        createSocket()
    }
    sendFirstMessage()
    {
        sendMessageOnSocket()
    }
    sendHelloMessage()
    {
        sendMessageOnSocket()
    }
    sendReportMessage()
    {
        sendMessageOnSocket()
    }
    sendFeedBackMessage()
    {
        sendMessageOnSocket()
    }
    createSocket()
    {
       //Create Linux UDP Socket
    }
    sendMessageOnSocket()
    {
    //Send Message Using Linux UDP APIs
    }

};

Explanation:

  • If we want to change transport to TCP then we are supposed to touch/modify every existing function.
  • More function we touch/modify more effort is required, more chances of inducing new bugs.

 

Explanation:

  • If we want to change transport to TCP then we are supposed to only two function.
  • Less effort is required and less chance of introducing a new bug.

Your Comments /Suggestions & Questions are always welcome. 

We would like to help you with best of our knowledge.

So feel free to put Questions

No comments:

Post a Comment