Сергей
Сергей

Reputation: 908

External Coupling vs Common Coupling

I've read a lot of definitions, but still don't understand - what is the real difference between External Coupling and Common Coupling? Example, definitions from here:

External Coupling arises when two modules share an externally imposed data format, communication protocols, or device interface.

Common Coupling: Two modules are common coupled if they share information through some global data items.

As I understand, if Class A and Class B both use some outer .txt file this case corresponds External Coupling and Common Coupling. Could someone gives a clear example ?

Upvotes: 1

Views: 403

Answers (1)

Dario Scattolini
Dario Scattolini

Reputation: 105

The definition of external coupling you are quoting is quite common nowadays, but I don't know where it comes from. As far as I know, the different types of coupling were originally defined by Glen Myers in this book. These concepts were conceived for languages such as Cobol and Fortran, sometimes it is hard to grasp them because they refer to features that are very different or don't exist in modern-day languages.

According to Myers' definition, there is not much difference between external and common coupling, it seems to be just a matter of referring to individual global variables vs. to a whole shared scope containing multiple data: "External coupling is similar to common coupling except that, in external coupling, the references between modules are to individual data items, not to data structures".

Current definitions of external coupling refer not to a global scope but to things that are external to the application itself: databases, hardware, external libraries, etc. As I see it, your example of the .txt file would be external coupling in this sense. Suppose that many classes of your application read .txt files. All those classes are coupled to that specific file format. If for some reason you need to change the type of file, you will need to fix things in different parts of your code.

The way of mitigating this form of coupling is to reduce as much as possible the sites of contact of your app with the external world. Perhaps you can have only one class C read .txt files, and other classes A and B consume the output of that reading. If you need to change the source format, you can change the implementation of C only, and A and B can keep consuming its output as before (as long as C maintains its interface). You can find out more about this by researching the Adapter Pattern and Hexagonal Architecture.

Upvotes: 1

Related Questions