Anti-patterns là solutions thoạt nhìn hợp lý nhưng thực ra gây hại về lâu dài.
- God Object/Class: một class biết và làm quá nhiều — vi phạm SRP, khó test, bottleneck khi scale
- Spaghetti Code: logic phân tán, tangled dependencies, không có structure rõ ràng
- Golden Hammer: dùng quen một tool/pattern cho mọi vấn đề dù không phù hợp
- Premature Optimization: tối ưu trước khi có evidence về bottleneck — lãng phí thời gian, tăng complexity
- Copy-Paste Programming: vi phạm DRY, bug fix ở một chỗ không fix chỗ khác
- Magic Numbers/Strings: hardcode
if (status === 3)thay vìif (status === OrderStatus.SHIPPED) - Shotgun Surgery: một thay đổi require sửa nhiều class nhỏ — ngược lại God Object
- Callback Hell trong JavaScript: Promise chain và async/await giải quyết
Nhận biết: code smell là dấu hiệu sớm của anti-pattern.
Anti-patterns are solutions that seem reasonable at first but cause long-term harm.
The most common ones:
- God Object/Class: a class that knows and does too much — violates SRP, hard to test, becomes a bottleneck at scale;
- Spaghetti Code: scattered logic, tangled dependencies, no clear structure — usually caused by lack of planning;
- Golden Hammer: defaulting to a familiar tool or pattern for every problem even when it doesn't fit (e.g., using Redis for all caching even when in-memory would do);
- Premature Optimization: optimizing before there is evidence of a bottleneck — wastes time and adds complexity;
- Copy-Paste Programming: violates DRY — a bug fixed in one place goes unfixed in all the copies;
- Magic Numbers/Strings: hardcoding
if (status === 3)instead ofif (status === OrderStatus.SHIPPED); - Shotgun Surgery: one change requires edits across many small classes — the opposite of God Object;
- Callback Hell in JavaScript: resolved by Promise chaining and async/await
Recognition tip: code smells are early warning signs of an anti-pattern taking hold.