Early previewThis guide is in development. Items tagged verify are still being checked against official docs.
nebx Learn
GitHub
Menu
Home/C++/Anti-patterns
C++

C++

Zero-cost abstractions and full control over memory.

Paradigm
Multi-paradigm, OOP, generic
Typing
Static, strong-ish
Memory
Manual / RAII, no GC
Runs
Native machine code (AOT)
Concurrency
Threads, atomics, coroutines (C++20)

Anti-patterns

Solutions that look reasonable but cause problems. First the C++-specific traps, then the classics that apply everywhere.

C++-specific

Anti-pattern
Do this instead
✕ Owning raw pointers with new/delete
✓ Use std::make_unique / std::make_shared; leaks and double frees disappear.
✕ using namespace std; in headers
✓ Qualify names (std::string) or put using-declarations inside .cpp files only.
✕ Macros for constants and functions
✓ Use constexpr variables, inline functions and templates; they respect scope and types.
✕ Relying on undefined behaviour
✓ Signed overflow, out-of-bounds access and dangling references “work” until they don't. Use sanitizers and .at() while debugging.
✕ Catching exceptions by value
✓ Catch by const reference (catch (const std::exception& e)) to avoid slicing.

Universal anti-patterns

God object

One class knows and does too much. Split it by responsibility (SRP).

Spaghetti code

Tangled control flow with no structure. Refactor into small functions with clear inputs and outputs.

Golden hammer

Using one familiar tool or pattern for every problem. Pick the simplest fit.

Premature optimisation

Optimising before measuring. Profile first, then fix the real bottleneck.

Copy-paste programming

Duplicated logic drifts apart. Extract shared code once the duplication is real (rule of three).

Magic numbers and strings

Unexplained literals. Use named constants or enums.

Singleton abuse

Global state disguised as a pattern. Prefer dependency injection with a single-instance lifetime.

Lava flow

Dead or experimental code nobody dares to remove. Delete it; version control remembers.

Saved in this browser only. Part of From code to production (0/5).