Archive for the FAIL Category

Performant SQL

| July 1st, 2011

This query performs amazingly on a table with 8 million rows:

SELECT `id`, `account`, `post` FROM `subscribers` WHERE MD5( CONCAT( account, post, email ) ) = '8ada1291a95d8919ba80f2d00167b86f' LIMIT 1;

Perfectly readable

| November 23rd, 2009

I have reformatted this single line of unspaced code to make it more readable…

$commentText = preg_replace("/\n/", "", stripExcessLineBreaks(
                                 str_replace(
                                    " ", "", str_replace(
                                       "&","&",str_replace(
                                          "u0026#39;","'", str_replace(
                                             "'", "'", str_replace(
                                                "u0026amp;", "&", str_replace(
                                                   "u0026quot;", """, nl2br(
                                                      htmlspecialchars(
                                                         htmlspecialchars_decode(
                                                            stripHTML(
                                                               stripReplyText(
                                                                  $message['body'], true
                                                               )
                                                            )
                                                         ),
                                                      ENT_NOQUOTES
                                                   )
                                                )
                                             )
                                          )
                                       )
                                    )
                                 )
                              )
                           )
                        );

Remove slashes, add slashes, add slashes, remove slashes… what was I doing again? I don’t remember, oh well, add more slashes, remove some slashes, keep adding slashes.

$text = addslashes( addslashes( stripslashes( preg_replace("/\n|\r/", "", $text) ) ) );

Maintainability FAIL

| May 4th, 2009

I dare you to read this without crossing your eyes:

$commentText = preg_replace("/\n/", "", stripExcessLineBreaks(str_replace(" ", "", str_replace("&","&",str_replace("u0026#39;","'", str_replace("'", "'", str_replace("u0026amp;", "&", str_replace("u0026quot;", """, nl2br(htmlspecialchars(htmlspecialchars_decode(stripHTML(stripReplyText($message['body'], true))),ENT_NOQUOTES))))))))));

ICANHAZLINEBREAKZ?

Not that it surprises me that much, but there are some truly bizarre chunks of code in WordPressMU. Check out this hot piece of love:

$wpdb->query( "UPDATE $wpdb->usermeta SET meta_value = 'a:1:{s:" . strlen( $role ) . ":\"" . $role . "\";b:1;}' WHERE user_id = '$userid'  AND meta_key = '" . $wpdb->base_prefix . $id . "_capabilities'" );

Yeah that’s right, a manually constructed string, representing a serialized array. WTF?

Even the best fall down

| January 8th, 2009
I just wrote "if (!$retval) {". In C code. :facepalm:

From Andrei Zmievski via Twitter.

Andrei is a leader in the PHP community, spearheading PHP development and bringing us closer to PHP6.

Just goes to show that developers are human too. (Sorta.)

…and one more thing

| January 8th, 2009

Screwed by the Goog!

Google Reader Confused

I hate you, Bill Gates

| January 7th, 2009

Found on http://www.americanbaby.com/404.html (You need to disable javascript before loading to see it.)

<body onLoad="redirect()">
<!-- this is a comment
the whole thing
please disregard this
unless you're Bill GAtes and
in which case this is all your fault
-->
</html>

Ugh. Gotta love Microsoft’s inability to adhere to standards.

Also, see this: Open Letter to IE6 for 2009

return true, or… return true

| January 5th, 2009

This method is supposed to determine if a post is a draft or not. I’ll let it speak for itself.

public function __get_draft() {
    if ($this->visible == 0 && $this->archived == 0 && $this->deleted = 0) {
        return true;
    } else {
        return true;
    }
}

worlds most unnecessary loop

| December 29th, 2008

Found this bit in a model class of my own.

static public function active_product($product_type, $role_id) {
	$q = 'SELECT COUNT(*)
                FROM product
                WHERE
                    visible = 1 AND
                    (deleted = 0 OR archived = 0) AND
                    postedby_role_id = ? AND
                    product_type = ?';

        $sth = $dbh->prepare($q);
        $sth->execute($role_id, $product_type);

        $product_count = 0;
        while(list($count) = $sth->fetchrow_array()) {
            $product_count += $count;
        }

        return $product_count;
}

WTF was I smoking?