WTF of the Week

Well, if you've ever dug into someone else's code trying to debug a problem, you've no doubt and a moment where you wondered what they were thinking at the time.

My WTF of the week stems from a Drupal module I'm using and how it manipulates some custom SQL.  First, a little background for the non Drupal developers out there.

The Drupal CMS provides a basic database abstraction layer for it's developers, like any decent framework should.  Furthermore as it's common for different circumstances (or development styles) to require returned data be either Arrays or Objects, Drupal provides two function calls to cover the bases: db_fetch_array() and db_fetch_object().

Well, today I found this gem in the code:

$account = (object) db_fetch_array(
      db_query(
        "SELECT * FROM {users}
            INNER JOIN {authmap} ON {users}.uid = {authmap}.uid
                AND  {authmap}.module = 'openid'
            WHERE {authmap}.authname = '%s'", $openid));

What?

So we run our query, return the results as an array, and then explicitly cast that array to an object?

Maybe I'm missing some point of micro-optimization technique here.  Or maybe this saves a few K of RAM on the server, but I'm not real confident.  This is why it's a good idea to become familiar with the API you are using before you go and publish something like this that has people scratching their heads.

 

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.