反模式是对重复出现的问题的常见但不良解决方案 —— 看起来合理但会导致不良后果(难以维护、充满缺陷或僵化的代码)的方法。识别它们可以帮助你避免常见的错误。
什么是反模式
ANTI-PATTERNS = commonly-used but COUNTERPRODUCTIVE solutions:
→ they seem like a solution but cause MORE problems (technical debt, bugs, rigidity)
→ the OPPOSITE of design patterns (proven good solutions)
→ recognizing them helps you AVOID common pitfalls
常见的代码/设计反模式
GOD OBJECT/CLASS → one class doing too much (huge, knows everything) → hard to maintain;
violates single responsibility
SPAGHETTI CODE → tangled, unstructured code with no clear flow → hard to follow/change
COPY-PASTE programming → duplicated code (violates DRY) → bugs/changes must be fixed everywhere
MAGIC NUMBERS/STRINGS → unexplained literal values → unclear, error-prone
PREMATURE OPTIMIZATION → optimizing before it's needed → wasted effort, complexity
GOLDEN HAMMER → over-using one familiar tool/pattern for everything ("when you have a hammer...")
HARDCODING → values that should be configurable baked into code
