Cargo Cult Programming
Copying code without understanding how it works.
“Ritualistic inclusion of code or structures that serve no real purpose.” – The Jargon File
Ritualistic coding.
Signs of Use (Symptoms)
- Blocks of code copied from Stack Overflow with comments intact.
- initializing variables that are immediately overwritten.
- Checking for conditions that can never happen “just to be safe”.
Why it is bad (Consequences)
- Bloat: Unnecessary lines of code.
- Bugs: If you don’t know what it does, you can’t debug it when it breaks.
- Misinformation: Readers assume the code is necessary.
Why it happens (Root Cause)
Inexperience or desperation. “I saw this in another project/tutorial, so I added it.”
When it might be okay (Exceptions)
- Almost never. Always understand what you commit.
Explanation
Problem
A developer adds if (true) { ... } because they saw it once. Or repeats a config block they don’t understand.
The Flaw
The code works by accident, not design.
Real world analogy
Indigenous islanders building wooden “planes” hoping to attract supply drops (the origin of the term). They mimicked the form but didn’t understand the function.
Refactoring Solution
- Delete unnecessary code.
- Research: Read the documentation to understand why code is needed.
Pros and Cons (of the Antipattern)
| Pros (Why people do it) | Cons (The price you pay) |
|---|---|
Comparison
- Related Antipatterns: Voodoo Chicken Coding.
- Related Principles: Intentional Programming.
Code example
Typescript
Bad (The Antipattern)
Good (The Fix)
PHP
Bad (The Antipattern)
Good (The Fix)