Let's get cohesive

Biting into sound principles

Top three mobile application threats

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

Maximizing your infrastructure through virtualization

More from The Register

next story
HIDDEN packet sniffer spy tech in MILLIONS of iPhones, iPads – expert
Don't panic though – Apple's backdoor is not wide open to all, guru tells us
NO MORE ALL CAPS and other pleasures of Visual Studio 14
Unpicking a packed preview that breaks down ASP.NET
Captain Kirk sets phaser to SLAUGHTER after trying new Facebook app
William Shatner less-than-impressed by Zuck's celebrity-only app
Mozilla fixes CRITICAL security holes in Firefox, urges v31 upgrade
Misc memory hazards 'could be exploited' - and guess what, one's a Javascript vuln
Apple fanbois SCREAM as update BRICKS their Macbook Airs
Ragegasm spills over as firmware upgrade kills machines
Cheer up, Nokia fans. It can start making mobes again in 18 months
The real winner of the Nokia sale is *drumroll* ... Nokia
EU dons gloves, pokes Google's deals with Android mobe makers
El Reg cops a squint at investigatory letters
Chrome browser has been DRAINING PC batteries for YEARS
Google is only now fixing ancient, energy-sapping bug
prev story


Top three mobile application threats
Prevent sensitive data leakage over insecure channels or stolen mobile devices.
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.
Application security programs and practises
Follow a few strategies and your organization can gain the full benefits of open source and the cloud without compromising the security of your applications.
The Essential Guide to IT Transformation
ServiceNow discusses three IT transformations that can help CIO's automate IT services to transform IT and the enterprise.