Losing the will to code



  • From createEvent.php:

    echo "<option value="2009"";
    if ($the_year == "2009")
            echo " selected="true" ";
    echo ">2009";
    echo "</option>";

    echo "<option value="2010"";
    if ($the_year == "2010")
            echo " selected="true" ";
    echo ">2010";
    echo "</option>";

    echo "<option value="2011"";
    if ($the_year == "2011")
            echo " selected="true" ";
    echo ">2011";
    echo </option>";

    echo "<option value="2012"";
    if ($the_year == "2012")
            echo " selected="true" ";
    echo ">2012";
    echo "</option>";

    echo "<option value="2013"";
    if ($the_year == "2013")
            echo " selected="true" ";
    echo ">2013";
    echo "</option>";

    echo "<option value="2014"";
    if ($the_year == "2014")
            echo " selected="true" ";
    echo ">2014";
    echo "</option>";

    echo "<option value="2015"";
    if ($the_year == "2015")
            echo " selected="true" ";
    echo ">2015";
    echo "</option>";

    And if you were to look in editEvent.php, you'd find, well, that - all over again.


    OK, pretty standard so far.

    But here's TRWTF:
    Whilst doing an HTML markup validation recently (a task i later abandoned after finding HTML forms whose working depended on invalid constructions and having no time to refactor them), I found myself opening each of those files in vim, doing a find-replace-confirm and pressing "Y" repeatedly watching "true" change to "selected", while the part of me that still gives a shit looked on in abject horror.



  • @token_woman said:

    the part of me that still gives a shit
     

    The other guy sure didn't.



  • I'll bite:

    [code]
    function print_year_options($the_year, $start = 2009, $end = 2015) {
      foreach (range($start, $end) as $optyear) {
        echo sprintf('<option value="%1$d"%2$s>%1$d</option>', $optyear, ($optyear == $the_year) ? ' selected="selected"' : '');
      }
    }
    [/code]


  • @jchannell said:

    <font face="Lucida Console" size="2">echo sprintf('<option value="%1$d"%2$s>%1$d</option>', $optyear, ($optyear == $the_year) ? ' selected="selected"' : '');</font>

    Except that 1) sprintf plus echo is inefficient when you could just use printf, and 2) echo with concatenation is faster than printf:

    <font face="Lucida Console" size="2">echo '<option value="' . $optyear . '"' . (($optyear == $the_year) ? ' selected="selected"' : '') . '>' . $optyear . '</option>';</font>



  • @Mr. DOS said:

    @jchannell said:

    <font size="2" face="Lucida Console">echo sprintf('<option value="%1$d"%2$s>%1$d</option>', $optyear, ($optyear == $the_year) ? ' selected="selected"' : '');</font>

    Except that 1) sprintf plus echo is inefficient when you could just use printf, and 2) echo with concatenation is faster than printf:

    <font size="2" face="Lucida Console">echo '<option value="' . $optyear . '"' . (($optyear == $the_year) ? ' selected="selected"' : '') . '>' . $optyear . '</option>';</font>

    Why concatenate at all?

    <font size="2" face="Lucida Console">echo '<option value="', $optyear, '"', (($optyear == $the_year) ? ' selected="selected"' : ''), '>', $optyear, '</option>';</font>

    [url=http://php.net/manual/en/function.echo.php]Echo[/url] can take multiple arguments!



  • @Mr. DOS said:

    echo '<option value="' . $optyear . '"' .
    (($optyear == $the_year) ? ' selected="selected"' : '') . '>' .
    $optyear . '</option>';
     

    That's basically what I would have done if I'd had the strength of spirit. Its not rocket science - i was just venting about how you can only go on beating them until eventually you break and join them.

    @Xyro said:

    Why concatenate at all?

    <font face="Lucida Console" size="2">echo '<option value="', $optyear, '"', (($optyear == $the_year) ? ' selected="selected"' : ''), '>', $optyear, '</option>';</font>

    Echo can take multiple arguments!



    ... because a comma is actually no easier to type than a full stop ... ?

     



  • @token_woman said:

    ... because a comma is actually no easier to type than a full stop ... ?

    Depends if PHP's parser has a special case for print/echo where concatenation "." is replaced with "," given that there isn't any point concatenating strings that are only going to be output anyway.

    PHP doesn't have an accessible stringbuilder that I know of, nor does Perl – I don't know the ins and outs of optimising text handling in either (you could base The Daily WTF: The Illustrated 2011 Annual entirely on my code). PHP's replace functionality has a nifty feature where you can pass a match array and a replacement array into it, and each match is found and swapped out with its corresponding replacement in what I presume is a single pass. In Perl (which is what I use almost exclusively now) you'd have to write the much uglier equivalent:

    my $matches = join("|", @matches);
    $foo =~ s/$matches/handle_one_replace($1, %replacements)/eg;

    (Although CPAN will probably have something somewhere for doing this right.)

    I also used to get annoyed that PHP doesn't support the equivalent of a void* pointer to callbacks, but I've realised since that closures provide a more powerful, albeit far more messy alternative. I never use them in Perl, but I use them extensively in JavaScript.



  • @Daniel Beardsmore said:

    PHP doesn't have an accessible stringbuilder that I know of, nor does Perl
    @Daniel Beardsmore said:
    my $matches = join("|", @matches);

    There you go, once again, saying something and then contradicting it.  I admit, 'join' is not the same name as 'stringbuilder', but they do basically the same thing.

    Also, your equivallent won't work, for a whole bunch of reasons.

    foreach (my $i = 0; $i <= @matches && $i <= @replacements; ++$i) {
      my $match = $matches[$i];
      $match = qr/$match/;  # Make sure special characters in $match aren't inappropriately evaluated.
      $foo =~ s/$match/$replacements[$i]/g;
    }

    The PHP code basically does that, except it's all builtin code, rather than interprepiled stuff.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.