Code anti-patterns: God objects, spaghetti and golden hammers

The most common code-level anti-patterns, how to recognize them and how to refactor your way out.

Key takeaways

  • An anti-pattern is a common "solution" that creates more problems than it solves.
  • Name it, measure it, then refactor in small, tested steps.
  • Most anti-patterns grow gradually, so small habits prevent them.

An anti-pattern is a solution that looks reasonable, is used often, and reliably makes things worse. Knowing their names helps teams spot them early and talk about them without blame.

#God Object

AuthBillingEmailPDFCacheLoggingOrdersReportsAppManager9,412 linesEverything depends on it, so every change is risky
One class that knows everything: every change touches it, every test needs it.

One class or module that does everything: AppManager, Utils, Helpers, CommonService.

  • Symptoms: thousands of lines, every PR touches it, merge conflicts, nobody fully understands it.
  • Fix: identify responsibilities, extract one cohesive piece at a time behind an interface, and keep tests green at every step.

#Spaghetti code

Tangled control flow: deep nesting, jumps, flags that change behavior far away, and functions that do five things.

  • Fix: guard clauses, extract functions, replace flag arguments with separate functions.
Why You Shouldn't Nest Your Code, CodeAesthetic

#Golden Hammer

"When all you have is a hammer, everything looks like a nail." Using the same favorite tool for every problem: Kubernetes for a static site, microservices for a 3-person startup, regex for parsing HTML.

  • Fix: write down the problem's actual requirements before picking a tool. Ask, "what's the simplest thing that could work?"

#Copy-paste programming

The same logic duplicated in several places, each copy slowly diverging.

  • Fix: extract shared logic once you have three copies (the Rule of Three). Two similar pieces of code may still be evolving differently.

#Magic numbers and strings

if (status === 3) or setTimeout(fn, 86400000). What does it mean?

  • Fix: named constants and enums: OrderStatus.Shipped, ONE_DAY_MS.

#Premature optimization

Making code complex for performance before measuring whether it matters.

  • Fix: write clear code, measure with a profiler, optimize the actual hot spots.

#Lava flow

Dead or mysterious code nobody dares to delete: "Don't touch that, it might be used somewhere."

  • Fix: use tooling to find unused code, add logging to confirm, then delete. Version control remembers.

#Boolean blindness and flag arguments

createUser(name, true, false, true). What do those mean?

  • Fix: options objects ({ sendWelcomeEmail: true }) or separate functions.

#How to fix anti-patterns safely

  1. Add tests around the current behavior (characterization tests).
  2. Refactor in small steps, committing often.
  3. Don't mix refactoring and features in one PR.
  4. Make it visible: track tech debt like any other work. See process anti-patterns.
Unmanaged debtPaying it down regularlyTimeCost
Unmanaged debt makes every change more expensive over time.

Keep reading

Anti-patterns

Architecture anti-patterns: distributed monoliths and big balls of mud

The structural mistakes that make systems slow to change and painful to run, and how to recover.

Anti-patterns

Process anti-patterns: death marches, feature factories and hero culture

The team and process traps that quietly destroy productivity and morale, and what healthy teams do instead.

Anti-patterns

AI anti-patterns: vibe coding, prompt-and-pray and agent sprawl

The new failure modes of AI-assisted development, and the habits that prevent them.