Ouch --- again, bogus assumptions from forum posts
Posted by: dhj in Software Development on Sep 12, 2010
A week ago or so, I was working on some backend code for a new application we're developing. For various reasons, I was using a language called PHP which happens to be very convenient for website development, particularly if you're also leveraging a database like MySQL. You can cobble stuff together pretty quickly without too much trouble.
Now, I'm no expert with PHP and basically I just know enough to be dangerous. But I know what I need to say and so my learning process for this kind of thing is to search the web or refer to a book or two to find out how to say it with PHP.
Although it would not be my first choice for large projects, you can definitely get the job done quickly. Unfortunately, the very features that let you cobble stuff together quickly can return the favor and bite you in the ass without even breathing heavily.
I ran into a problem that took about an hour or so to figure out. The symptoms were very bizarre and intermittent (and not relevant right now) but the cause of the problem was an assignment to a variable inside a function that was supposed to be a call-by-reference parameter in that function. Unfortunately, I misspelled the variable name.
Assignment to a previously unseen variable name is absolutely fine in PHP so it's perfectly legal to write it. The behavior at the top level was strange because that assignment, guarded by a conditional inside the function, only happened occasionally.
This kind of error can be eliminated completely by requiring all variables to be declared before they can be used. In a language like Pascal (or C, C++ and numerous others), this problem would have been detected the moment one attempted to compile the program.
So, after tracking this down, I wrote a brief comment about it on Reddit, pointing out that a problem that took an hour to debug in PHP could have been solved within five seconds in Pascal.
Boy, did I get beat up! I was accused of all of the following:
- being an idiot,
- supporting a language not good for anything (Pascal),
- not using error reporting (which wouldn't have helped since what I did was not an error in PHP)
- not using best practices --- I'm still wondering what best practice would have prevented the problem!
- not using a debugger
One person suggested that if it took an hour to find a misspelled variable, then I shouldn't be writing programs at all. Sigh!
A friend of mine once said that a properly designed programming language doesn't need macros. I would add to this and suggest that if you have to write code to protect yourself from the language itself, then it's definitely not properly designed.
Don't shoot the messenger.
Unfortunately, the responses reminded me of why I wrote an earlier article a few months ago.
Please: mistrust the obvious!


Scorecerer: the musician's 