- 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
Not a word about failing to just format the random number into the filename string? I am disappoint.
Admin
Apperantly, it dwarfs the WTF that is PHP(-documentation.) But yes, at first glance that was where I thought the article was heading. <groan>
Admin
And now you've ruined the joke.
Admin
Nah, that's actually accidentally the same instead of really code repetition. Nothing stops them from adding another image in a different path. The switch solution would still work, but the path manipulation would not...
This one is not terrible code... It is clumsy... cumbersome... but not terrible...
I guess I would have written it similarly. Would have used an array instead of a switch statement. Would have used the length of the array as the rand max... To remove the magic number that will probably break on extending or reducing it. To make adding and removing pictures easier. And make it easier to move the array into a config file or the database later... Or would have put it there right away if it was allowed. But all in all, that's passable code.
Admin
Hey, it could have been worse. At least, there are no loops to count from 1 to the random number.
Admin
You would need such a loop for the array version suggested by TrayKnots, because you can't guarantee that PHP gets the count correct or correctly stores a URL at each element.
(I've just come fresh from reading yesterday's WTF.)
Admin
The real WTF here is that this code most likely does not do what the guy making the requirement wanted it to.
Remember, what we developers mean when we say random is not the same as what the average person does. For us random means unpredictable. As in you newer know what the next image is going to be. And this code matches that.
But designers typically want something that is closer to a random shuffle. As in, they don't want the same image to show up back to back, they want to cycle through all the images before repeating etc. Stuff like that. And this code most definitively won't work like that.
So if my experience is any indicator it's very likely that this is bad code that the original customer isn't happy with either.
Admin
At least this logic is in one header file. It would be worse if the logic was duplicated on every page.
I once worked for a company, where their main app had menus and submenus hardcoded into every page. Each one had at least one item that was misaligned to the others, and every page had a different one, even ones that you'd think would be copied from another.
Admin
PHP is just using C's rand() function, at least up through PHP 7.1 when it switched to the Mersenne Twister. All of the problems and caveats listed there are the same as in C. For once, PHP isn't TRWTF here.
Admin
That is exactly the first thing I thought too. :-D
Admin
There are two wtfs not mentioned in the article: First is that PHP switch statements use "loose" equality, so even though the case statements are matching on string numbers but $pic is an actual integer, loose equality makes it still work. Second is that they seem to be completely unaware of PHP string interpolation
Admin
thanks
Addendum 2023-08-21 23:07: During my recent online escapade, I uncovered a hidden gem - a website housing an impressive collection of stock photos. These high-quality images, covering a myriad of themes and moods, have become an invaluable resource geometric pattern vector in my creative toolbox. From enhancing visual aesthetics in web design to creating eye-catching marketing materials, these stock photos are my newfound source of inspiration and impact.
Admin
Link spam bot. @editors
Speaking of which: Why is there no report function in this comment section?