Code Quality Beyond Linters: Readability, Cohesion, and Technical Debt
From Mid-Level to Senior · Part 5 of 18
- #code-quality
- #refactoring
- #technical-debt
- #senior-engineer
- #engineering-practices
You can have a pull request where every check is green. Checkstyle is happy, SpotBugs found nothing, test coverage went up. And you can still open the main class and need ten minutes to work out what it does.
That's not a failure of the tools. Linters are very good at what they do: formatting, unused imports, obvious null bugs, methods past some line limit. What they can't do is read the code the way a person will read it six months from now, at 2am, while something is broken.
In Part 4 we talked about how to give feedback in a review. This part is about what to look for once the machine has done its job. These are the judgment calls that stay with you as you get more senior, because nobody can automate them for you.
What the linter can't see
Here's a rough split between what tools catch well and what's left for you.
Notice that the right column is mostly about meaning. A linter can count the lines in a method. It can't tell you the method's name is lying about what it does.
So the rule I'd suggest is simple: don't spend human attention on the left column. Put it in CI, make it fail the build, and stop discussing it. Then spend the time you saved on the right column.
Names carry most of the weight
If you only improve one thing, improve names. Most of the time a reader never opens the method. They read the call site and decide whether to keep going. A good name lets them skip the body with confidence. A bad one forces them in.
A few things I look for:
- Does the name tell the truth? A method called
getUserthat also creates the user if it doesn't exist is lying. Call itfindOrCreateUser. It's longer, and it's honest. - Is it named after what it means, or how it's stored?
userMaptells me it's a map.usersByEmailtells me what I can do with it. - Are there boolean parameters?
process(order, true)means nothing at the call site. Two methods, or an enum, almost always read better. - Are there vague words doing the work?
Manager,Helper,Util,Data,process,handle. Sometimes they're fine. Often they're a sign that nobody decided what the thing actually is.
A useful test: read the method out loud using only the names. If it sounds like a sentence a teammate would say, you're close.
Size is a symptom, not the disease
Linters can flag a 200-line method. But a method isn't bad because it's long. It's bad because it's doing several things, and the length is just how you notice.
My favourite test for this is the "and" test. Try to describe what the method does in one sentence. If you need the word "and" (it validates the order and calculates the total and saves it and sends an email), it's probably doing too many jobs.
Here's what that looks like in practice:
The "after" version isn't shorter because we deleted logic. The logic moved
to places with names: validation lives on Order, pricing lives in a
pricing class, the email lives behind a notifications interface. Each of
those can now be tested and changed on its own. (And yes, the double for
money was its own bug waiting to happen. BigDecimal exists for a reason.)
One caution: don't take this too far. Splitting a clear 30-line method into eight 4-line methods that you have to jump between is not an improvement. The goal is that each piece does one thing at one level of detail, not that every method fits in a tweet.
Cohesion and coupling, without the textbook
These two words get thrown around a lot. Here's the practical version.
Cohesion asks: do the things in this class belong together? A good
signal is whether they change together. If every time you touch the
discount rules you also touch the same three methods in OrderService, and
never the other twenty, those three methods probably want to be their own
class.
Coupling asks: how much does this class need to know about others to do
its job? If changing how Customer stores its address breaks code in
InvoicePdfGenerator, ShippingLabelService and FraudCheck, they all know
too much about Customer.
You want high cohesion and low coupling. In practice, the clearest warning sign is the change that touches too many files. When a small business rule change means editing seven classes across four packages, the design is telling you that knowledge is spread around instead of kept in one place.
Some questions worth asking in a review:
- If this requirement changes next month, how many files will I edit?
- Can I describe this class's job without listing everything it does?
- Is this class reaching into another object's internals
(
order.getCustomer().getAddress().getCountry().getCode()) instead of asking it a question?
When a comment is a smell
Comments aren't bad. But there are two very different kinds, and only one of them earns its place.
A comment that explains what the code does is usually a sign the code could be clearer:
// check if the user is an active admin
if (u.getStatus() == 1 && u.getRoles().contains("ADM")) { ... }
Rename and extract, and the comment has nothing left to say:
if (user.isActiveAdmin()) { ... }
A comment that explains why is different. It holds information the code can't hold:
// The payment provider returns 409 for a reused idempotency key even when
// the first attempt failed on their side. Retry with a new key on 409.
No amount of good naming will tell the reader that. That comment will save someone an afternoon. Keep those, and write more of them.
The other comments worth deleting: commented-out code (git remembers it for you), and comments that have drifted away from the code they describe. A wrong comment is worse than no comment, because people trust it.
Technical debt: pay it, log it, or leave it
Every codebase has debt, and that's fine. Taking on debt on purpose is a normal engineering decision. You ship the simpler version now because the deadline is real, knowing you'll pay for it later.
The problem is debt nobody knows about. Debt that's written down is a loan. Debt that isn't is a leak.
When you find a piece of debt, the senior question isn't "is this ugly?". Lots of code is ugly and harmless. The question is: how much is this costing us, and how often? Two things matter most: how often people touch that code, and how much it hurts each time.
A few habits that help:
- Write it down where people will find it. A ticket with a short "why
this hurts" and "what fixing it would take" beats a vague
// TODO: clean this upthat lives forever. - Link the TODO to the ticket.
// TODO(PAY-412): remove once all clients send idempotency keystells the next person exactly when it's safe to delete. - Pay small debts inside normal work. If you're already changing a method and you can rename something or extract a helper in ten minutes, do it in the same PR. Keep it a small, separate commit so the reviewer can see what's refactor and what's behaviour.
- Make the case for big debts in terms of cost. "This is messy" doesn't get time on the roadmap. "Every change to pricing takes two extra days and caused the last two incidents" usually does.
Bringing it back to review
Most of this lands in code review, so it's worth connecting it to the labels
from Part 4. Almost everything in this post is a suggestion:, not a
blocking:. A confusing name is worth pointing out. It's rarely worth
holding a release for.
The exceptions are the ones that will be expensive to change later: a public API with a misleading name, a class boundary that every future feature will have to work around, or debt being added with no record of it. Those are worth a real conversation before merge.
What to try this week
- Run the "and" test on the longest method you touched recently. If it fails, extract one piece and give it a good name.
- Delete one "what" comment by renaming or extracting, and write one "why" comment somewhere that's missing one.
- Find one
// TODOwith no ticket. Either create the ticket and link it, or delete the TODO. - In your next review, ask the coupling question: if this requirement changes, how many files would we edit?
The short version
- Let linters handle formatting and simple rules. Spend human attention on meaning.
- Names are the cheapest, highest-value fix. Make them tell the truth.
- Long methods are a symptom. The disease is doing more than one job. Use the "and" test.
- Things that change together should live together. A small change touching many files is a design smell.
- Comments should explain why, not what.
- Debt is fine when it's a decision. Write it down, and pay it based on how often it hurts.
So far Module A has been about the code itself and how it moves through a team. In Part 6 we'll go one level out, to the code other people depend on: designing APIs with contracts, versioning and backward compatibility in mind.
This is Part 5 of 18 in the series. The Senior Engineer Roadmap maps the whole series, with a short self-assessment and four reading orders.