Skip to content
Architecture & TypesChapter 21 / 57Advanced6 min read

Following Existing Patterns

Codebase conventions and consistency. Find the existing pattern before writing new code, pick the majority pattern and avoid duplicate functions or libraries.

On this page

1. What is this practice?

Every codebase has its own language: how data is fetched, how errors are returned, which library formats dates. Learning that language and writing in it is the heart of this practice.

It has two parts. First, consistency: use the pattern that is everywhere. Second, reuse before you add: extend a function that already exists instead of creating a similar new one.

2. Why does it matter?

A real story: the codebase had two ways to fetch data. The new typed client was in 40 files, and the old raw fetch was in 3 old files. A new developer copied the nearest old file. Now the bug of a forgotten auth header showed up in one more place.

Another story: the project already had date-fns. Someone installed moment for one feature. The bundle grew and dates started appearing in different formats on two screens.

Copy whatever is nearestFollow the representative pattern
An old or deprecated pattern spreadsThe majority and current pattern grows
Three ways to do one job, every bug fixed three timesOne way, one fix
A similar second function, and the two drift apartOne function extended, one behavior
Two libraries for one jobThe existing library, a smaller bundle
The reviewer has to check by handThe PR says what was reused and why

3. When to follow it

LevelHow importantWhy
Learning projectBuild the habit of keeping one pattern in your own codeEven in a solo project, three ways of doing one thing cause confusion
Production codeA must. Search first, code laterIn a team codebase, every new way adds to everyone's load
Senior levelTo change a pattern, plan the full migration and record the decisionA "better" way in one file only creates a third pattern

4. Bad vs good code

Bad: copy the nearest file, and a new function

ts
export async function loadInvoices() {
  const response = await fetch("/api/invoices");

  return response.json();
}
ts
export async function activeUsersForTeam(teamId: string) {
  return db.user.findMany({ where: { teamId, status: "active" } });
}

The first one uses raw fetch even though the project has a typed client. The second is a new function even though list() already accepts filters.

Good: search first, then the majority

bash
rg "apiClient\." src -l | wc -l
rg "await fetch\(" src -l
rg "export async function" src/lib/users

The first command shows 40 files, the second shows 3 old ones. The decision is clear.

ts
import { apiClient } from "@/lib/api/client/client";

import type { Invoice } from "./invoices.types";

export function loadInvoices() {
  return apiClient.get<Invoice[]>("/api/invoices");
}
ts
const members = await list({ teamId, status: USER_STATUS.active });

What is the difference?

ThingBadGood
FetchRaw fetch from the nearest old fileThe majority's typed client
Users queryA new near-duplicate functionReuse the existing list()
DecisionA guessThe numbers from a search
PRSays nothingSays: reused list(), no new function

5. How to apply it

Before writing code
Look for a function that does the same job on the same entity
Count how many ways this job is done in the codebase
Pick the majority or the documented decision
Extend or call the existing function
In the PR, say what was reused or why nothing fit

The same goes before adding a new dependency: does package.json already have a library for this job? Does it work with our framework version? One library per job.

If two ways are equally common, or the majority pattern is old and bad, do not guess. Ask the team or check the decision records. Whatever you choose, write one line in the PR about why.

6. Exceptions and common mistakes

If the existing pattern is truly bad (a security problem, a deprecated API), copying it is wrong. But quietly bringing in a new way in one file is wrong too. The right path: raise the problem, plan a migration, and move the whole codebase together.

Some teams follow the "boy scout rule": leave every place you pass a little better. That is good as long as each small improvement does not split the codebase into a new pattern. The trade-off: consistency sometimes means living with the old pattern until there is time for a full migration.

Mistake: Treating the nearest file as the standard

Why? That file may be old, deprecated or the only exception. Copying it spreads the mistake.

Better: Search how many places use this pattern. Follow the majority and current way.

Mistake: Writing a new function instead of looking, because you were in a hurry

Why? Two similar functions drift apart over time and one rule ends up in two places.

Better: Search the service for the same verb and the same entity. Extend it if found, and say so in the PR if not.

Mistake: Using your own favorite new way in one feature

Why? The codebase gets one more pattern and the next developer does not know which to follow.

Better: If you want a new way, first agree on it with the team, then migrate everything, then record it.

7. What to learn before and after

Suggested order
Project Structure
Following Existing Patterns
Refactoring Safely
Documentation & Decision Records

Final quick revision

CheckYes / No
Search before codeYes
Treating the nearest file as the standardNo
Picked the majority patternYes
A near-duplicate functionNo
A second library for one jobNo
Wrote the reuse or the reason in the PRYes