What does 'low in coupling and high in cohesion' mean

200,680

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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.

Solution 5

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.

Share:
200,680

Related videos on Youtube

user1315906
Author by

user1315906

Updated on July 08, 2022

Comments

  • user1315906
    user1315906 almost 2 years

    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?

    • Infinity
      Infinity over 11 years
      For a more detailed explanation, you can prefer to the answer from this post Cohesion & Coupling
    • Lokesh
      Lokesh about 7 years
      This answer is certainly better and concise then the ones given here.
    • cellepo
      cellepo almost 4 years
      In fact, this is a duplicate of those. Answer by Infinity is the only non-duplicate not mentioned so far here.
  • user1315906
    user1315906 over 11 years
    Isn't that the same as High Cohesion ?
  • BugHunterUK
    BugHunterUK over 8 years
    You missed Dependency Injection. It is closely related to low coupling to ensure a class has the least/no dependencies.
  • Lokesh
    Lokesh about 7 years
    Our professor says "High cohesion is about making sure module doesn't do many things, it is meant to do only one particular thing".
  • sschrass
    sschrass almost 7 years
    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.
  • Max Hodges
    Max Hodges over 6 years
    @Lokesh 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.
  • Max Hodges
    Max Hodges over 6 years
    >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...
  • M. Habib
    M. Habib about 6 years
    @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.
  • Liam
    Liam over 5 years
    That diagram means literally nothing.
  • sactiw
    sactiw over 5 years
    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.