Skip to content
Shell
6.8 KB
Raw Blame

ANTI_PATTERNS.md

What to Avoid

These patterns seem like good ideas. They're not.


Code Anti-Patterns

The God Component

What it looks like:

function Dashboard() {
  // 500+ lines
  // Fetches 5 different data sources
  // Manages 12 pieces of state
  // Contains all business logic
  // Renders everything
}

Why it's bad: Impossible to test, understand, or modify without breaking something.

What to do instead: Break into smaller components. Container/presenter pattern. Extract hooks for logic.


Prop Drilling Hell

What it looks like:

<App user={user}>
  <Layout user={user}>
    <Sidebar user={user}>
      <UserMenu user={user} />  // Finally used here
    </Sidebar>
  </Layout>
</App>

Why it's bad: Every component in the chain knows about data it doesn't use. Changes cascade.

What to do instead: Context for truly global state. Composition patterns. Component props slicing.


useEffect for Everything

What it looks like:

useEffect(() => {
  setDerivedValue(computeFromProps(props));
}, [props]);

useEffect(() => {
  if (condition) {
    doSomething();
  }
}, [condition]);

Why it's bad: Effects are for synchronizing with external systems, not for reacting to state changes. Creates render loops, stale closures.

What to do instead:

  • Derived state? Compute during render.
  • Event response? Handle in event handler.
  • External sync? That's what effects are for.

Boolean Blindness

What it looks like:

function createUser(name: string, isAdmin: boolean, isActive: boolean, sendEmail: boolean)

createUser("Eddie", true, false, true); // What do these mean?

Why it's bad: Call sites are unreadable. Easy to swap arguments.

What to do instead:

function createUser(options: {
  name: string;
  role: 'admin' | 'user';
  status: 'active' | 'inactive';
  notifications: 'email' | 'none';
})

createUser({ name: "Eddie", role: 'admin', status: 'inactive', notifications: 'email' });

String Typing

What it looks like:

function handleStatus(status: string) {
  if (status === "pending") { ... }
  if (status === "pendnig") { ... }  // Typo - no error!
}

Why it's bad: No autocomplete, no typo protection, no exhaustiveness checking.

What to do instead:

type Status = 'pending' | 'active' | 'complete';

function handleStatus(status: Status) {
  // TypeScript will catch typos and ensure all cases handled
}

Copy-Paste Inheritance

What it looks like:

// In ComponentA
const handleSubmit = async () => {
  setLoading(true);
  try { await api.submit(data); }
  catch (e) { setError(e); }
  finally { setLoading(false); }
};

// In ComponentB - exact same code
const handleSubmit = async () => {
  setLoading(true);
  try { await api.submit(data); }
  catch (e) { setError(e); }
  finally { setLoading(false); }
};

Why it's bad: Bug in one place means bug in all copied places. Changes require hunting down all copies.

What to do instead: Extract shared logic into hooks, utilities, or shared components.


Architecture Anti-Patterns

Premature Abstraction

What it looks like: Creating a "flexible" system before you understand the use cases.

Why it's bad: You'll abstract the wrong things. The abstraction becomes a constraint.

What to do instead: Write concrete code first. Abstract after you see the pattern 3 times.

"Duplication is far cheaper than the wrong abstraction." - Sandi Metz


The Junk Drawer

What it looks like:

/utils
  helpers.ts      // 2000 lines
  misc.ts         // "miscellaneous" functions
  common.ts       // used by... everything?

Why it's bad: No discoverability. Everything depends on everything. Can't delete anything.

What to do instead: Organize by domain or feature. Small, focused modules. Clear names.


Config Soup

What it looks like:

const config = {
  API_URL: process.env.API_URL || process.env.NEXT_PUBLIC_API_URL || 'http://localhost',
  DEBUG: process.env.DEBUG === 'true' || process.env.NODE_ENV === 'development',
  // 50 more like this
}

Why it's bad: Hard to know what's required vs optional. Different in every environment. Bugs hide here.

What to do instead: Validate config at startup. Fail fast on missing required values. Type your config.


The Database as Message Queue

What it looks like: Polling a "jobs" table every second to find work.

Why it's bad: Inefficient. Doesn't scale. Race conditions. DB load.

What to do instead: Use a real queue (Redis, SQS, etc.) or event system when you need async processing.


The Distributed Monolith

What it looks like: Microservices that all deploy together and call each other synchronously.

Why it's bad: All the complexity of distributed systems, none of the benefits.

What to do instead: If everything changes together, it's a monolith. Embrace it. Split when you feel specific pain.


Process Anti-Patterns

The Infinite Refactor

What it looks like: "Let me just clean this up before we move on..."

Why it's bad: Refactoring without shipping is gardening without harvesting.

What to do instead: Ship first. Refactor when you're going back to that code anyway.


The Perfect PR

What it looks like: 2000-line PR that rewrites everything "properly."

Why it's bad: Impossible to review. High risk of bugs. Blocks other work.

What to do instead: Small PRs. Ship incremental improvements. Stack PRs if needed.


The TODO Graveyard

What it looks like:

// TODO: fix this hack
// TODO: handle edge case
// TODO: refactor later
// TODO from 2019: still here

Why it's bad: TODOs are good intentions that rarely become actions.

What to do instead: If it matters, create an issue. If it doesn't, delete the TODO.


Comment-Driven Development

What it looks like:

// First we get the user
const user = getUser();
// Then we check if they're admin
if (user.isAdmin) {
  // If admin, show admin panel
  showAdminPanel();
}

Why it's bad: Comments that explain WHAT code does are just noise. Comments go stale.

What to do instead: Write self-documenting code. Comments explain WHY, not WHAT.


Adding New Anti-Patterns

When you encounter a pattern to avoid:

### [Anti-Pattern Name]

**What it looks like**: [Example code or description]

**Why it's bad**: [The actual problem it causes]

**What to do instead**: [Better approach]

**Encountered in**: [Project where you learned this]

Remember

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." - Martin Fowler

The goal isn't clever code. It's clear code that works.