Complexity Fills the Space it's Given
Tags: programming, development, softwaredevelopment
Published: 2024-06-17 08:16:45

I want to talk about an idea that I've started seeing everywhere during my work. To introduce it, here are a number of cases where excessive pressure in the software development process leads to certain perhaps undesirable designs.

1. You have a slightly slow algorithm, but you have enough processing power to handle it so you leave it as is (runtime fills the computing power it’s given)
2. The same is true for memory, see [any meme about chrome ram usage](https://knowyourmeme.com/memes/google-chrome-ram-hog) 
3. You have a big class with far too many responsibilities but you don't break it up (usually this leads to spaghettification of code)
4. You see a class that shouldn't really exist, it's too simple and only used in one or two places, but you might need it later so you leave it there (the topic of this post)

The last one here is what I want to talk about because I think it goes most under the radar. The class with few methods (for now) is the "space" and the complexity is what will cause your clean, well designed codebase to slowly rot over time. Because, you _will_ need that small class in future. It's the perfect spot to add a new feature, or fix a small bug, and it will grow and grow until you realize "Wow we should really break this up" so you make a bunch of new small classes and the cycle repeats again. It's one of the main problems that principles like [YAGNI](https://en.wikipedia.org/wiki/You_aren%27t_gonna_need_it) and [KISS](https://en.wikipedia.org/wiki/KISS_principle) are trying to fix. But as with most principles if you don't truly understand the problem they're there to solve applying them can feel dogmatic, and they can often be applied incorrectly.

I can't find who originally said it, but a pithy observation that applies to this issue is that: 
 > There is no such thing as a _small_ legacy codebase

That is to say: if you keep your codebase small, then it will never become what we call "legacy", regardless of its age.

(If you're thinking microservices or any other highly fragmented architecture is the answer, keep in mind that "codebase" refers to all the files, programs, services, container definitions etc. that a single team needs to manage. Whether that's a huge monolith or 124 tiny REST APIs makes very little difference.)

All of this may seem obvious, and in truth obviousness should be the goal of any half-decent software engineering blogpost. Pointing out what you already know but in a way that you can tell your boss or your colleagues. What I think might actually interest you is just how universal this particular phenomenon is. Here are some examples where creating space can be done without much thought, but can lead you down the road to an incomprehensibly large architecture.

1. **Reducing code duplication too eagerly.** Principles like [DRY](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself) encourage
us to limit how much code we copy and paste. Valuable to be sure, and a common method to implement DRY is many classes with complicated inheritance schemes, or helper functions with only one or two usages. These cases are rife with space. Space to put a little patch bugfix. Space to add a redundant type conversion or safety check that you don't actually need.
"Good" code like this is easy to develop on, so easy in fact that we often will develop until it has become bad code. (Which incidentally sounds a lot like [The Peter Principle](https://en.wikipedia.org/wiki/Peter_principle))
2. **Choosing to use a subdirectory instead of a file.** In python as an example, subdirectories or "submodules" allow you to organize your code into conceptual blocks that should tie themselves nicely together. Each subdirectory is wonderful new space to populate with files and even more subdirectories. The natural urge of "this is too many files, we should find
a way to merge some of their responsibilities" is lost in a sea of new space that can be used. I'm always impressed how popular/standardlib libraries are often quite flat, with few nestled directories, whilst in-house developed equivalents are often deeply nested and have few files per folder.
3. **Breaking your team and organization into more teams.** In this case "space" is the collective knowledgebase a team can form and "complexity" is the acronyms, endpoints, architecture, coding styles, frameworks and other tooling that the team chooses to use. This isn't always a bad thing but will be when done for the wrong reasons. A common underlying problem is just having too many under-performing members of a team. There will be consensus that the team is "understaffed and overworked". More engineers will be hired, perhaps by the same individuals that hired the first batch. Communication issues will grow and the obvious answer will be to
slice the team up. This is likely to alleviate some of the immediate issues but is unlikely to bear fruit in the long term. The underlying problem was never really solved. Instead the complexity will grow and you will wonder why your IT department is so damn big but doesn't seem to be able to deliver.

The core malady here is when this complexity becomes *unnecessary*. Hard problems usually require complex solutions. Sure, sometimes there _is_ a beautiful mathematical formula to describe a problem but often there isn't (especially whenever you are building anything human-facing). There's no shame in having a complex solution to a complex problem. Too often however, the complexity is just there because of all the space that was available, regardless of the hard-ness of the problem at hand. It will feel intractable, comments of "it's always been this way" come up regularly. Cynicism and apathy will propagate in the team, and many projects
either die or enter a kind of life-support state.

## Can we fix it? 

(This is entering more-opinionated-than-before territory)

With enough effort, yes. Restarting a project can be an option, but better yet is to simply recognize that there is a problem and methodically focus on the underused or overbuilt components. Even if you need to redo everything does not mean you need to throw it all out right now. It will probably be costly, and still requires radical surgery but it can be done. ([Twitter](x.com) is currently attempting this and it remains to be seen if it will work.)

## Can we stop it from happening in the first place?

One of the most important lessons beyond simple principles like YAGNI and KISS is a simple rule you can apply in your own development: [If you don't understand a problem you're not allowed to fix it](https://therealgriff.medium.com/if-you-dont-understand-a-problem-you-re-not-allowed-to-fix-it-af6b8054606c) For all developers, from the highly capable to the less-so, taking the time to understand a core problem is how you identify if unnecessary complexity is to blame. This applies also to managers defining teams in an organisation. Many of us are aware that "the quick patch" is a precursor to technical debt, but fewer perhaps might recognise that "too much space" in your organisation or codebase or filesystem or class is what makes that quick patch so alluring.

Running a tight ship. Less is more. Simple is better than complex etc. "Complexity Fills the Space it's Given" is one in a long string of phrases that ultimately mean the same thing. But perhaps the more times it is said, the easier it will be for you to convince other people who need to hear it that taking time and thinking about problems deeply is at the core of what we do. A good codebase may last a long time, and it will cost us very little to maintain. We just need to believe that we _can_ have nice things.

I'm Tomass Wilson and I work on an in-house python library, these opinions are my own etc etc. This is my first real blogpost like this and I'm open to feedback.