• OMG (unregistered)

    implode($brain);

  • (cs)

    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.

  • corroded (unregistered)

    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.

  • noland (unregistered) in reply to Severity One
    Severity One:
    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.

    Not really sure what this is attempting to suggest.

    $name =~ s/[^\w\d]/_/g;

    Any more elegant way to this? Any other language that hasn't derived from this in one way or the other?

  • noland (unregistered) in reply to noland

    Note: The code above is derived from R.P.'s suggestion in the HTML-comment. Better:

    $name =~ s/\W/_/g;
    ;

    (We have Perl for that.)

  • rfoxmich (unregistered) in reply to noland

    FORTRAN any version.

  • Sigh..... (unregistered)

    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

  • Anom nom nom (unregistered)

    C'mon TRWTF is PHP, $right?

    Captcha: nobis ("the coder was a real noobis")

  • TheSHEEEP (unregistered)

    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!

  • Ben (unregistered) in reply to TheSHEEEP

    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);

  • dudddde (unregistered)

    TRWTF is that he knows what implode is rather than usig another for loop.

  • musicman (unregistered) in reply to TheSHEEEP

    It splits the array into key/value pairs (PHP array indices can strings as well as integers)

  • Captcha: sino (unregistered)

    Too PHP; Didn't Read

  • Cabbage (unregistered) in reply to corroded

    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.

  • Anon (unregistered) in reply to TheSHEEEP
    TheSHEEEP:
    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!

    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.

  • Joat (unregistered)

    $result = implode($head);

  • (cs) in reply to corroded
    corroded:
    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.
    Split is not deprecated. The PHP function "split" is in favour of explode or preg_split, but the concept "split" is still very much around. Most languages I know still call it "split" too.
  • 3rd Ferguson (unregistered)

    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:

    1. You're probably not especially smart compared to everyone else in your field; and

    2. You're probably not the first person ever to try to solve whatever problem you're working on.

  • Dakhran (unregistered)

    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...

  • JF (unregistered)

    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

  • Zeffr (unregistered) in reply to TheSHEEEP
    $myArray = array('apples' => 3, 'bananas'=>7);
    
    foreach($myArray as $key=>$value){
              echo "{$key} :: {$value}\n";
            }
    

    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.

  • corroded (unregistered) in reply to NMe
    NMe:
    corroded:
    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.
    Split is not deprecated. The PHP function "split" is in favour of explode or preg_split, but the concept "split" is still very much around. Most languages I know still call it "split" too.

    We're talking about PHP. Split was deprecated in 5.3.0.

    This is a community of pedants, after all.

  • Doctor (unregistered)

    That's because PHP is a shitty programming language and PHP programmers generally don't have any idea what is programming.

  • MindChild (unregistered)

    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.

  • Joby (unregistered) in reply to NMe
    Split is not deprecated. The PHP function "split" is in favour of explode or preg_split, but the concept "split" is still very much around. Most languages I know still call it "split" too.
    Oh really? See: http://php.net/manual/en/function.split.php
    Note: As of PHP 5.3.0, the regex extension is deprecated in favor of the PCRE extension. Calling this function will issue an E_DEPRECATED notice. See the list of differences for help on converting to PCRE.

    Tip split() is deprecated as of PHP 5.3.0. preg_split() is the suggested alternative to this function. If you don't require the power of regular expressions, it is faster to use explode(), which doesn't incur the overhead of the regular expression engine.

  • anon (unregistered)

    My guess this is a spawn of copying and pasting examples and working snippets together.

  • Joby (unregistered) in reply to JF

    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.

  • Brain damage (unregistered) in reply to Sigh.....
    Perl written by C coders.
    You're lucky. I once had to deal with FoxPro written by COBOL coders.
  • OH GOD IT BURNS! (unregistered) in reply to 3rd Ferguson
    3rd Ferguson:
    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:

    1. You're probably not especially smart compared to everyone else in your field; and

    2. You're probably not the first person ever to try to solve whatever problem you're working on.

    Don't forget #3:

    Brian Kernighan:
    Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
  • OH GOD IT BURNS! (unregistered) in reply to Brain damage
    Brain damage:
    You're lucky. I once had to deal with FoxPro written by COBOL coders.

    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.

  • (cs) in reply to Brain damage
    Brain damage:
    Perl written by C coders.
    You're lucky. I once had to deal with FoxPro written by COBOL coders.

    I am dealing with java servlets written by girl VB programmers.

  • Xarthaneon the Unclear (unregistered) in reply to Nagesh
    Nagesh:
    Brain damage:
    Perl written by C coders.
    You're lucky. I once had to deal with FoxPro written by COBOL coders.

    I am dealing with java servlets written by VB programmers.

    Underlined is TRWTF. In most circumstances, to boot. I've never understood why, exactly...

  • Omego2K (unregistered)

    I just googled PHP regex replace and found this function preg_replace. Problem solved.

  • Anon (unregistered) in reply to Joby
    Note: As of PHP 5.3.0, the regex extension is deprecated in favor of the PCRE extension. Calling this function will issue an E_DEPRECATED notice. See the list of differences for help on converting to PCRE.

    Tip split() is deprecated as of PHP 5.3.0. preg_split() is the suggested alternative to this function. If you don't require the power of regular expressions, it is faster to use explode(), which doesn't incur the overhead of the regular expression engine.

    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"?

  • CodeBeater (unregistered)
    <?php $string = "Not a single fuck was given that day"; $var = 0; while ($var != count($string)) { echo($string[$var]) } ?>
  • (cs) in reply to Xarthaneon the Unclear
    Xarthaneon the Unclear:
    Underlined is TRWTF. In most circumstances, to boot. I've never understood why, exactly...

    PHP makes VB look normal.

  • urza9814 (unregistered) in reply to Anon
    Anon:
    Note: As of PHP 5.3.0, the regex extension is deprecated in favor of the PCRE extension. Calling this function will issue an E_DEPRECATED notice. See the list of differences for help on converting to PCRE.

    Tip split() is deprecated as of PHP 5.3.0. preg_split() is the suggested alternative to this function. If you don't require the power of regular expressions, it is faster to use explode(), which doesn't incur the overhead of the regular expression engine.

    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"?

    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.

  • Z (unregistered) in reply to MindChild
    MindChild:
    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.

    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.

  • Anon (unregistered) in reply to urza9814
    urza9814:
    Anon:
    Note: As of PHP 5.3.0, the regex extension is deprecated in favor of the PCRE extension. Calling this function will issue an E_DEPRECATED notice. See the list of differences for help on converting to PCRE.

    Tip split() is deprecated as of PHP 5.3.0. preg_split() is the suggested alternative to this function. If you don't require the power of regular expressions, it is faster to use explode(), which doesn't incur the overhead of the regular expression engine.

    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"?

    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.

    In this case, since we are escaping strings to be passed to a database, I think breaking insecure apps is preferable to backwards compatibility.

  • 6u53ui (unregistered) in reply to corroded
    corroded:
    We're talking about PHP.
    No, the opening paragraph where "split" is mentioned is about programming in general, not PHP specifically.
  • foxyshadis (unregistered) in reply to Anon
    Anon:
    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"?

    You're pretty out of date, that was old PHP. Everything done since PHP 4.3 has been work on fixing that legacy of horrible design decisions. In modern PHP, using either of those functions would get you a swift kick in the real, you just use the database encapsulation classes which takes care of escaping for you, and get fixed first with optional flags for backwards compat when necessary.

    (I haven't used PHP in over 5 years and even then it was considered functionally retarded to use straight mysql* functions.)

  • foxyshadis (unregistered) in reply to MindChild
    MindChild:
    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.

    I don't get it, starting a port with rote translation is perfectly fine, if the whole point isn't to throw it all out and start over, especially for someone completely unfamiliar with the language. It's better to start with a quick working v1.0, then work with the experts to improve important parts than to spend months mastering a language before even beginning any work at all.

    Sounds like your QA team were assholes who don't understand things like business deadlines.

  • foxyshadis (unregistered) in reply to corroded
    corroded:
    We're talking about PHP. Split was deprecated in 5.3.0.

    This is a community of pedants, after all.

    This is a community of assholes who feel better by calling themselves pedants. To prove I'm one, can you point out where PHP is mentioned in the first paragraph? I'd hate to see your software if it makes those kind of assumptions everywhere.

  • Seriously? (unregistered) in reply to Nagesh

    This is the not coolest thing I have ever read on TDWTF ever. WTF does gender have to do with anything.

  • Anonymous (unregistered) in reply to Anon
    Anon:
    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"?

    Who uses the mysql extension anyway? PDO has been around for years!

  • Anonymous (unregistered) in reply to Anom nom nom
    Anom nom nom:
    C'mon TRWTF is PHP, $right?

    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.)

  • (cs) in reply to foxyshadis
    foxyshadis:
    To prove I'm one, can you point out where PHP is mentioned in the first paragraph? I'd hate to see your software if it makes those kind of assumptions everywhere.

    Words three and four of the first paragraph.

  • Sigivald (unregistered)

    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.

  • (cs) in reply to TheSHEEEP
    TheSHEEEP:
    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!

    I think the answers to this so far assume that you already know PHP, and since you need to ask the question I think that's a dodgy assumption. So the fuller answer is that the first line inside the first loop is a kind of "append to array" which will store the one-character substring at a numeric index in the array, generating the number automatically. (I can't remember offhand whether it's the first unused non-negative integer or whether it also depends on the number of non-integer indices which are being used).

    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, ...

  • Ollo (unregistered) in reply to noland

    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.

Leave a comment on “All the Pieces”

Log In or post as a guest

Replying to comment #:

« Return to Article