There’s a metaphor I had in mind for a long time when thinking about software design: because I’m proudly lazy, in order to make the code smaller and easier to learn, I must do my best to reduce the “surface-area over volume ratio” of the software.
Surface-area over volume ratio?
I like the Surface-area over volume ratio as a metaphor to express how to make software cheaper to discover and learn, and smaller to maintain as well.
For a given object, the surface-area over volume ratio is the amount of surface area per unit volume. For buildings and for animals, the smaller this ratio, the less the heat loss during the winter, hence a better thermal efficiency.
Have you ever noticed that huge warehouses were always cool even during the summer when it’s hot? This is just because in our real 3D world the surface-area over volume ratio is much smaller when the absolute size of the building increases.
The theory also mentions that the sphere is the optimal shape with respect to this ratio. In fact, the more “compact” the less the ratio, or the other way round we could define compactness of an object directly by its surface-area-over-volume ratio.
What about software design?
Let’s consider that each method signature of each interface is part of the surface-area of the software, because this is what I have to learn primarily when I join the project. The larger the surface-area, the more time I’ll need to learn, provided I can even remember all of it.
Larger surface is not good for the developers.
On the other hand, the implementation is part of what I would call the volume of the software, i.e. this is where the code is really doing its stuff. The more volume, the more powerful and richer the software. And of course the point of Object Orientation is that you don’t have to learn all the implementation in order to work on the project, as opposed to the interfaces and their method signatures.
Larger volume is good for the users (or the value brought by the software in general)
As a consequence we should try to minimize the surface-area over volume ratio, just like we’re trying to reduce it when designing a green building.
Can we extrapolate that we should design software to be more “compact” and more “sphere”-like?
Reusing the same interface as much as possible is obviously a way to reduce the surface-area of the software. Adhering to interfaces from the JDK or Google Guava, interfaces that are already well-known, helps even better: in our metaphor, an interface that we don’t have to learn comes for free, like a perfectly isolated wall in a building. We can almost omit it in our ratio.
To further reduce the ratio, we can find out every opportunity to use as much as possible the minimum set of common interfaces, even over unrelated concepts. At the extreme of this approach we get duck typing in dynamic languages. In our usual languages like Java or C# we must introduce additional small interfaces, usually with one single method.
For example in a trading system, every class with a isInCurrency(Currency) method can implement a common interface CurrencySpecific. As a result, a lot of processing (filtering etc.) on stuff that is related to currencies in some way can be done on all these classes without any knowledge about them, except their currency-specificity.
In this example, the currency-specificity we extracted into one interface is like a single facet over a larger volume made of several implementation. It makes our design more compact, it will be easier to learn, while offering a rich set of behaviors.
The limit for this approach of putting a lot of implementation code under the same interface is that sometimes it really makes no domain sense. Since code is primarily meant to describe the domain, without causing confusion we must be careful not to go too far. We must also take great care when sharing interfaces between bounded contexts, there’s a high risk of excessive coupling.
Yet another metric?
This metric could be measured by a tool, however the primary value is not in checking the figures, but in the thinking and taking care of making the design easy to learn (less surface-area), while delivering a lot of valuable behaviors (more volume).Follow me on Twitter!
5 thoughts on “Surface-area over volume ratio – a metaphor for software design”
you might remember that I hinted at the idea of surface, volume and density in an old post in my blog (http://www.carlopescio.com/2010/10/notes-on-software-design-chapter-11.html, you added an interesting comment as well :).
In my work on the physics of software, I’m trying rather hard not to label things as “good” or “bad”, but to reach a deeper understanding of consequences instead. For instance, a small surface is easier to learn, but very often you have to sacrifice control (customizability, observability, etc). So it largely depends on what you’re trying to build.
Ideally, we would have a fractal-like surface, where we can learn more when we need to do so. It’s not totally obvious how to create one, of course.
Also, in my current understanding a class has at least two surfaces, one for callers, the other for inheritors. I’ve tons of notes on this kind of stuff, hopefully sooner or later I’ll put them together into a post : )
Thanks for your great comments! I remember your post on mass and viscosity, but I forgot you mentioned surface. I eventually decided to finalize and publish this post that was pending for years, to share a common feeling I often have when thinking about my code, a bit like the feeling of signal to noise ratio.
You mention a good point, there is no absolute “good” or “bad” practice, and I tend to be a bit too bold in my posts. Ultimately you say it best:
Talking with colleagues today I can add that design patterns also suggest to go towards lower Surface-area over volume ratio. For example the Composite pattern suggests to use the same interface (Component) for both the Leaf and the Composite; the Proxy pattern suggests to use the exact same interface for the code doing the job and the code doing the remote call, and so on.
Good point again! Yet I really dislike inheritance these days, so in my view at present only the former kind of surface really matters, and I tend to forget about everything inheritance…
When you are taking about “learnability” or “easy to learn”, I suppose you have in mind the people who are to maintain the software. But those guys are only one of the many stake-holders. Or, are you talking about user-interface-design and do you have in mind the users of the software (perhaps not, because you are talking about methods and their signatures)? If you are, isn’t a single interface, which provides (even if only by way of links) for all that the particular user needs / wants / is authorized to do, considered good for the user? So, large surface area also may be good for the users.
When you say “we should try to minimize the surface-area over volume ratio”, Is that a directly usable directive? How exactly do you put that into practice? When you say those words, aren’t you repeating the basic design-tenet: decompose the problem, identify the orthogonal primitives in the problem-domain, and then build the software as a set of multiple compositions of those primitives? Isn’t that a directive that is directly usable in practice?
What is the value of the metaphor, really?
Just two more observations:
1. It is a well-settled principle of software-design that any addressable chunk of software (procedure / function / method / class / file) must be cohesive – do one small and specific thing. This ensures testability, manitainability and reusability. This means “implementation or volume” should be small, for each of the modules.
2. For a given scope, small cohesive modules implies large number of modules or interfaces ie. large “surface area”.
Comments are closed.