Feeds

Up with cohesion, down with coupling

We all love cohesion and hate coupling, don't we?

Secure remote control for conventional and virtual desktops

The point about the header file is that it made a conceptual coupling that existed within the program explicit and, therefore, visible. Brushing it under the carpet did not reduce the coupling, it just made it harder to see and deal with. So, taken by itself, the header file was highlighting the problem rather than necessarily being the problem. That said, given some of the other design decisions the team took and the code I saw, perhaps the real problem was that they had access to keyboards. But I digress.

Now, armed with a reasonable and reasoned understanding of what's meant by maximise cohesion and minimise coupling, what is the relationship between coupling and cohesion?

The received wisdom is quite simple: when one goes up, the other goes down; tight coupling and weak cohesion go together, as do loose coupling and strong cohesion. Most of the time that correlation holds, but there are a number of well-known design examples that are not quite as straightforward.

Consider, first of all, the Composite design pattern. This design allows code to treat individual objects and groups of objects the same way through a common interface. Instead of having duplicate code where individuals and groups are handled in similar ways, code only has to be written once. Instead of explicit selection based on a runtime type check, such as instanceof in Java or dynamic cast in C++, polymorphic dispatch determines the right code to execute. So, instead of coupling to the specific types of individual objects and of groups of objects, usage code is coupled only to the interface.

From the perspective of usage code, lower coupling is typically a consequence of using a Composite. What about cohesion? Here the case is not so clear cut. Looking at the common interface implemented by classes for individual objects classes and for groups it appears that the common operations are not always, well, common.

In most Composite implementations the common interface provides a way to traverse groups of objects, whether using Iterator objects or via callbacks from Enumeration Methods, optionally expressed in terms of Visitor to separate callbacks on different object types.

This is all very well for groups of objects, but what does it mean to traverse an individual object? Not much. You have to invent a behaviour, such as doing nothing, returning null, returning a Null Object, throwing an exception, or whatever is appropriate for the language and iteration style of choice. In other words, to achieve lower coupling, some cohesion has also been traded in.

And speaking of iteration, the common-or-garden Iterator pattern offers another example of a trade-off between coupling and cohesion.

A collection that holds its elements but doesn't allow you to traverse them is unlikely to prove popular. There are many ways to offer traversal, but if the caller needs to be able to know the position of elements in some way there are essentially only three general designs that keep the collection's internal representation hidden from the caller.

First, it is possible to use an index into the collection. The advantage of this is that it appears simple and there is no coupling to the collection's data structure. The main disadvantage is that it is only practical if indexing is a constant-time operation, which it won't be for linked data structures.

The second approach is to internalise a cursor within the collection. This cursor can be moved efficiently to and fro by the caller without revealing the internal data structure. However, it can't support more than one pass at a time — which fails in interesting ways for nested traversals — and for collections that are supposed to be read-only you have the additional question of whether or not you consider the cursor to be part of the collection's state (it turns out that you have problems either way).

And the third option is to introduce an Iterator object.

An Iterator object represents a clean separation of concerns, resulting in two kinds of objects, each with clear responsibilities: objects whose responsibility is to iterate, with an interface to match, and objects whose responsibility is to collect, with an interface to match. The trade-off here is that although the collection's data structure is not revealed to the world, it is revealed to the Iterator, which is closely coupled to it. ®

The essential guide to IT transformation

More from The Register

next story
The Return of BSOD: Does ANYONE trust Microsoft patches?
Sysadmins, you're either fighting fires or seen as incompetents now
Munich considers dumping Linux for ... GULP ... Windows!
Give a penguinista a hug, the Outlook's not good for open source's poster child
Intel's Raspberry Pi rival Galileo can now run Windows
Behold the Internet of Things. Wintel Things
Linux Foundation says many Linux admins and engineers are certifiable
Floats exam program to help IT employers lock up talent
Microsoft cries UNINSTALL in the wake of Blue Screens of Death™
Cache crash causes contained choloric calamity
Eat up Martha! Microsoft slings handwriting recog into OneNote on Android
Freehand input on non-Windows kit for the first time
prev story

Whitepapers

Implementing global e-invoicing with guaranteed legal certainty
Explaining the role local tax compliance plays in successful supply chain management and e-business and how leading global brands are addressing this.
Top 10 endpoint backup mistakes
Avoid the ten endpoint backup mistakes to ensure that your critical corporate data is protected and end user productivity is improved.
Top 8 considerations to enable and simplify mobility
In this whitepaper learn how to successfully add mobile capabilities simply and cost effectively.
Rethinking backup and recovery in the modern data center
Combining intelligence, operational analytics, and automation to enable efficient, data-driven IT organizations using the HP ABR approach.
Reg Reader Research: SaaS based Email and Office Productivity Tools
Read this Reg reader report which provides advice and guidance for SMBs towards the use of SaaS based email and Office productivity tools.