- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
It's surprising how often developers actually reinvent the wheel; in the worst way possible.
Admin
Isn't appending to a file literally the second lesson ever in computer science class in high school?
Admin
Watch what you're calling worse. I'll have you know my four sided right angled wheel offers superior stability when parked on inclines. And the broad flat surfaces give it much better ground pressure as well. And it can't slip in mud either.
About the only down side is that you can't turn it. But who wants to deal with rotation anyway?
Admin
Ever seen Carry On Cleo?
Admin
Do we really need to pick on PHP? It seems kind of gratuitous.
Admin
"Infamy! Infamy! They've all got it infamy!"
Admin
Square? Mine are triangular. Less corners so less bumpy.
Admin
Sure you can turn it - you just need an appropriately shaped surface. Look at Wikipedia under “square wheel.”
Admin
I'll raise you my three-sided wheel. 25% fewer bumps!
Admin
Early in my career I managed to bring a bank's (14th largest in the nation) nightly processing to a halt for a few hours with a similar piece of bad code. Branch openings were delayed by 3 hours(really bad). The systems guys tried to crucify me and have me fired, but ultimately it way deemed their error for not putting in guard rails to kill a process cascading out of control. Lesson learned.
Admin
Ultimately, this kind of thing could be written in almost any programming language that has strings constructed by dynamic allocation, like, for example, C++ or Java or Python or whatever.
And it may well be worse than the article implies, since there are, in fact, four concatenations that involve a copy of the whole existing contents, unless PHP's string concatenator is right-associative. (i.e. unless
a . b . c
is equivalent toa . (b . c)
as opposed to(a . b) .c
)Admin
konnichimade , we need to keep kicking it until it's dead. It'll take a while, but we need to. My daughter had a good idea for a website and I put together for her. She turned to a friend of hers to market the idea (I'm not much good at that) and we had some discussions of support websites. Her friend wanted to put it all together with WordPress. "What's wrong with WordPress and PHP? It's easy and fun! I've made web pages with it myself!" I about wanted to slap him. I'm the guy who's had to come in behind and clean up the messes after people who thought a toy could be used for serious business. I have to remind people that PHP actually stands for "Personal Home Page." It was a toy for a guy and his friends and it got out of hand.
Admin
Seems to me it is not PHP that is being picked on. Other languages have similar APIs to read a whole file into memory at once. This could have been a WTF in any one of a number of languages.
Admin
You want to kill the language because people can write crap in it? Time to kill all programming languages then.
Admin
I am in favor of this. Every programming language is bad, and computers were a mistake.
Admin
Maybe if PHP didn't have the convenient file_get_contents and file_put_contents functions, so you have to do your own open/read/close, the programmer wouldn't have been lured into writing it this way.
But it feels wrong to blame PHP for this, when those functions are especially nice in other contexts. Not to mention that they could have used file_put_contents with the FILE_APPEND flag.
Admin
Right on! FILE_APPEND would do it.
Admin
PHP is a poorly designed Perl wannabe. I'll just leave it at that, for fear of angering any of my Perl friends. :)
Admin
This is especially egregious because PHP makes it very very easy to append a line to a file, such as for logging. I believe there's even a special call that takes care of all the locking etc. etc.
Admin
What? No, show me teh codes to make a colourful explosion or write my name in super big letters. Learning how to open files is BORRRRRRING.
Admin
PHP programs do a lot of string concatenation; it's one of the things the implementation goes to a lot of effort to optimise. In this particular case it will recognise that the huge string in $content doesn't need to be kept intact for anything else after having the additional line added, so it will reuse the existing allocation (instead of copying the entire thing).
Then again, in this particular case, you'd append to the file, of course.
Admin
That code was hilarious. Seriously. Can we all hope it was written as a joke for April Fool's Day?
It's got to be a joke, right? Right?
Admin
Hope is the first step on the road to disappointment.
Admin
The worst part of this approach (apart from the ghastly performance) is that this can lose log messages if you're in a multithreaded or multiprocess environment. Properly appending typically won't.
Admin
Hey, with this most excellent aLOGorithm they can (but didn't) put the most recent log entry first in the file, instead of last. No extra cost, but I bet that could be an enterprise-level feature. Gotta bug like that, you gotta buy it a suit.