What's Your Signal-to-Noise Ratio in Your Code?
Join the DZone community and get the full member experience.Join For Free
you write code to deliver business value, hence your code deals with a business domain like e-trading in finance, or the navigation for an online shoe store. if you look at a random piece of your code, how much of what you see tells you about the domain concepts? how much of it is nothing but technical distraction, or « noise »?
like the snow on tv
i remember tv used to be not very reliable long ago, and you’d see a lot of « snow » on top of the interesting movie. like in the picture below, this snow is actually a noise that interferes with the interesting signal.
the amount of noise compared to the signal can be measured with the
. quoting the definition from wikipedia:
signal-to-noise_ratio (often abbreviated snr or s/n ) is a measure used in science and engineering that compares the level of a desired signal to the level of background noise. it is defined as the ratio of signal power to the noise power. a ratio higher than 1:1 indicates more signal than noise.
we can apply this concept of signal-to-noise ratio to the code, and we must try to maximize it, just like in electrical engineering.
every identifier matters
look at each identifier in your code: package names, classes and interfaces names, method names, field names, parameters names, even local variables names. which of them are meaningful in the domain, and which of them are purely technicalities?
some examples of class names and interface names from a recent project (a bit changed to protect the innocents) illustrate that. identifiers like « cashflow »or « cashflowsequence » belong to the ubiquitous language of the domain, hence they are the signal in the code.
on the other hand, identifiers like « cashflowbuilder » do not belong to the ubiquitous language and therefore are noise in the code. just counting the number of « signal » identifiers over the number of « noise » identifiers can give you an estimate of your signal-to-noise ratio. to be honest i’ve never really counted to that level so far.
however for years i’ve been trying to maximize the signal-to-noise ratio in the code, and i can demonstrate that it is totally possible to write code with very high proportion of signal (domain words) and very little noise (technical necessities). as usual it is just a matter of personal discipline.
logging to a logging framework, catching exceptions, a lookup from jndi and even @inject annotations are noise in my opinion. sometimes you have to live with this noise, but everytime i can live without i definitely chose to.
for the domain model in particular
all these discussion mostly focuses on the domain model, where you’re supposed to manage everything related to your domain. this is where the idea of a signal-to-noise ratio makes most sense.
it’s probably possible to create a metric for the signal-to-noise ratio, by parsing the code and comparing to the ubiquitous language « dictionary » declared in some form. however, and as usual, the primary interest of this idea is to keep it in mind while coding and refactoring, as a direction for action, just like test coverage.i introduced the idea of signal-to-code ratio in my talk at dddx 2012, you can watch the video here . follow me ( @cyriux ) on twitter!
Published at DZone with permission of Cyrille Martraire, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.