Lesson 7 of 17 · The Forces That Shape Code
Under-Engineering vs Over-Engineering
What "over-engineering" actually means
Wikipedia's definition of over-engineering:
Overengineering (or over-engineering) is the act of designing a product or providing a solution to a problem in an elaborate or complicated manner, where a simpler solution can be demonstrated to exist with the same efficiency and effectiveness as that of the original design.
That's the textbook version. In day-to-day code reviews, the word gets used much more loosely than that, and that's where the confusion starts.
How I understand over-engineering and under-engineering in terms of code.
Over-engineering:
When it describes good code: makes sense for maintainability in the long run.
When it describes bad code: badly written and completely unnecessary.
Under-engineering:
When it describes good code: makes sense for the short run.
When it describes bad code: badly written and completely unmaintainable.
The same word covers two opposite things. That's why team arguments about "this is over-engineered" so rarely go anywhere. Both sides are using a word that means two things, and neither side stops to ask which one the other one means.
The "looks over-engineered" example
Here's an example of code that people may think is over-engineered. It's built around a command bus: the controller packages up what should happen into a plain object, the SignUpMemberCommand, and hands it to a dispatcher. A separate class, the SignUpMemberCommandHandler, does the actual work when the dispatcher runs it, and returns a result. That's why dispatch() on the controller's side comes back with $memberId - it's just the handler's return value, passed straight through:
<?php
declare(strict_types=1);
namespace App\Members;
use Illuminate\Contracts\Bus\Dispatcher;
use Illuminate\Http\JsonResponse;
final readonly class SignUpMemberController
{
public function __construct(
private Dispatcher $bus,
) {}
public function __invoke(SignUpMemberRequest $request): JsonResponse
{
$memberId = $this->bus->dispatch(new SignUpMemberCommand(
email: $request->validated('email'),
password: $request->validated('password'),
fullName: $request->validated('full_name'),
));
return new JsonResponse(['id' => $memberId], 201);
}
}<?php
declare(strict_types=1);
namespace App\Members;
use Illuminate\Foundation\Http\FormRequest;
final class SignUpMemberRequest extends FormRequest
{
public function rules(): array
{
return [
'email' => ['required', 'email', 'unique:members,email'],
'password' => ['required', 'string', 'min:8'],
'full_name' => ['required', 'string', 'max:255'],
];
}
}<?php
declare(strict_types=1);
namespace App\Members;
final readonly class SignUpMemberCommand
{
public function __construct(
public string $email,
public string $password,
public string $fullName,
) {}
}<?php
declare(strict_types=1);
namespace App\Members;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Hashing\Hasher;
final readonly class SignUpMemberCommandHandler
{
public function __construct(
private Members $members,
private Hasher $hasher,
private Dispatcher $events,
) {}
public function handle(SignUpMemberCommand $command): int
{
$member = Member::signUp(
email: $command->email,
passwordHash: $this->hasher->make($command->password),
fullName: $command->fullName,
);
$this->members->save($member);
$this->events->dispatch(new MemberSignedUp($member->id));
return $member->id;
}
}This code looks over-engineered at first sight. Why would anyone write code like this for such a simple create operation? It must be the ego, right?
I don't believe that people go to work thinking "I'm going to write this complex code just so that I can make my life harder, because building software is easy".
I do believe that people who understand what they're doing write code like this so that they can make their lives easier later, in order to manage the complexity that's coming.
There are people who misuse and misunderstand design patterns and principles for the sake of it. That's real. It's just not the main reason someone writes code like this.
The "looks simple" example
Here's an example of code that you may think is fine, but is actually under-engineered:
<?php
declare(strict_types=1);
namespace App\Members;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
final class SignUpMemberController
{
public function __invoke(Request $request): JsonResponse
{
$member = Member::create([
'email' => $request->input('email'),
'password' => bcrypt($request->input('password')),
'full_name' => $request->input('full_name'),
]);
return new JsonResponse(['id' => $member->id], 201);
}
}Eight lines. Does the job. On a brand website, on a side project, on a one-off internal tool, this is good code. There is nothing to fix here, in those contexts.
In a scale-up or an enterprise product, the same eight lines are a problem in slow motion. Not because they're wrong today, but because every future requirement will land on top of them, in this same controller, the same way the reservation controller in the previous lesson grew tentacles.
So which is which?
The context it's written in.
The level of maintainability and the ability to change it easily as the business rules and processes become more complex over time.
The first code example, written in a scale-up or enterprise product, is simple and easy to maintain. The seams are already there, named, in their own files. New behavior lands in new classes. The original ones don't need to change.
The second code example, written for a brand website or a throwaway personal project, is also simple and easy to maintain. There's nothing to refactor. There's no second feature to share with. The "structure" the first example has would be paid for, every reading, in exchange for nothing the project will ever cash in.
The use of various design patterns and principles is also called over-engineering, but that's mostly from a lack of experience or understanding of the code. The label says more about the reader's familiarity than about the code itself, in those cases.
Both feel like virtue from the inside
Part of why this is a hard topic is that both extremes feel principled when you're the one doing them.
The under-engineer thinks they're being pragmatic. They're avoiding YAGNI. They're shipping value. They're not building a cathedral for a feature that hasn't been asked for. All of that is true some of the time, and it's also a pretty good cover for "I didn't want to think about it".
The over-engineer thinks they're being responsible. They're paying down future tech debt. They're protecting the next developer. They're applying the patterns the senior team uses everywhere else. All of that is true some of the time, and it's also a pretty good cover for "I wanted to use this pattern and I found a place to put it".
You can't tell, from the inside, whether you're being pragmatic or sloppy, responsible or self-indulgent. The only honest test is the situation in front of you, and whether the structure you wrote matches its real demands.
Consistency in the codebase
Consistency is important here. If you have some kind of structure and coding standards, it may look like the simpler use cases of the application are over-engineered.
Your first instinct will be to simplify the first example. Strip out the request, fold the command into the controller, drop the event. It looks cleaner. It is, in isolation. The cost is that the code becomes inconsistent with everything else in the system. The next reader has to learn two patterns instead of one. The simple version gets harder to extend, and the team starts negotiating "is this one big enough to deserve the full pattern, or not?" on every feature, which is a meeting nobody needs.
In a codebase that's already shaped around commands, requests, handlers, and events, writing the "simple" version of one feature is an exception people will trip over for years. Consistency wins. Inside one product, pick the level of structure that fits the largest features and use it for the small ones too.
What gets added to "Sign Up Member" later
A few things that might be added to the Sign Up Member use case:
Sign the member up to the onboarding and marketing newsletters, if the member gave consent.
Assign a role and permissions to the member.
Send an account activation email.
Add an audit log entry.
Add a tracking log of where the member signed up from.
Start the free trial.
Apply a coupon code, if applicable.
Check that the member is above 18 years of age.
Validate their address or location.
Assign the member to a team.
I can think of many more, and you can see how things get complex very quickly. None of these are unusual. They're the standard package any real product accumulates over a couple of years.
If you keep writing the code in the second example, right there in the controller, and your context is a well-funded startup, a scale-up, or an enterprise product, you will have a hard time managing that codebase. The potential for bugs increases. The speed of developing or working with that code slows down quickly.
Once those features are implemented in a single fat controller, changing the whole use case to be slightly different affects many scenarios that will have to be re-tested. The work of one small change keeps reaching across rules that have nothing to do with each other.
In the first example, most of the code stays the same. New features land in new classes: a listener for the welcome email, a listener for the audit log, a separate command for applying a coupon, a policy for the age check. The main class doesn't need many changes. You also get the benefit of readability for other team members.
If you're working alone and you're the person who comes up with these ideas, you can probably manage the second example somehow. It won't be easy, but you'll know what and where to change because you wrote the code. It may not be the easiest thing to work with, and it doesn't extend the same kindness to anyone who joins later.
How to spot under-engineering early
A few practical tells:
The same logic appears in three places, slightly different in each.
A controller is doing five jobs.
You can't change a business rule in one place. You change it in four files and hope.
New developers can't find where a concept lives, because it doesn't live anywhere in particular.
Tests have to set up half the application to exercise one rule.
The file you change every sprint is the same one, no matter what the ticket is about.
If you see these in a codebase that's been running for a few years and growing, that's not a clean MVP holding up. That's accidental complexity baked into a flat shape.
How to spot over-engineering early
The other side has its own tells:
An interface with one implementation, and no plan for a second.
A factory that creates exactly one thing.
A "configurable" feature with only one configuration anyone has ever used.
A layer that just forwards calls to the next layer with no decision in between.
A pattern was added because a blog post said it should be there, not because the codebase asked for it.
Mocking is painful out of proportion to what the test is actually checking.
If you see these in code that's barely been alive long enough to need them, that's structure spent up front for needs that may never arrive.
Two principles to keep you near the line
Two short rules of thumb that catch most of the bad calls in both directions:
Wait for the third occurrence. Don't extract a shared abstraction the first time you see something twice. Let it be duplicated. The third time, you've earned the information about which parts of the duplication are the same and which only look the same. Abstract on three. The next lesson goes deeper into this one.
Pattern follows pain. Don't apply a pattern preemptively. Apply it when the pain it solves shows up. If the pain hasn't shown up, the pattern is paying for itself with someone else's reading time.
These don't replace context. They work inside it.
What this isn't
Two failure modes bookend this argument. The codebases that age worst mistook minimalism for discipline - the "just write code, no structure" version. The ones that age second-worst mistook ceremony for craft - the "always extract everything, layers all the way down" version.
This also isn't the same as YAGNI. YAGNI is a useful tool when applied honestly, and a useful excuse when applied lazily. It's one input, weighed alongside everything else in this lesson.
These are simple code examples to illustrate the idea. We'll come back to this topic later in the series, with more complex use cases where a more complex code sample will be refactored, so you can see how this lands in practice rather than in theory.
The one thing to remember
Over-engineering and under-engineering are both failures, both feel like virtue from the inside, and neither has a fixed definition. The same code that is over-engineered for a side project is correct for a scale-up. The same code that is fine for a scale-up is unmaintainable in an enterprise.
The job isn't to land on a single right amount of structure. It's to keep asking, every time you write or review, whether the structure in front of you matches the situation it lives in.