Click here to monitor SSC

Tony Davis

Simple-Talk Editor
News, views and good brews

What is "maintainable code"?

Published Tuesday, April 14, 2009 5:06 PM

"Maintainable code" does not mean the same thing to a DBA as it does to a developer. Production Support staff will want something altogether different from either. These differing perspectives on maintainability have always been a cause of immense grief to IT projects.

 

To a developer, maintainable code simply means "code that is easy to modify or extend". At the heart of maintainability is carefully constructed code that is easy to read; code that is easy to dissect in order to locate the particular component relating to a given change request; code that is then easy to modify without the risk of starting a chain reaction of breakages in dependant modules.

 

Aside from "common sense" principles such as keeping code as simple and terse as possible, creating effective documentation, using source control, and so on, developers aim to ensure a clean separation of functionality in their code, with high cohesion and low coupling. They will generally employ a range of tools and techniques, such as MVC patterns, object-relational mapping tools, and test-driven development, which offer the promise of more maintainable code.

 

Unfortunately, such tools mean little to the DBA. They will find little solace in the idea that the code is elegant and object-oriented; it matters little to them if there are few inter-class dependencies. They are not enamoured by object-relational mapping. Maintainable code to a DBA means code that is written to a defined interface, based on routines, so that access rights can be restricted for security, and the SQL can be optimised independently of the application code. It is code that is instrumented and easy to troubleshoot, where any anomalous conditions within the application are logged, even if they don't cause errors.

 

Support staff will be largely unimpressed by either definition of maintainability. For them, a maintainable application provides an easily-accessible list of all possible 'exceptions', along with a description of what to do to fix the problem. They want an application that alerts them when there is anything that needs to be corrected, and gives them a step-by-step procedure to do so, or indicates who they should contact. They just want something they can keep upright, no matter what elegant innards lie behind and beyond the 'application unavailable: please contact your administrator' message.

 

What is the way out of this conundrum? Is it possible to reach a "consensus" definition of maintainability, or do we simply need to maintain distinct definitions and make it clearer which "type" of maintainability we're referring to?

 

As always, we'd all love to hear what you think. The best contribution to the debate, added as a comment to the editorial blog, will receive a $50 Amazon voucher.

 

Cheers,

 

Tony.

Comments

 

Dave.Poole said:

OK, I've had a foot in all three camps and apart from a really unusual form of groin strain I've come to the conclussion that maintainable code is code where it's intent, context and purpose is clearly communicated.

In my place of work we have a development standard for code comments where the above three criteria have to be incorporated into the comments or extended properties of the database objects.  This allows us to use a documentation tool to scavenge these properties and publish them to a developer intranet with a "morons couldn't forget it" url.

In effect we are producing an MSDN type source of knowledge for our DB code.
We use Innovasys DocumentX but Apex SQLDoc or Red-Get SQLDoc would work just as well.

We have set the documentation set to rebuild on a rolling week basis and active database projects are rebuilt on a nightly basis.  Our DB developers can force a rebuild if they want a more up-to-date version.

Anyone browsing the code intranet can see the following
1.  A summary describing the purpose of the database object
2. Any additional comments/remarks for stored procs, functions, triggers and views.
3. Any parameters with their intended use.
4. Any return values
5. Any interdependencies with other objects.

Some developers take it to the level of incorporating schema diagrams or flow diagrams.  This does take additional effort and it tends to be those developers who don't like being called out at 03:00 who document it to that level.

I live in hope that one of the three suppliers mentioned above will extend their product to incorporate broader aspects  such as Jobs, Replication, Cubes, SSIS/DTS packages into their documentation capabilities.
April 15, 2009 10:05 AM
 

BuggyFunBunny said:

The most maintainable code is code that isn't there.  One of the unix guys said something close to that.  Since this is a RDBMS forum, I'll mention that the notion of declarative, code free development edges ever closer to fruition for such systems.  From the birth of COBOL (code that is 'English'; "any manager can understand the process"), through Model Driven Architecture, to full on code generation from the catalog, we approach Nirvana (Valhalla?).

In the last few years have been middlegen and andromeda.  Currently, in the Python web development world is sprox, which reads the catalog and spits out Python and templates and javascript (I think, still looking into it, but it does integrate with Dojo) for the UI.  

Since I am a 5NF sort of person (I know; Old Comber gets me through the day), but too lazy to attempt to write such a parser/generator, using the catalog to generate all external artifacts always seemed the smartest thing to do.  The MDA folk, and some of the UML folk (and do I hear Oslo's siren song in the distance?), want to run both the catalog and the UI from their "model data"; I am less enamoured of that approach.  

Since the catalog, whatever the RDMBS, is clear text, I have always suspected that large enough organizations have built bespoke generators as their own secret weapons.  One could also parse SP's too, with sufficiently stringent coding conventions.  For those old enough, and unfortunate enough, to have been around COBOL in the 60's to early 80's, code generators were de rigour.  The idea is not so farfetched.

So, in a better world (perfection shouldn't necessary) the catalog would be all that needs maintaining.  Push the Big Red Button to generate a new release of all other tiers' code.  What could be more maintainable than that?

Since Phil, today, discusses RegEx (as the examples show, a *very* useful tool for generators), I wonder how many such generators he can claim as his?  Would he spin some tales about whether they were worth the effort?  I, for one, would sure like to know.
April 15, 2009 12:22 PM
 

Phil Factor said:

I once worked on a system that was written in Cobol, but not the sort of Cobol with meaningful labels that one could read like a book, it was completely opaque and all labels were numbers. It had actually been written in a 3GL code generator; We were just seeing the Cobol output. The actual source had been lost and the 3GL had ceased to be maintained anyway. The application struggled on regardless, but was passed to a poor soul who was tasked with maintaining this pile of scary Crooked Cobol. It was his first programming job. He knew no better, and learned on the job. Incredibly, he taught himself to make sense of it all to the point that he felt unhappy about writing modules any other way. He added to the system in the same bizarre machine-generated style.

For this developer, the output of a code-generator was delightful, and maintainable. We tapped away in OO languages and tried to tantalize him with the delights of real development languages, but it was impossible to dissuade him of the virtues of machine-generated cobol. Each to his taste.
April 15, 2009 12:55 PM
 

Louis Somers said:

To me, maintainable code is code that has been obfuscated, encrypted or compiled to machine language which is difficuilt to decompile.

To maintain the distribution and use of code, one needs online registraton mechanisms, tamper detection, strong name key signage, dongles or private permutation's, all attached to servers on the Internet that can log usage ande verify licensing.

Considering that self destruction is also becoming a part of "maintainable code" (see products like "Shelve life"), I sense that there is something paradoxal or oxymoronical in "maintainable code".
April 19, 2009 4:44 PM
 

merfman said:

To me the most important thing in a maintainability debate is not to identify things that everyone should ( and mostly does ) implement by common practice, but to identify the single most common cause of software disfunction.

We all know that well structure code accompanied by well written documentation is mandatory.  So whats missing ?  If I had to identify ONE BUG BEAR it would be :

ERROR HANDLING :

And by that I mean that EVERY SINGLE LAYER of software MUST return ALL exceptions to its calling language or user environment with a unique error number that indicates ( as precisely as possible ) the cause of the error and the execution ramifications ( eg whether the application code or call continued or did not complete its task).  

This should start at the machine code / assembler layers, then pass back up the technology platform (eg .NET, ASP, runtime engine whatever) to the application error handler then to the error logs and end user.

For example, how can I  ( as an application developer ) have my code inform the user that they have run out of hard drive space and not Core RAM if the language I'm using only returns an 'insufficient resources' error ?

I make no apologies at this stage in citing code structure or lack of documentation as almost irrelevant when stacked up against the single most MAJOR aspect that needs overhaul in the IT industry.
April 20, 2009 9:26 PM
You need to sign in to comment on this blog
<April 2009>
SuMoTuWeThFrSa
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789
Migrating from OCS 2007 R2 to Lync: Part 4
 Having migrated the rest of our users and legacy resources across, and start getting ready to... Read more...

Automated Script-generation with Powershell and SMO
 In the first of a series of articles on automating the process of building, modifying and copying SQL... Read more...

Seth Godin: Big in the IT Business
 Seth Godin has transformed our understanding of marketing in IT. He invented the concept of 'permission... Read more...

Using SQL Test Database Unit Testing with TeamCity Continuous Integration
 With database applications, the process of test and integration can be frustratingly slow because so... Read more...

Converting String Data to XML and XML to String Data
 We all appreciate that, in general, XML documents or fragments are held in strings as text markup. In... Read more...