Secrets of Maintainable Codebases
Quality of code is important, but the secret to maintainable codebases lies in a good test suite, high cohesion, low coupling, and low time to comprehension.
Join the DZone community and get the full member experience.Join For Free
you should write maintainable code. i assume people have told you this, at some point. the admonishment is as obligatory as it is vague. so, i’m sure, when you heard this, you didn’t react effusively with, “oh, good idea — thanks!”
if you take to the internet, you won’t need to venture far to find essays , lists , and stack exchange questions on the subject. as you can see, software developers frequently offer opinions on this particular topic. and i present no exception; i have little doubt that you could find posts about this on my own blog.
so today, i’d like to take a different tack in talking about maintainable code. rather than discuss the code per se, i want to discuss the codebase as a whole. what are the secrets to maintainable codebases? what properties do they have, and what can you do to create these properties?
in my travels as a consultant, i see so many codebases that it sometimes seems like i’m watching a flip book show of code. on top of that, i frequently find myself explaining concepts like the cost of code ownership, and regarding code as, for lack of a better term, inventory. from the perspective of those paying the bills, maintainable code doesn’t mean “code developers like to work with” but rather “code that minimizes spend for future changes.”
yes, that money includes developer labor. but it also includes concerns like deployment effort, defect cycle time, the universality of skills required, and plenty more. maintainable codebases mean easy, fast, risk-free, and cheap change. here are some characteristics in the field that i use when assessing this property. some of them may seem a bit off the beaten path.
a high-value test suite
you had to see this coming, so i’ll get it out of the way right off the bat. you want unit tests. that notion is up there with death and taxes among life’s inevitabilities. and yet, so often we miss the mark with interim valuations.
people tend to look for the presence of unit tests or the number of unit tests at first, and then mature to look for unit test coverage. i don’t, personally, find any of these metrics highly predictive of maintainable codebases (though the absence of any automated testing generally correlates with low maintainability).
surprising? not if you consider how easily people can spray low-value unit tests at an existing codebase. want coverage? write a bunch of tests that invoke that gigantic singleton at the heart of your application, swallow all exceptions, and don’t assert anything. you can probably get your coverage up near 30% in just a few hours while doing absolutely nothing to improve your maintainability.
i look for two basic things when doing a snap evaluation of a test suite: if i delete a line of production code, does a test go red, and do the unit tests have high code churn? this indicates a high-value suite via goldilocks triangulation. i know that any line in the codebase was created according to a plan and i know that the tests themselves are not unduly adding to the maintenance burden.
high cohesion, low coupling
here, i offer a broad concern, but that’s because i look for it thematically, in all facets of the codebase. the easiest way that i can express these concepts in a narrative sense is as follows:
- high cohesion: does stuff that needs to change together occur together?
- low coupling: do you avoid making otherwise independent concerns dependent?
this represents a surprisingly holistic concern when talking about codebases. developers will want to have a sense of these concepts when creating classes or even methods. but it rolls up from there as well. when eyeing more strategic application concerns, the team should consider cohesion and coupling for namespaces, projects, and even disparate applications as well. does your codebase take on external service dependencies cavalierly or sparingly?
low cohesion tends to drive risk and reputation related problems. if i use my couch to sleep, and i also put wheels on it and use it to get to work, my life gets weird in a hurry, and i offer explanations to others that make no sense. this happens because my couch is doing two disparate things. “sorry i was late to the office — one of the legs broke off of my couch.” in code that becomes, “sorry you couldn’t log in for a while — we messed up the css for a maintenance screen.”
high coupling tends to create business capability nightmares. “oh, yeah, i can’t move out of my apartment ever. it turns out that i cemented my couch to my refrigerator and now the whole thing won’t fit through the door.” fusing weird things together in your code will make “no way” the answer to “can we add feature x” way too often.
low time to comprehension
the last secret that i’ll mention covers more ground than it may seem like at first. frequently, i’ll see advice on maintainable code that includes things like, “indent consistently” or, perhaps more profoundly, “take the time to think of good names.” yes, and yes. definitely, both of these pieces of advice will serve you well.
but the broader concern here, particularly at the codebase level, has to do with the 0 to comprehension. generic pieces of advice about code readability address the wide audience of developers and assume a relatively fluid influx and efflux of team members. but the maintainability of a particular codebase, maintained by a particular team at a particular point in time, will hinge heavily on how quickly that team understands the code.
obviously, a critical factor here is the development labor effort. if i get a defect in my queue and trace it to some part of the code, then i’ll have a fix more quickly when i understand the code more quickly. but low time to comprehend extends way beyond this simple concern. absent barriers to comprehension, knowledge transfer and communication become simpler. the team benefits via more stability in general maintenance and development, and via a substantially increased likelihood of collaboration.
to put it another way, you’ll spend a lot less money on a codebase where no one says, “oh, that’s in bob’s code… you’re going to have to take that up with bob.” even assuming bob can fix his own, inscrutable stuff quickly, the cost of ownership of that code includes the caveat, “and it gets a lot more expensive if bob ever takes a vacation.”
you maintain codebases, not code
it may seem, to some extent, as though i’m quibbling. but i really don’t think that’s the case at all. nobody maintains a method or six files of code — they maintain applications.
certainly applications consist of code, and certainly, that code being maintainable helps the application’s maintenance footprint. and you should strive to keep any bit of code that you write clean and maintainable.
but don’t lose sight of the real goal here. understand that you need to think about the application and that you need to think about the cost of owning it in a whole variety of ways. when you start thinking that way, you’ll find yourself in on the secrets of maintainable codebases.
Published at DZone with permission of , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.