Working on my Big Dirty PHP Project at work, I’ve found this bit of code in many places.
Why is $categories set to an empty string, and then an array? It’s not necessary to pre-initialize a variable before setting it to another value. So why is the code there? It’s not just one case. It’s throughout the codebase, where I delete the first line whenever I find it.
The original programmer is (thankfully) no longer around to ask, but I’m guessing it’s superstition. Perhaps he had some problem that went away for an unrelated reason when he added the first line of the code. The problem is that he never considered why.
Here’s another coding horror to avoid in Perl. Ever seen a regular expression by someone who wasn’t entirely familiar with regexes and quoted everything whether it needed it or not?
The hyphen in Marcus‘s name isn’t a metacharacter, but the unsure, superstitious programmer will quote it anyway. “Eh, it doesn’t hurt anything,” he may reply, but it also demonstrates his non-mastery of regexes.
If you ever find a piece of your code where you can’t understand exactly why it works, why every single statement exists, stop and rework it until you do.