Feeds

Up with cohesion, down with coupling

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

Providing a secure and efficient Helpdesk

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. ®

Secure remote control for conventional and virtual desktops

More from The Register

next story
'Windows 9' LEAK: Microsoft's playing catchup with Linux
Multiple desktops and live tiles in restored Start button star in new vids
Not appy with your Chromebook? Well now it can run Android apps
Google offers beta of tricky OS-inside-OS tech
New 'Cosmos' browser surfs the net by TXT alone
No data plan? No WiFi? No worries ... except sluggish download speed
Greater dev access to iOS 8 will put us AT RISK from HACKERS
Knocking holes in Apple's walled garden could backfire, says securo-chap
NHS grows a NoSQL backbone and rips out its Oracle Spine
Open source? In the government? Ha ha! What, wait ...?
Google extends app refund window to two hours
You now have 120 minutes to finish that game instead of 15
Intel: Hey, enterprises, drop everything and DO HADOOP
Big Data analytics projected to run on more servers than any other app
prev story

Whitepapers

Secure remote control for conventional and virtual desktops
Balancing user privacy and privileged access, in accordance with compliance frameworks and legislation. Evaluating any potential remote control choice.
Saudi Petroleum chooses Tegile storage solution
A storage solution that addresses company growth and performance for business-critical applications of caseware archive and search along with other key operational systems.
High Performance for All
While HPC is not new, it has traditionally been seen as a specialist area – is it now geared up to meet more mainstream requirements?
Security for virtualized datacentres
Legacy security solutions are inefficient due to the architectural differences between physical and virtual environments.
Providing a secure and efficient Helpdesk
A single remote control platform for user support is be key to providing an efficient helpdesk. Retain full control over the way in which screen and keystroke data is transmitted.