Estimate project
← All posts

Why Code Modularity and Clear Boundaries Matter More Than Ever Today

Everybody appears to have a view on how AI will change our lives and our work as software engineers. Here is my take on one particular subject: modularity, encapsulation and well-drawn software boundaries in the era of AI coding.

6 min read
Software DevelopmentModularityLLMAI

We certainly live in interesting times, don’t we? Some people call it the largest shift the industry has seen yet: the era of AI writing code on its own – or, at the very least, the era of AI-assisted coding. Everybody appears to have a view on how AI will change our lives and our work as software engineers. Here is my take on one particular subject: modularity, encapsulation and well-drawn software boundaries in the era of AI coding.

The importance of modularity

Talk of modularity usually brings microservices to mind – or at least a modular monolith. Code modularity in general, however, rests on being able to assemble something out of smaller, clearly defined blocks, a bit like LEGO bricks. In the end, whether you deploy it as a microservice or as an ordinary monolithic app hardly matters.

Badly divided microservices can cause problems even worse than monoliths without any modularization: tightly coupled systems that are hard to maintain and scale, in which changing one part triggers a cascade of changes elsewhere – the so-called ‘shotgun surgery.’ This happens, for instance, when you divide your code along technical functions or team structure instead of the business capabilities you need to support.

By contrast, modularity done right – with encapsulation, a minimal API surface and proper boundaries – makes systems more flexible and easier to maintain. The possible area and scope of changes tend to be much smaller.

The human role in AI-assisted development

Some claim that modularity and proper boundaries matter only to us, mere human developers. If AI will soon be able to regenerate even huge codebases with every change, perhaps we can stop worrying about it? Still, somebody will have to review those changes, right? You don’t want to ship huge changes to production and simply hope for the best.

Even if full test coverage verifies that AI-generated code is correct, keeping such a test suite up to date turns into a nightmare, particularly when changes happen often and touch the whole application or many dependent services.

Who tests the tests?

Picture updating hundreds of tests each time AI regenerates a large part of your codebase. That happens because responsibilities leak, forcing changes in many places, and because things aren’t private or tucked away behind well-defined interfaces. It is precisely the ‘shotgun surgery’ described above playing out in your codebase: one change calls for edits in several unrelated areas. Of course, you could have AI generate the tests, but how can you be sure they are correct? Who tests the tests?

If we, as software engineers, are going to become nothing more than reviewers of AI code, we had better keep the scope of changes narrow to keep complexity down. It’s in our own interest. It will make life easier for us, since the volume of code to review will shrink to the absolute minimum and, more importantly, it will be focused, with changes made in one specific module, package or component – call it what you like.

It will be easier for us to load the context into our heads and hold it there during the review, which allows for a more deliberate review and a better-informed decision to accept or reject the changes.

Being the ultimate gatekeepers

Then again, what if an LLM cheerfully adds yet another ‘if’ to the already enormous heap of ‘if’s in our logic? As the ultimate gatekeepers, it’s up to us to judge whether the spaghetti code has gone far enough and to carry out or commission a rework of the whole solution, one that may be a little more complex than the original change on its own, but done with a different aim – readability for our future selves.

And I’m not even getting into reviewing LLM-generated code and making sure it is secure. It’s only a matter of time before we see widespread attacks in which an LLM suggests and injects malicious code, or supply chain attacks are attempted through installing suggested dependencies, and so on. That subject surely deserves a blog post of its own.

The role of LLMs as a teammates

LLMs are powerful tools – that much is certain – but they aren’t able to write modular code yet.

Modularity can’t be measured. There’s no way of saying whether one codebase is more modular than another. Everything depends on the context in which the code is used, the constraints and architecture drivers, the purpose of the code and so on. Training AI models on such vague qualities and expecting them to get it right is hard. Again, some might argue that LLMs don’t need modularity, since code readability means nothing to them – it’s all machine, after all.

Human in the loop

Nor do they care about our architectural drivers or what we optimize a particular code path for. Some of these drivers can be captured in tests, but someone has to maintain those tests over time, which brings us back to the earlier point – a human is still in the loop.

The fact that LLMs can’t write properly crafted, modular code shouldn’t entitle us to stop caring about it. Apart from the fact that we still read the code, there’s another reason: the narrower the area of change, the better the results LLMs produce – much as specific prompts lead to better results when we chat with, say, ChatGPT.

Modern LLM models

For example, if you ask an LLM to implement a feature or do some refactoring where you know the impact will stay within a package, module or component, you’re likely to get better results than when the change spreads across the whole application or several microservices repositories.

You might object that ‘modern’ LLM models handle large contexts well and can hold entire codebases in them while helping us. True, they can, but it’s like handing a model a carefully written, precise prompt with clearly specified boundaries versus tossing it a vague question and hoping for great results. LLMs are trained on datasets and problems produced by humans, which are naturally limited in scope so that people can work on them effectively. The whole discipline of prompt engineering didn’t come about by accident.

Setting guardrails for AI-generated code

As engineers, it’s our job to put guardrails around AI-generated code and to adapt whatever the AI suggests accordingly. LLMs don’t take our project’s business context into account unless it is given to them explicitly (is it even possible to hand the LLM the entire context? Is that feasible?) They just generate code from patterns and prompts.

That means it’s up to us to make sure the code they produce matches our architectural goals and our quality and maintainability requirements. Keeping our codebase in good shape requires reviewing the code, suggesting changes, iterating on the proposed solution and so on. Being able to make those changes within a fairly narrow scope – where most changes stay inside ‘private’ areas and are implementation details – gives us additional comfort and fewer compatibility problems, and lets us genuinely understand and accept the changes.

Foundations of software engineering don’t change

I don’t think good software engineers are doomed in the age of AI. Our day-to-day work will change a lot, but the foundations of software architecture and engineering will stay essential.

In fact, as we lean on AI more and more in our development workflows, these principles will matter even more. By truly focusing on modularity, encapsulation and proper boundaries, we raise the odds that AI-generated code is correct in one place and doesn’t unexpectedly alter other parts of the system, since we are able to fully understand and validate the changes.

AI is transforming the way we build software – that is beyond doubt. In my view, though, it won’t remove the need for good design and architecture, nor will it render obsolete all the foundations and practices of good software craftsmanship. On the contrary, it will make these principles even more important.