What does 'low in coupling and high in cohesion' mean
Asked Answered
M

18

252

I have problems understanding the statement low in coupling and high in cohesion. I have googled and read a lot about this, but still finding it hard to understand.

To what I understand is High cohesion means, that we should have classes that are specialized to perform a particular function. Hope this is correct? Like a credit card validation class, which is specialized to validate credit cards only.

And still don't understand what low Coupling means?

Michail answered 22/12, 2012 at 6:57 Comment(3)
For a more detailed explanation, you can prefer to the answer from this post Cohesion & CouplingStiff
This answer is certainly better and concise then the ones given here.Jannajannel
In fact, this is a duplicate of those. Answer by Infinity is the only non-duplicate not mentioned so far here.Nematode
C
359

What I believe is this:

Cohesion refers to the degree to which the elements of a module/class belong together, it is suggested that the related code should be close to each other, so we should strive for high cohesion and bind all related code together as close as possible. It has to do with the elements within the module/class.

Coupling refers to the degree to which the different modules/classes depend on each other, it is suggested that all modules should be independent as far as possible, that's why low coupling. It has to do with the elements among different modules/classes.

To visualize the whole picture will be helpful:

enter image description here

The screenshot was taken from Coursera.

Columbus answered 22/12, 2012 at 7:32 Comment(6)
Our professor says "High cohesion is about making sure module doesn't do many things, it is meant to do only one particular thing".Jannajannel
From what I believe it is more like "making sure one module does a thing, not many modules do the same thing", by this you can ensure that only a single module specifies the behaviour, so the overall behaviour for a thing is cohesive.Parsons
@Jannajannel I think your comment muddles things. Your professor is confusing high cohesion with "Single responsibility principle". High cohesion means to keep similar and related things together. You can have high cohesion in an object or a service which is made up of many functions.Lian
That diagram means literally nothing.Grandaunt
In term of micro-service architecture high cohesion means that strongly related things should be kept together in one micro-service and loose coupling means that a micro-service itself should be fine-grained to work in bounded context i.e. do one thing independently.Arabia
@Grandaunt Now listen to what the professor above have said: high cohesion is required within a module with single responsibility. Low coupling is needed between such modules. To summarize the above, you should not allow high coupling in code that does different tasks. For example, you are creating a service with an API (or a set of APIs). It performs different tasks and should have a set of low coupled modules behind it. But each such module is allowed to have high cohesion inside.Recti
M
72

Cohesion in software engineering, as in real life, is how much the elements consisting a whole(in our case let's say a class) can be said that they actually belong together. Thus, it is a measure of how strongly related each piece of functionality expressed by the source code of a software module is.

One way of looking at cohesion in terms of OO is if the methods in the class are using any of the private attributes.

Now the discussion is bigger than this but High Cohesion (or the cohesion's best type - the functional cohesion) is when parts of a module are grouped because they all contribute to a single well-defined task of the module.

Coupling in simple words, is how much one component (again, imagine a class, although not necessarily) knows about the inner workings or inner elements of another one, i.e. how much knowledge it has of the other component.

Loose coupling is a method of interconnecting the components in a system or network so that those components, depend on each other to the least extent practically possible…

I wrote a blog post about this. It discusses all this in much detail, with examples etc. It also explains the benefits of why you should follow these principles.

Mousy answered 8/4, 2015 at 19:32 Comment(0)
S
46

In software design high cohesion means that class should do one thing and one thing very well. High cohesion is closely related to Single responsibility principle.

Low coupling suggest that class should have least possible dependencies. Also, dependencies that must exist should be weak dependencies - prefer dependency on interface rather than dependency on concrete class, or prefer composition over inheritance .

High Cohesion and low coupling give us better designed code that is easier to maintain.

Sheers answered 10/2, 2016 at 8:9 Comment(1)
You missed Dependency Injection. It is closely related to low coupling to ensure a class has the least/no dependencies.Caveator
E
34

Short and clear answer

  • High cohesion: Elements within one class/module should functionally belong together and do one particular thing.
  • Loose coupling: Among different classes/modules should be minimal dependency.
Exhibitionism answered 24/4, 2018 at 11:56 Comment(0)
A
10

When I was reading about a microservice. I came across the following things:

enter image description here

Cohesion is a measure of the number of relationships that parts of a component have with each other. High cohesion means that all of the parts that are needed to deliver the component's functionality are included in the component

Coupling is a measure of the number of relationships that one component has with other components in the system. Low coupling means that components do not have many relationships with other components

Appellate answered 25/10, 2022 at 5:10 Comment(0)
S
9

Low coupling is in the context of two or many modules. If a change in one module results in many changes in other module then they are said to be highly coupled. This is where interface based programming helps. Any change within the module will not impact the other module as the interface (the mean of interaction ) between them has not changed.

High cohesion- Put the similar things together. So a class should have method or behaviors to do related job. Just to give an exaggerated bad example: An implementation of List interface should not have operation related to String. String class should have methods, fields which is relevant for String and similarly, the implementation of List should have corresponding things.

Hope that helps.

Stunk answered 7/5, 2014 at 12:42 Comment(0)
B
8

Cohesion - how closely related everything is with one another.
Coupling - how everything is connected to one another.

Let's take an example - We want to design a self-driving car.

(1) We need the motor to run properly.

(2) We need the car to drive on its own.

All of the classes and functions in (1) starting the motor and making it run work great together, but do not help the car steer. So we place those classes behind an Engine Controller.

All of the classes and functions in (2) work great to make the car steer, accelerate and brake. They do not help the car start or send gasoline to the pistons. So we place these classes behind its own Driving Controller.

These controllers are used to communicate with all of the classes and functions that are available. The controllers then communicate only with each other. This means I can't call a function in the piston class from the gas pedal class to make the car go faster.

The pedal class has to ask the Driving Controller to talk to the Engine Controller which then tells the piston class to go faster. This allows us programmers to be able to find issues and allows us to combine large programs without worrying. This is because the code was all working behind the controller.

Bibbie answered 30/4, 2018 at 3:25 Comment(0)
T
7

Metaphorically, if your cat barks, it has poor cohesion, and if your dog needs a cat by his side to bark, it is highly coupled.

"Dogs bark and cats purr, if they barf your pull request will be rejected"

Townsfolk answered 3/9, 2021 at 15:53 Comment(1)
it should be the best answer!Dilworth
E
6

Take the example of an old PC motherboard.

  • Mouse had its own PS/2 port.
  • Printer had its own Printer port.
  • Monitor had its own VGA port.

This meant that a particular port was meant only for a particular device, and for none other.

This is Strong / High Coupling

Since a mouse is used only for operating the cursor and related functionalities, a keyboard for typing keys, etc i.e they perform only the task they are intended for, this is High Cohesion

If a mouse had a few buttons 'a' 'b' 'c' to enter, then it is doing more than what it should, since a keyboard is already performing them, this is Low Cohesion

The outdated usage of exclusive ports was thankfully replaced by a standard (interface) we call USB. This is Loose / Low Coupling

Looking at these physical attributes, it looks obvious that this is how it is supposed to be, but while writing software it is very easy to lose track of what functionality is to be put where, etc. and hence as a reminder, in everything in life, always stick to:

'High Cohesion and Loose Coupling'

Extradition answered 30/4, 2021 at 9:31 Comment(0)
T
5

Long story short, low coupling as I understood it meant components can be swapped out without affecting the proper functioning of a system. Basicaly modulize your system into functioning components that can be updated individually without breaking the system

Thoracoplasty answered 22/12, 2012 at 7:3 Comment(1)
Isn't that the same as High Cohesion ?Michail
C
4

Do you have a smart phone? Is there one big app or lots of little ones? Does one app reply upon another? Can you use one app while installing, updating, and/or uninstalling another? That each app is self-contained is high cohesion. That each app is independent of the others is low coupling. DevOps favours this architecture because it means you can do discrete continuous deployment without disrupting the system entire.

Cramoisy answered 29/6, 2017 at 20:22 Comment(2)
>Does one app reply upon another? . . well yes, some do. Many apps use the Camera app, by workout app feeds heart and workout data to Health and Activities. I can share a snippet from one app to many others. My alarm app knows the time and play a track from the Music app...Lian
@MaxHodges that thing (low cohesion and high coupling) is depreciated and should be minimized to least extent possible. In some cases, as you mentioned. This cannot be completely removed.Bein
B
2

Inheritance or generalization is an example of high coupling (i.e. high interdependence). What I meant by this is that in inheritance often the parent class defines base functionalities that is used by its child class and change in methods of parent class directly impact its child classes. Hence we can say that there is a greater degree of interdependence between classes.

Realization or using interface is an example of high cohesion (i.e. low interdependence). What this means is that an interface put forward a contract for any class that implements it but each class has the right to implement methods declared in interface in its own way and changes in method declared in one class doesn't affect any other class.

Brandnew answered 23/5, 2017 at 16:37 Comment(0)
K
2

Low Coupling:-- Will keep it very simple. If you change your module how does it impact other modules.

Example:- If your service API is exposed as JAR, any change to method signature will break calling API (High/Tight coupling).

If your module and other module communicate via async messages. As long as you get messages, your method change signature will be local to your module (Low coupling).

Off-course if there is change in message format, calling client will need to make some change.

Koffman answered 19/2, 2020 at 6:35 Comment(0)
C
1

Low Coupling and High Cohesion is a recommended phenomenon.

Coupling means to what extent various modules are interdependent and how the other modules are affected on changing some/considerable functionality of a module. Low coupling is emphasized as the dependency has to be maintained low so that very least/negligible changes are made to other modules.

Crake answered 22/1, 2014 at 10:58 Comment(0)
B
1

An example might be helpful. Imagine a system which generates data and puts it into a data store, either a file on disk or a database.

High Cohesion can be achieved by separate the data store code from the data production code. (and in fact separating the disk storage from the database storage).

Low Coupling can be achieved by making sure that the data production doesn't have any unnecessary knowledge of the data store (e.g. doesn't ask the data store about filenames or db connections).

Birk answered 16/11, 2016 at 12:35 Comment(0)
C
1

Here is an answer from a bit of an abstract, graph theoretic angle:

Let's simplify the problem by only looking at (directed) dependency graphs between stateful objects.

An extremely simple answer can be illustrated by considering two limiting cases of dependency graphs:

The 1st limiting case: a cluster graphs .

A cluster graph is the most perfect realisation of a high cohesion and low coupling (given a set of cluster sizes) dependency graph.

The dependence between clusters is maximal (fully connected), and inter cluster dependence is minimal (zero).

This is an abstract illustration of the answer in one of the limiting cases.

The 2nd limiting case is a fully connected graph, where everything depends on everything.

Reality is somewhere in between, the closer to the cluster graph the better, in my humble understanding.

From another point of view: when looking at a directed dependency graph, ideally it should be acyclic, if not then cycles form the smallest clusters/components.

One step up/down the hierarchy corresponds to "one instance" of loose coupling, tight cohesion in a software but it is possible to view this loose coupling/tight cohesion principle as a repeating phenomena at different depths of an acyclic directed graph (or on one of its spanning tree's).

Such decomposition of a system into a hierarchy helps to beat exponential complexity (say each cluster has 10 elements). Then at 6 layers it's already 1 million objects:

10 clusters form 1 supercluster, 10 superclusters form 1 hypercluster and so on ... without the concept of tight cohesion, loose coupling, such a hierarchical architecture would not be possible.

So this might be the real importance of the story and not just the high cohesion low coupling within two layers only. The real importance becomes clear when considering higher level abstractions and their interactions.

Camelot answered 22/8, 2018 at 17:31 Comment(0)
N
1

I think you have red so many definitions but in the case you still have doubts or In case you are new to programming and want to go deep into this then I will suggest you to watch this video, https://youtu.be/HpJTGW9AwX0 It's just reference to get more info about polymorphism... Hope you get better understanding with this

Nansen answered 17/2, 2020 at 7:33 Comment(0)
N
0

High Cohesion: Qualitative degree to which a module focuses on one thing

low coupling: qualitative degree to which a module is connected to other modules and the outside world.

ideally, a good module should focus on one specific thing and do it well while it should be shouldn't be too dependent on other modules.

Nostrum answered 18/5, 2023 at 17:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.