Archive for November 2007

An awesome customer service experience!

It is so rare to have an awesome customer service experience; I have to share this one.

I came home Friday night to find my computers in my office all shut off and the damn replace battery alarm beeping every five seconds on my UPS (highly annoying.) So it was time to order some new batteries, I went to APC(Canada) and ordered the replacement batteries via CDW, and then tried to figure out how to stop the infernal beeping (which took nearly 3 hours and 2 bourbons.) On Monday morning I got a call from a very nice person Natashe informing me that I had placed my order with the CDW US and not CDW Canada, but that she had canceled my order in the US and placed a new order here in Canada and that my batteries would arrive tomorrow. This is jaw dropping amazing service as far as I’m concerned, and my batteries arrived around 10am on Tuesday.

Three cheers for CDW the left hand really knows what the right hand is doing and thanks again.

Esoteric Ant Hacks

When debugging ant build scripts (fun wow,) sometimes you want to view the contents of a path reference, this handy hack does just the trick:

<echo message="${toString:pathref}"/>

It probably also works for other “classes” as well.

After a little more digging around (my ant is a little rusty) the proper way to do this is to convert your path reference to a property:

<property name="class.path" refid="class.path.ref"/>

Finally a little sugar to print out your classpath in a human readble form:

<!-- pretty print the class path so it is human readable -->
<macrodef name=\"print_classpath\">
 <attribute name=\"path\"></attribute>
 <attribute name=\"description\" default=\"=== CLASSPATH ===\"></attribute>
 <sequential>
    <echo>@{description}</echo>
    <for list=\"@{path}\" param=\"pathitem\" delimiter=\";\">
       <sequential>
          <echo>pathitem == @{pathitem}${line.separator}</echo>
       </sequential>
    </for>
    </sequential>
</macrodef>

Note you will need the for loop from ant contrib to use this macro/task

<<taskdef resource=\"net/sf/antcontrib/antlib.xml\" classpathref=\"ant.classpath.tools.lib\">

Coding Horror

Given the rate that I’m running accross these little horrors, this may become a regular post.

public static String trimLeadingZeros( String trimString )
{
         String retVal = trimString;
         boolean nonZeroFound = false;
         StringBuffer rtnString = new StringBuffer();
         if ( trimString != null ) {
                  for ( int index = 0; index < trimString.length(); index++ ) {
                                char c = trimString.charAt( index );
                                if ( c == '0' && nonZeroFound == false ) {
                                         // skip all leading zeros
                                } else {
                                         nonZeroFound = true;
                                         rtnString.append( c );
                                }
                  }
         }
         if ( rtnString.length() > 0 ) {
                  retVal = rtnString.toString();
         }

         return retVal;
}

Obviously this person doesn’t have a degree in computer science or they failed out.

  • The have an empty if statement, what you’re not capable of doing the necessary boolean algebra in your head to invert the conditional, give me a break
  • Hey lets scan every character whether you need to or not, making this O(n) where n is the total number of charaters in all strings being scanned (sheesh)
  • How many damn variables do you need to do this 5! Could we make this any more complex
  • The real shame is that the JDK doesn’t have a general strip/trim function, they have a trim whitespace, so why not generalize it.

So in about 5 minutes I put a more general  version together which O(f(x)) << O(n), in the general case my version is 2.5 times faster than the horror. For the pathalogical case my versio is only.8 time faster, and for the ideal case we’re 3 times faster. This was all done over 1 million iterations using the same string that was 20 characters long.

public static String trimLeadingChar( String trimString, char trimChar )
{
        try {
                String tempString = trimString;
                int offset = 0;
                while( tempString.indexOf( trimChar, offset ) == offset )
                        offset++;

                tempString = tempString.substring( offset );

                if( tempString.length() == offset )
                        return trimString;

                return tempString;
        } catch( NullPointerException ex ) {
                return trimString;
        }
}

I’m using a very Pythonic idom here, which is to not care about the NullPointerException, so instead of explicitly checking
for trimString == null, we just catch the excetption and do no harm. I could have made the while loop empty, but frakly I find
that a little bit vulgar.

Coding Horror

I ran across this gem today…

static public Date getYesturdaysDate() {
        Date yesterday = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24);
        return yesterday;
}

All I can say is WTF? How does something like this get through and then hang around without being cleaned up.