Feeds

Let's get cohesive

Biting into sound principles

The essential guide to IT transformation

Comment It is entirely possible to write applications as a monolithic slab of undifferentiated code. Indeed, for some this appears to approach an art form, with a stream-of-consciousness code style not dissimilar to Jack Kerouac's spontaneous, amphetamine-fuelled writing style.

Caffeine is the more likely the drug of choice in programming, and the abandonment of form might be seen as rebellion against the discipline (perhaps, tyranny!) of packages, files, classes, subroutines, table normalisation, testability, teams, sustainable development, and so on. But writing code in this state is not where most of us want to be and, unlike Jack Kerouac's writing, there's little to enjoy in such code — except perhaps a shallow flush of schadenfreude when the code is somebody else's problem.

Most of us prefer to partition code into smaller units — packages, files, etc — each of which is intended to be more easily comprehensible and easier to work with than the monolithic alternative.

This is a human consideration: a concern with the developmental quality of code rather than its runtime behaviour. Although the "principle of locality" is important in the design of processor caching and virtual memory systems, it does not (yet) confuse or upset machines when programmers choose to define APIs of a thousand functions in a single header file; or when code is deployed in DLLs with apparently arbitrary contents (Graham Lea reported Microsoft doing this here). But the fact that an approach is possible doesn't make it effective or exemplary.

This is where cohesion enters the picture. Cohesion is in some sense about "things sticking together". The oft-stated principle here is to "maximise cohesion". Of course, maximising cohesion does not imply just throwing loads of stuff together to see if it sticks — down that path lies the urban sprawl of <windows.h>, which spans neighbourhoods of, at best, coincidentally related features. There has to be a reason that things are either put together or separated, and, preferably, a sound and useful reason. In other words, cohere coherently. It's this coherence that makes a modular partition easier to work with (read, write, discover, test, evolve, explain, etc).

Sound-bite principles

On its own, saying, "maximise cohesion" isn't enough to inspire or educate programmers, whatever their level of experience. This sound bite needs further clarification, some good examples to back it up; and a few counterexamples as lighthouses to warn of known trouble spots.

But useful as counterexamples are, the problem with learning by example is the disproportionate representation of counterexamples in published APIs - and not all of them are recognised as such. Keeping in mind that people are most influenced by what they see around them, a typical programmer's perception of cohesion and quality of cohesion is more likely to be shaped by the APIs he or she uses all the time, than by sound-bite principles.

The meaning of "maximise cohesion" is often clarified as "be or do one thing well". This clearly distinguishes between the relative cohesiveness of realloc and free, both found in the standard C library. free clearly does one thing well: it deallocates the result of a previous memory allocation.

By contrast, depending on how you call it, realloc behaves like an allocator (malloc)... or a deallocator (free)... or as a reallocator of memory (which is what you'd expect, given the name). This surprisingly inclusive portfolio of behaviour inspired Steve Maguire to dub it the "one function memory manager"

On the other hand, it is all too easy to weasel-word your way past a simply worded sound-bite recommendation: if the goal is to "be the Windows OS API", then <windows.h> could be said to meet this goal pretty well, and could therefore be considered "cohesive". However, it aggregates a slop bucket of features that are generally unrelated and unlikely to always be used together — COM, memory management, file I/O, DDE, multithreading, windowing, etc. There is no simple sense in which <windows.h> could be said to cohere, but the unintentional lesson that a programmer may take away from working with it is to "put everything in your application/subsystem/library in the same place".

Secure remote control for conventional and virtual desktops

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
Microsoft cries UNINSTALL in the wake of Blue Screens of Death™
Cache crash causes contained choloric calamity
Munich considers dumping Linux for ... GULP ... Windows!
Give a penguinista a hug, the Outlook's not good for open source's poster child
Time to move away from Windows 7 ... whoa, whoa, who said anything about Windows 8?
Start migrating now to avoid another XPocalypse – Gartner
You'll find Yoda at the back of every IT conference
The piss always taking is he. Bastard the.
HANA has SAP cuddling up to 'smaller partners'
Wanted: algorithm wranglers, not systems giants
prev story

Whitepapers

Endpoint data privacy in the cloud is easier than you think
Innovations in encryption and storage resolve issues of data privacy and key requirements for companies to look for in a solution.
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 8 considerations to enable and simplify mobility
In this whitepaper learn how to successfully add mobile capabilities simply and cost effectively.
Solving today's distributed Big Data backup challenges
Enable IT efficiency and allow a firm to access and reuse corporate information for competitive advantage, ultimately changing business outcomes.
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.