Feeds

Let's get cohesive

Biting into sound principles

Choosing a cloud hosting partner with confidence

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

Business security measures using SSL

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
SUSE Linux owner Attachmate gobbled by Micro Focus for $2.3bn
Merger will lead to mainframe and COBOL powerhouse
iOS 8 release: WebGL now runs everywhere. Hurrah for 3D graphics!
HTML 5's pretty neat ... when your browser supports it
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

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.
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.
Security and trust: The backbone of doing business over the internet
Explores the current state of website security and the contributions Symantec is making to help organizations protect critical data and build trust with customers.
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.
Security for virtualized datacentres
Legacy security solutions are inefficient due to the architectural differences between physical and virtual environments.