Some terrible code arises out of terrible business rules which no one truly understands. Some terrible code arises from laziness, sloppiness, or the need to just get it done now.
But there’s a special class of awful code that arises for a complete misunderstanding of how the language is supposed to work. That gives us things like the loop Rasmus found:
$i = 0; for (;;) { $i++; echo "<input type="text" name="row[]"><br />"; if ($i >= 15) break; }
Semaj inherited a code-base which uses this “pattern” everywhere:
try { if (_staff == null) throw new NullReferenceException(); } catch (NullReferenceException) { _staff = _provider.GetStaff(); }
Who doesn't love using Exceptions to govern program flow?
Adam’s co-worker seems to misunderstand the purpose of an if
statement:
if (dncrptList.Contains(destName)) { dId = SymmetricMethod.Decrypto(id); } else { dId = SymmetricMethod.Decrypto(id); }
Adam also points out that Decrypto
would make an excellent name for a CS-themed super-villain.
And John found this one, which demonstrates PHP’s “flexibility”:
foreach ($category as $category) { /* do stuff */ }
The loop works just fine, but has the nasty side-effect of completely replacing the array with the last assignment to $category- the last element of the array. That wasn’t a problem until John later tried to add some features to the category listing and expected the array variable to still hold an array.