appropriate method name

Company.Project.Tests.Listeningtool.IntegrationTest.ListeningToolBrandCategoryAndMentionsReportTest.Should_Add_a_Valid_File_And_The_MentionsByBrandAndCategoryReport_Should_Have_Any_Sentiment_Equal_To_Positive()

Or, if you prefer,

Company
 .Project
  .Tests
   .Listeningtool
    .IntegrationTest
     .ListeningToolBrandCategoryAndMentionsReportTest
      .Should_Add_a_Valid_File_And_The
       _MentionsByBrandAndCategoryReport_Should_Have
        _Any_Sentiment_Equal_To_Positive()

How to use Exceptions

   $error = false;
   try
   {
    $response = $comm->sendRequestToPaymentGate($this);
   }
   catch(Exception $e)
   {
    $error = $e;
    $response = null;
   }
   // Save the response's values
   $this->loadEnrollmentResponse($response);
  }
 
  // Save the progress so transaction process can be continued in the future
  $this->save();
 
  if($error !== false)
  {
   Mage::throwException($error->getMessage());
  }

Always, ALWAYS use sub-selects!

SQL with sub-sub-sub-selects: 9 minutes

SELECT p.sku AS "Product code",
 p.annotation AS "Product Description",
  (SELECT COUNT(orders_products.fk_product_id) AS "count"
   FROM orders_products
   INNER JOIN orders 
     ON orders.id = orders_products.fk_order_id 
     AND orders.order_created >= '2008-12-01 00:00:00' 
     AND orders.order_created < = '2008-12-07 24:00:00'
   INNER JOIN products 
     ON products.id = orders_products.fk_product_id 
     AND products.sku = p.sku
   WHERE orders_products.fk_order_status_id >= 30) AS "Total number of units sold",
    (
      SELECT SUM((orders_products.price-orders_products.discount)/1.15) AS "sum"
      FROM orders_products
      INNER JOIN orders 
        ON orders.id = orders_products.fk_order_id 
        AND orders.order_created >= '2008-12-01 00:00:00'
        AND orders.order_created < = '2008-12-07 24:00:00'
      INNER JOIN products 
        ON products.id = orders_products.fk_product_id 
        AND products.sku = p.sku
      WHERE orders_products.fk_order_status_id >= 30
     ) AS "Total value"
     FROM products p
      WHERE (
        SELECT COUNT(orders_products.fk_product_id) AS "count"
        FROM orders_products
        INNER JOIN orders 
          ON orders.id = orders_products.fk_order_id 
          AND orders.order_created >= '2008-12-01 00:00:00'
          AND orders.order_created < = '2008-12-07 24:00:00'
        INNER JOIN products 
          ON products.id = orders_products.fk_product_id
          AND products.sku = p.sku
        WHERE orders_products.fk_order_status_id >= 30
      ) > 0

Inner joins for the same thing: 400ms

SELECT products.sku, products.annotation,
  COUNT(orders_products.fk_product_id) AS "count",
  SUM ((orders_products.price-orders_products.discount)/1.15) AS "sum"
  FROM orders_products
  INNER JOIN orders 
    ON orders.id = orders_products.fk_order_id 
    AND orders.order_created >= '2008-12-01 00:00:00' 
    AND orders.order_created < = '2008-12-07 24:00:00'
  INNER JOIN products 
    ON products.id = orders_products.fk_product_id
  WHERE orders_products.fk_order_status_id >= 30
  GROUP BY products.sku, products.annotation

As much as I love WordPress…

Seems that some WP developers are fans of refuctoring.

if ( ($fQ=strpos($Fid,'"'))!==false )
    $Fname = sanitize_title_with_dashes(substr( $Fid, $fQ+1, strpos($Fid,'"',$fQ+1)-$fQ-1 ));
### wrapped in <p> ?
   $p_offset = ($p_close < $p_open) ? $a-(strlen($part_content)-$p_open) : $a;

Argh. Guys, seriously, how about some objects? How about some readable variables? How about instead of nesting function calls and using ugly ternary operators… ugh. Nevermind.

transactions

function run() {
  ...
  //DONT TOUCH !!! -&gt; THIS DB TRANS STARTS, BECAUSE XYZ MODUL CONSIST OF DB::Commit() clausule
  DB::Trans();
  return $returnVal;
}

Hey guys, let’s start a transaction here and expect that no one will ever change the code in any of the other 2000 files. And if they do, I’m sure that while they’ll be editing “modul” XYZ, I’m sure they will know that transaction starts here.

Hardcoding FTW

function _getCRServices($data)
  {
    if (is_array($data)) {
      //this is ehmmmm, d*ment requirement from client
      $not_cr = array(3006, 2416, 1026, 5604, 3501);
      ...
    }
  }

Makes one wonder if there was a demented requirement, or just a demented programming technique.

OCD code?

if (!empty(fetchResult()) and isObject(fetchResult()) and is_a(fetchResult(), "objectType")) {
  $result = fetchResult();
  doSomethingElse();
}
elseif (isObject(fetchResult()) {
  $result = fetchResult();
}

I think the guy who wrote it was slightly OCD. And he definitely forgot that fetchResult() fetches results… from the database.

After optimizing aforementioned code and singleton-ifying the fetchResult(), number of queries made in one pageload went down to 300 from 800. Our code is just awesome.

Who likes ugly hacks?

According to my search, a lot of people do.

#ugly hack
self.log.info("Running script")
dict={ 'self' : self.model,
'model' : self.model,
'interpreter': self }
exec(self.script,dict)
// Ugly hack
Class.forName("tristero.util.Conduit");
Class.forName("tristero.util.Lock");
Class.forName("tristero.util.PumpListener");
Class.forName("tristero.util.StringUtils");
_label.Show();
_label.Hide(); // Ugly hack!
base.Show();
 /*
UGLY UGLY UGLY HACK !!!
*/
QFile inFile(uisFile);
if (!inFile.open(QIODevice::ReadOnly | QIODevice::Text))
return;
 
QFile outFile("/tmp/temp.ini");
if (!outFile.open(QIODevice::WriteOnly | QIODevice::Text))
return;
QTextStream in(&inFile);
QTextStream out(&outFile);
 
while (!in.atEnd())
{
QString line = in.readLine();
line.replace("\\","\\\\");
out << line << endl;
}
inFile.close();
outFile.close();
/*
END OF UGLY UGLY UGLY HACK
*/
 // ugly, ugly hack
if(adapterUse.getAdapterIfKnown()== SwaRefAdapter.class) {
programElement.annotate(XmlAttachmentRef.class);
} else {
// [RESULT]
// @XmlJavaTypeAdapter( Foo.class )
programElement.annotate2(XmlJavaTypeAdapterWriter.class).value(
adapterUse.adapterType.toType(outline,EXPOSED));
}

Of course, these doesn’t provide fun nor educational value. But one thing is obvious: ugly code is here to stay. Hopefully I’ll be able to find some more entertaining pieces next time.