- 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
Hey, that looks like code 9-year old me would have wrote when PHP would have been around in the 80s.
Admin
This looks a lot like something I've seen before on this site.
Admin
Sorry, Remy, but your assessment is incorrect. It will not return an empty array when the ID can't be found --- that only happens when the table is empty. If the table contains any records at all, the code returns $a[$id] which means it's gonna blow because the array won't have an element with that index.
Admin
Unless I'm misreading, it looks like it returns an empty array only in the case where the SQL query returns no rows - i.e there are no countries that exist, or there was an error with the query. Otherwise it returns $a[$id] regardless of whether the country is in the list or not. I'm not sure what that generates when $id isn't an entry in $a - does it return null, or an empty string, or throw an exception?
Admin
Just yesterday I was looking at countries and codes and present you https://en.wikipedia.org/wiki/ISO_3166-1 The code they may store is the 3 digit numeric code.
Admin
DELETE FROM devs WHERE code_quality = "WTF"
Admin
Not so long ago TDWTF didn't make fun of code that was obviously written by a noob. Slow day?
Admin
Surely this is the most performant solution. Dictionaries are O(1) lookup.
Admin
Seems like a similar function would be used to populate a dropdown or some such, and this version gets the selected row.
If so, I'm kinda disappointed there's no apparent caching mechanism. List of countries hardly ever changes, seems like a great place to retrieve once and cache.
Admin
How old is this code, and is it still in use? It's using a function that hasn't been in PHP itself for YEARS. If this code is still in use, either there's a shim/polyfill (pick which ever word is best for when a function is removed from the std lib of a language) added before this code is run, or the server is running a version of PHP before 7.0 (and we're already at 8.3) and who know what security issues that is bringing along.
Admin
I've encountered code like this written by contractors who were allegedly not noobs.
Back in 2004 I started a programming job at a pretty well known and now pretty large bank in their loan origination IT department. Within that department I primarily worked on the loan origination software that the loan originators used on their desktops/laptops which was a Windows application. The application was a product called Empower which is/was essentially a stock system which could be customized for your financial institution. Sometime before I had started there it was setup and a lot of the customizations were done by contractors of whom a lot were Indian from what I gathered. I was coming in after all the dust had settled.
Now, one of the complaints from the users was that a lot of the interactions in the program were really, really slow so I was tasked with figuring that out. What I found was this exact type of code pattern all over the place. SELECT * FROM
with no conditions and then traversing the result sets looking for the one record that was needed. So yeah, huge speed increase due to fixing supposedly not noobs bad code.Admin
Also remember if this was done in the database it would be a case-insensitive comparison. The function developed, however, is not.
Admin
Ah, over fetching is not something only noobs make; I can confirm that I saw it all the time even by "seasoned" developers. Basically 10yr old devs in the body of a 40yr old ;-)
Admin
The people to make fun of here are the team who let this pass the code review stage. Or the team that doesn't have a code review stage in their processes.
That this code got written isn't the problem. That this code made it into production is.
Admin
Most likely there is no need for id column in this table, a table with just countries' names would do just fine, so this is a completely unneeded extra busywork.
Admin
Back in the day, this article would be surrounded by some made-up story / embellished with some self-titled "guru" whose code everyone thought was "untouchable", and attempts to get the submitted code fixed led to the submitter getting shut down by higher-ups.
Admin
I would not have believed that this kind of code gets into production unless I had seen it with my eyes. This kind of thing really does happen. It is done by so called expert contractor's too.
Admin
The coder obviously didn't believe in WHERE Clause -- which is a fat guy in a red suit, who gives out presents when the moon is full.
Admin
"Basically 10yr old devs in the body of a 40yr old ;-)" Or rather than someone with 20 years experience, someone with 6 months experience 40 times.
Admin
I am not originally from the CS side but came I to programming through a other STEM field.
I happened to take a lecture on databases, so I know the basic principles of designing them and efficiently querying them.
But I have to wonder how many programmers are put there in the wild that never had such a lecture and as a consequence don't see the issue with such code.
The irony of most fields is that in order to know that you're bad at it, you first need to know enough of the topic to understand why it is a field in it's own right.
Admin
At least the SELECT is not inside the loop...