Jump to content

Recommended Posts

Cohesion is the degree in which different parts form a meaningful unit. 'Parts' can refer to pieces of software on any level, from components, to packages, subpackages, classes, methods, or even blocks of code. It's not really contradictory to coupling. High cohesion can promote loose coupling, because high cohesion usually equals less (or more related) responsibilities for that part.

 

High cohesion and loose coupling is what you're usually after. Hope that helps a little.

Let's see if I understand this. An example would be that when I use a class, code snippet, library, etc from someone else, i'd have to make sure I choose something that fits my needs as close as possible. Eg not choosing something with extra functionality or features I don't need for my program.

 

Is this correct?

No, not really. Cohesion is about centralizing responsibilities. One common way to accomplish this abstraction. For a familiar example, imagine some CMS with data access logic scattered throughout different business objects. By abstracting out the SQL and whatever else there is to abstract concerning data access, I centralize the responsibility in the data access layer, reducing coupling between the different business components, and increasing cohesion. There is high cohesion in the newly created data access component, as well as in the business components, who now have reduced responsibilities.

 

Cohesion and loose coupling are goals, abstraction is one of the means.

Ok let me try again. Strong cohesion is about handling clearly defined responsibilities and delegating what's not part of that responsibility. Or: if it's not part of the responsibility, ideally some other part should be responsible for handling that. The strategy pattern as an example of how one might implement this.

That's a lot more accurate. Though I think it's important to understand that this doesn't just concern classes or types, like something like implementing a Strategy addresses. It also applies on a higher level (think MVC), a lower level (think Template Method), and can even be a goal within a relatively simple block of code. Abstraction, delegation, the SRP (and OCP, DRY and the like), they're just handles for approaches to achieve higher cohesion and looser coupling. Many, many patterns (if not most) are common practices to achieve this, with the ultimate goal of increased maintainability and extensibility in mind.

 

It's very broad concept, that applies across layers as well as methodologies; it's not at all exclusive to OO.

  • 6 months later...

That's a lot more accurate. Though I think it's important to understand that this doesn't just concern classes or types, like something like implementing a Strategy addresses. It also applies on a higher level (think MVC), a lower level (think Template Method), and can even be a goal within a relatively simple block of code. Abstraction, delegation, the SRP (and OCP, DRY and the like), they're just handles for approaches to achieve higher cohesion and looser coupling. Many, many patterns (if not most) are common practices to achieve this, with the ultimate goal of increased maintainability and extensibility in mind.

 

It's very broad concept, that applies across layers as well as methodologies; it's not at all exclusive to OO.

 

Agreed.  I've done a mathematical analysis of coupling within the Zend Framework.  It shows the difference in coupling at class level and at component level.  Anyone interested can check it out at http://codecaine.co.za/posts/analysis-of-coupling-within-the-zend-framework.

I'll admit, I didn't read the whole thing (interesting thing you're doing though), but the first thing that sprung to mind is: you can't calculate coupling. Coupling is the degree in which the change in one component requires a change in another. There isn't a mathematical formula in the world that can calculate that, it's too abstract.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.