Back to insights
Coding
5 min read

Writing Code Your Future Self Will Thank You For

Code is read far more often than it is written. You will write a function once, but you, your teammates, and a future version of you will read it dozens of times while fixing bugs and adding features. Optimizing for that reader is one of the highest-leverage habits a developer can build.

Here are the practices that make the biggest difference, with examples in TypeScript that apply to nearly any language.

Name things honestly

Naming is the cheapest and most powerful form of documentation. A good name removes the need for a comment.

Compare these:

TypeScript
// Hard to follow
const d = u.filter(x => x.a && !x.b).map(x => x.n);

// Clear
const activeUserNames = users
  .filter(user => user.isActive && !user.isSuspended)
  .map(user => user.name);

Some guidelines:

  • Use full words. Saving three keystrokes costs every reader a moment of decoding.

  • Name by intent. getUser says what, but findUserByEmail says how it is looked up.

  • Make booleans read like questions. isLoading, hasAccess, canEdit.

  • Be consistent. If you say fetch in one place, do not say retrieve or load for the same idea elsewhere.

If you struggle to name something, that is often a signal that the function is doing too many things.

Keep functions small and focused

A function should do one thing and do it at a single level of abstraction. When a function grows long, it usually mixes high-level steps with low-level details, which makes it hard to see the overall flow.

TypeScript
async function checkout(cart: Cart, user: User) {
  validateCart(cart);
  const total = calculateTotal(cart, user.discountCode);
  const payment = await chargeCard(user.paymentMethod, total);
  await createOrder(user, cart, payment);
  await sendConfirmationEmail(user, payment);
}

Reading that top to bottom tells you the entire story in five lines. Each step hides its own details, and you can dive into whichever one you need.

Length alone is not the rule. A twenty-line function that reads clearly beats five tiny functions that force you to jump around. Split based on meaning, not line counts.

Let the type system work for you

Types are executable documentation that the compiler checks. Used well, they make whole classes of bugs impossible.

  • Model states precisely. Instead of several optional fields that can be inconsistent, use a discriminated union.

TypeScript
type RequestState =
  | { status: "idle" }
  | { status: "loading" }
  | { status: "success"; data: Post[] }
  | { status: "error"; message: string };

With this, you cannot accidentally read data while loading, because the compiler will not let you.

  • Avoid any. It switches off the checks you are paying for. Use unknown when a type is truly uncertain, and narrow it.

  • Validate at the boundaries. Data from APIs, forms, and storage is untrusted. Parse it once at the edge, then trust the types inside.

Handle errors deliberately

Errors are part of the design, not an afterthought. Decide what should happen when things go wrong, and make that behavior visible.

  • Fail early with clear messages. A good error says what went wrong and, ideally, what to do about it.

  • Do not swallow exceptions. An empty catch block hides problems until they become mysteries.

  • Distinguish expected failures, like invalid input, from unexpected ones, like a crashed dependency, and handle each appropriately.

  • Return early to avoid deep nesting.

TypeScript
function getDiscount(user: User | null) {
  if (!user) return 0;
  if (!user.isMember) return 0;
  return user.membershipTier === "gold" ? 0.2 : 0.1;
}

Guard clauses keep the main path flat and easy to follow.

Comment the why, not the what

Code shows what is happening. Comments should explain why. A comment that restates the code adds noise and tends to go stale.

TypeScript
// Bad: increments the counter
count++;

// Good: the API paginates at 100 items, so we must request page by page
// or we silently lose everything past the first page.

Write comments for the things the code cannot say: business rules, surprising constraints, links to the issue that explains a workaround, and trade-offs you considered. If you find yourself writing a long comment to explain confusing code, try rewriting the code first.

Avoid premature abstraction

It is tempting to generalize early. You see two similar pieces of code and extract a flexible helper with options for every case. Then a third case arrives that does not quite fit, and the abstraction becomes a tangle of flags.

A useful rule: tolerate duplication until you see the same pattern three times, and until you understand what truly varies. Duplication is cheap to fix. The wrong abstraction is expensive, because everyone builds on top of it.

Prefer simple, boring solutions. Clever code impresses for a moment and costs the team for years.

Write tests that protect behavior

Good tests give you the confidence to change code. They are most valuable when they describe what the system should do rather than how it does it internally.

  • Test behavior and outputs, not private implementation details, so refactoring does not break them.

  • Give tests names that read like specifications: rejects an expired discount code.

  • Cover the edges: empty lists, missing values, boundaries, and failure paths.

  • Keep tests fast and independent so people actually run them.

You do not need one hundred percent coverage. You need coverage where a bug would hurt.

Make small, reviewable changes

Large changes are hard to review and risky to ship. Break work into small steps that each leave the code working. Commit with messages that explain the reason for a change, not just the content.

Small pull requests get better reviews, merge faster, and are easier to roll back. If a change is big, ask whether a refactor can land first on its own, followed by the feature.

Leave the code a little better

You will not fix everything, and you do not need to. The boy-scout rule works well in practice: whenever you touch a file, leave it slightly cleaner than you found it. Rename one confusing variable, delete one dead branch, add one missing test. Over months, those tiny improvements compound into a codebase that is pleasant to work in.

Final thoughts

Good code is not about showing off. It is about empathy for the next person who must understand it, who is quite often you. Choose clear names, keep functions focused, lean on types, handle errors on purpose, and prefer simplicity to cleverness. None of these habits is difficult, but practiced consistently, they are what separate code that merely works from code that lasts.

Written by

Software Engineer