Improving Your Craft with Static Analysis
Join the DZone community and get the full member experience.Join For Free
these days, i make part of my living doing what's called "software craftsmanship coaching." loosely described, this means that i spend time with teams, helping them develop and sustain ways to write cleaner code. it involves introduction to things like the solid principles , design patterns, dry code , pair programming, and, of course, automated testing and test driven development ( tdd ). i've spent a lot of time contemplating these subjects and their economic value to organizations, even up to the point of creating a course for pluralsight.com about this very thing . and through this contemplation, i've come to realize that tdd is an extraordinarily nuanced practice, both in terms of advantages offered and challenges presented.
this post is not about tdd, so what i'd like to do is zoom in on one particular benefit offered by the practice. it's a benefit that tends to be overlooked beside the regression suite that it generates and the loosely coupled design that it encourages. but one of the important things that tdd does is to provide a very tight, automated feedback loop. consider what generally happens if you're working on a web application and you want to evaluate the effects of your most recent changes to the code base. you build the code and then run it, and running it is generally accomplished by deploying it to some local version of a web server and then starting the web server. once the web server and your web application are running, you then engage the gui and navigate to wherever it is that will trigger your code to be run. only at this point do you get feedback about what you've done. tdd short-circuits this process by requiring only build and execution of a test suite.
of course, tdd isn't the only way to create a tight feedback loop, but it is a well-recognized one. and it's also one that tends to spoil you. after becoming used to tdd, it's hard to go back to waiting for long cycle times between writing code and seeing the results. in fact, it tends to go the other way and you find yourself chasing other means of obtaining fast, automated feedback. it was this exact dynamic that got me hooked on the idea of static code analysis . if i could get quick feedback from unit tests about whether my code worked, why couldn't i get feedback about whether it was well written?
a code quality feedback loop?
now, "well written" inherently invites a great deal of subjectivity, and it's not as though there is any universal agreement, even in a given language, as to what properties of code are ideal. but there are some pretty well established trends that get pretty wide agreement. it is preferable not to write classes and methods that are overly large or complex. it is preferable not to create modules that are too tightly coupled or needlessly interdependent. and, speaking of dependencies, it's better not to create cycles. it's pretty easy to argue that inheritance hierarchies shouldn't be too deep, method parameter rosters shouldn't be too long, and classes shouldn't be too overrun with methods.
but factoring all of these things and more into the mix, it gets sort of hard to keep track of it all. i mean, it's easy enough to be in the middle of some monster 4000 line method and think, "man, this method is waaay too big," but it can be harder to notice when you're adding a few lines to a method that may already be marginally too long. after all, it's not necessarily at the forefront of your mind since you're probably in their chasing some infuriating bug.
before giving up hope, though, consider things with which you may be more familiar, such as test coverage tools and compiler warnings. you can deliver code with minimal test coverage or even with boatloads of compiler warnings, but there's a nagging pull not to so. call it gamification or perfectionism or whatever you like, but it's there, even if you don't always obey it. there's a pressure to fix these issues because they're constantly there, in your face. they're part of a pretty tight feedback loop for you.
so i encourage you to add static analysis tools into your feedback loop. i'm not really talking about the kinds of tools that alert you if you're not following the team's coding standards (go nuts with this if you want). rather, i'm referring to the kinds of tools that show you things about your code like line count in methods, cyclomatic complexity, number of methods in a class, and class cohesion. set up tools that warn you when these things are running afoul of what they generally look like in "clean code."
what you're going to get out of this is not the bullet-proof, "one true way" to do things. life isn't that simple, people who tell you it is are selling you a false bill of goods. what you're going to get out of it is a growing understanding of architectural tradeoffs buried within the code that you write. the static analysis tool serves the same purpose as the rumble strips on highways by jolting you whenever you're venturing beyond what may be considered standard usage. sure, there might be reasons to veer onto the shoulder in certain odd circumstances, but usually you've just drifted over there due to inattentiveness. well, not anymore you won't.
if you're skeptical, just install such a tool and see what you think. see what it says about your code, but don't take any action one way or another if you're not comfortable with it. if you disagree with it, do some research and try to formulate an argument as to why. i'm not advocating that you revisit all of your programming decisions to achieve a number that some tool says you should have. i'm advocating that you make yourself aware of these numbers and the concepts that drive them so that you can have intelligent conversations about them and make informed decisions. and i'm advocating that you do this with a fast feedback loop, safely in the comfort of your own ide.
the quick feedback here is the best part of all. the static analysis tools are just executed algorithms. you're not submitting to peers for a code review or putting your code on the internet and being blasted by mean-spirited trolls. you're just helping yourself to some automated feedback with the understanding that you can keep helping yourself to it whenever you want. after enough time with this approach, you'll be prepared for the arguments that actual trolls and critics might offer up. and, hey, you might just learn some things and change some habits in ways that make you happy.
Published at DZone with permission of Josh Anderson, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.