- 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
implode($brain);
Admin
In Perl, it doesn't really matter if you don't do it the "appropriate" way, because to do any task in Perl, there are at least five ways to do it, all equally inappropriate. The only real difference is the number of years added in purgatory.
Admin
Double WTF. The code itself, and that the developer is suggesting split, which is deprecated, and isn't aware join is the alias of implode as they mention the other developer isn't aware of either.
Admin
Not really sure what this is attempting to suggest.
Any more elegant way to this? Any other language that hasn't derived from this in one way or the other?
Admin
Note: The code above is derived from R.P.'s suggestion in the HTML-comment. Better:
;(We have Perl for that.)
Admin
FORTRAN any version.
Admin
Perl written by C coders. <flashback> 1000's of lines of perl to be reviewed </flashback> Shudders, seen it before far too often
Challenge: minim Most developers have a minim of knowledge
Admin
C'mon TRWTF is PHP, $right?
Captcha: nobis ("the coder was a real noobis")
Admin
Could someone explain this:
$key=>$letter
... in the foreach?
What exactly does it do? What is the difference between $key and $letter?
Right now I only get 2/3 of the WTFs in this code, so please help me!
Admin
It breaks each character of the string into an array (unnecessary as PHP can handle the characters of a string like an array anyway), then loops through each character, using a regexp to replace non-alpha characters with an underscore. The entire block of code could be replaced with something like:
$contentName = preg_replace('/[^A-Za-z0-9]/', '_', $contentData->name);
Admin
TRWTF is that he knows what implode is rather than usig another for loop.
Admin
It splits the array into key/value pairs (PHP array indices can strings as well as integers)
Admin
Too PHP; Didn't Read
Admin
Um, no. The submitter wasn't suggesting that usage of split or join here was a good idea. The first paragraph was speaking in general, language-agnostic terms. And the whole point of this WTF isn't a lack of knowledge, it's a failure to act upon knowledge that the author of the code clearly has.
The developer who wrote this is aware of the existence of higher-level string-manipulation functions (since he's using them), but still creates this overcomplicated monstrosity to do something that could be handled with a single call to a builtin function.
Admin
It's PHP syntax for an array. $key => $letter means that the key-value pair is assigned to $key and $letter respectively. PHP lets you use anything as a key so you can do stuff like: array("apple" => "fruit", => "orange" => "Hard to Rhyme", "Potato" => "Not Mango")
Where "Apple" is the key for fruit, etc.
Admin
$result = implode($head);
Admin
Admin
I am much too lazy to want to try to implement a novel solution to most of the problems I encounter. Also, my work is too mundane to require many novel solutions.
These attitudes--essentially the converse of the Dunning-Kruger Effect--enable me to be extremely productive and to write/filch a relatively large amount of relatively low-maintenance code. Where I've perpetrated WTFs over the years it's been when I've forgotten my own incompetence.
Just remember, folks:
You're probably not especially smart compared to everyone else in your field; and
You're probably not the first person ever to try to solve whatever problem you're working on.
Admin
It's obvious - using preg_replace would be one line of code, this is obviously twelve times as much, so it must be twelve times better! At least, to the non-coder suits measuring performance by SLOC...
Admin
TRWTF is PHP's stupid naming conventions, or lack of.
Deprecating split, replacing it by preg_split. Thank you very much.
The best example is array functions: array_X X_array arrayX Xarray X
Admin
outputs:
apples :: 3 bananas :: 7
PHP iterates across every key/ value pair of the array or object and assigns the array key and value respectively inside the scope of the for loop.
Admin
We're talking about PHP. Split was deprecated in 5.3.0.
This is a community of pedants, after all.
Admin
That's because PHP is a shitty programming language and PHP programmers generally don't have any idea what is programming.
Admin
Sometimes this kind of code can come around from directly translating each line of code from another language to the closest equal function in the current language. In that case, each line of code is looked at individually, without considering the code around it.
A good example was me needing to translate a module from C++ Builder to Delphi at a particular job. I was the only C++ developer at that point, so the rest of the team needed to be able to work on it in something they were comfortable with. I didn't really know Delphi, but it wouldn't be THAT hard to port mostly API calls over. When moving it over, line by line I translated the code. It worked fine. I was happy.
Code review time comes and they tore me up one side and down another. They pointed out all sorts of framework and functions in place to do what I was doing with "cumbersome" API calls.
Admin
Admin
My guess this is a spawn of copying and pasting examples and working snippets together.
Admin
TO be fair, the PHP team is currently expending a lot of time and energy to go good and OO and also clean up their naming conventions.
The developers will have to follow suit though, and therein may lay the problem.
Admin
Admin
Don't forget #3:
Admin
I certainly can sympathize. My current project, which dropped onto my desk from on high like a flaming sack of dog shit, involves FoxPro + VB6.
Admin
I am dealing with java servlets written by girl VB programmers.
Admin
Underlined is TRWTF. In most circumstances, to boot. I've never understood why, exactly...
Admin
I just googled PHP regex replace and found this function preg_replace. Problem solved.
Admin
And this is why PHP is so fucking stupid.
I played around with it once connecting to a MySQL database. It helpfully has a "mysql_escape_string" function for, well, escaping strings. But if you look at the manual pages, you see it's deprecated for "mysql_real_escape_string". WTF!?!
If "mysql_escape_string" doesn't work, why not just fucking fix it? Instead we create a new function with a longer and stupider name. What happens when somebody decides "mysql_real_escape_string" doesn't work right? "mysql_really_really_definitely_now_working_correctly_escape_string_for_real_we_mean_it_this_time"?
Admin
Admin
PHP makes VB look normal.
Admin
I imagine it was done that way for backwards compatibility. It sucks, but being an interpreted language, your option are either make a new function and leave the existing one, or possibly break thousands of existing, running apps. Made even worse by the fact that a lot of people don't maintain their own server, using shared hosting or whatever, so they wouldn't necessarily know the changes were coming.
Personally I prefer this over the route the Python guys took of creating a whole new binary, python2, and requiring most Python users to have multiple complete copies of the interpreter installed....but yes, either method gets cumbersome if you do it too often.
Admin
Your problem was you did a translation when the spec clearly called for a port.
What? There was no spec? Well, since you were going from the nasty world of C++ to the clearly superior component-based world of Delphi, it should have been OBVIOUS to you that the rest of the developers wanted a true port.
-1 to you for not reading minds.
Admin
In this case, since we are escaping strings to be passed to a database, I think breaking insecure apps is preferable to backwards compatibility.
Admin
Admin
(I haven't used PHP in over 5 years and even then it was considered functionally retarded to use straight mysql* functions.)
Admin
Sounds like your QA team were assholes who don't understand things like business deadlines.
Admin
Admin
This is the not coolest thing I have ever read on TDWTF ever. WTF does gender have to do with anything.
Admin
Who uses the mysql extension anyway? PDO has been around for years!
Admin
PHP is just as good or bad as plain C++. Both languages have strange syntax and features you shouldn't use. If you don't know them well, you'll definitely produce a lot of WTFs when writing a program in one of those languages. But if you know what you're doing, they are great tools.
(Another similarity: Both languages are currently trying to become more "modern" and easy-to-use.)
Admin
Words three and four of the first paragraph.
Admin
If they don’t know about regexes, they might really overcomplicate some string munging .
Alternatively, if they do know about regexes, they might make their code write-only by using one when it's not really necessary for clarity or performance.
Admin
So in the foreach, $key will be the automatically generated integer index; on the assumption that $letters is a new array, it will simply count 0, 1, ...
Admin
I guess there's no more elegant way to do this (assuming "elegant" means with the least possible amount of characters). My second (or third) assumption is that there's also no more obfuscated way to do this.