Trusted answers to developer questions

What are the different types of coupling?

Get Started With Machine Learning

Learn the fundamentals of Machine Learning with this free course. Future-proof your career by adding ML skills to your toolkit — or prepare to land a job in AI or Data Science.

Coupling is the degree of interaction between two classes or functions that violates the principle of hiding information. The lower the coupling, the more modular a program is, which means that less code has to be changed when the program’s functionality is altered later on. However, coupling cannot be completely eliminated; it​ can only be minimized.

Types of coupling

1. Content coupling: Is when one class modifies the content of another class. For example, in C++, friend classes can access each other’s private members. However, the benefit of friend classes is that they can increase the performance of a large-scale program by removing one layer of interaction.

2. Common coupling: Is when two classes access the same shared data (e.g., a global variable).

3. Control coupling: When one function controls the flow of another function.

bool foo(int x){
    if (x == 0)
        return false;
    else
        return true;
}

void bar(){
    // Calling foo() by passing a value which controls its flow:
    foo(1);
}

4. Routine call coupling: When one function calls another function without passing any data as arguments. Almost every program has a function that is calling another function(s), which is why it is impossible to completely remove coupling.

5. Data coupling: When one function passes data to another function that may be used for calculation. Although this is quite common, the passed data must have the same interpretation across the two functions.

6. Type-use coupling: When a data member of class B is of class A type.

7. Stamp coupling: When the signature of one of Class B's functions has class A as its argument or return type.

class A{
   // Code for class A.
};

class B{
    // Data member of class A type: Type-use coupling
    A var;

    // Argument of type A: Stamp coupling
    void calculate(A data){
        // Do something.
    }
};

8. Import coupling: When a library is imported for use inside a program. For example, when #include and import statements are used in C++ and Java, respectively.

9. External: When communicating with an external system.

RELATED TAGS

analysis and design
software engineering
classes
objected oriented
modularity
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?