Monday, February 1, 2010

Calcuating Percentiles

A simple proof-of-concept to better understand how to calculate percentiles.

public class PercentileTest
{
    public static void main(final String[] args)
    {
        final int[] scores = { 40, 50, 50, 66, 67, 67, 68, 69, 73, 75, 79, 79,
                81, 82, 82, 88, 89, 93, 93, 93, 99, 100
        };

        int outerCounter = 0;
        int repeatingCounter = 0;
        int lastScore = -1;

        for (final int s : scores) {
            if (s != lastScore) {
                outerCounter = outerCounter + 1 + repeatingCounter;
                repeatingCounter = 0;
            } else {
                repeatingCounter = repeatingCounter + 1;
            }
            final float percentile = (float) outerCounter / scores.length * 100;
            System.out.println("Score: " + s + "\tPercentile: " + percentile);
            lastScore = s;
        }
    }
}

Saturday, January 23, 2010

Method Name Guidelines

As a general rule, do you prefer:

Account findAccount(String telephoneNumber);
Account findAccount(String company, String division, String accountNumber);

or

Account findAccountByTelephoneNumber(String telephoneNumber);
Account findAccountByCompanyAndDivisionAndAccountNumber(String company, 
                                                        String division, 
                                                        String accountNumber);

Friday, January 22, 2010

Take The Intellisense Challenge

Try turning off intellisense for a day (or an hour) and realize how little (or much) you actually know!

Thursday, January 21, 2010

What do these results imply?

  • Response Time: 21801570 (iters=400000)
  • Exception Time: 281756916 (iters=400000)

// $Id$
package sandbox;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * @author Luther Baker
 */
public class ExceptionalPerformance
{
    private static final int ITERATIONS = 100000;

    public static class Response
    {
        public int code;
        public String message;

        public Response(final int code, final String message)
        {
            this.code = code;
            this.message = message;
        }
    }

    public static class TestException extends RuntimeException
    {
        private static final long serialVersionUID = 1L;

        // private final Response response;

        public int code;
        public String message;

        public TestException(final int code, final String message)
        {
            // response = new Response(code, message);
            this.code = code;
            this.message = message;
        }
    }

    public static void main(final String[] args)
    {
        // prime the pump
        final List list = new LinkedList();
        testResponse(list);
        testException(list);

        // time it
        final long start = System.nanoTime();
        testResponse(list);
        final long stop1 = System.nanoTime();
        testException(list);
        final long stop2 = System.nanoTime();

        // calculate it
        final long resTime = stop1 - start;
        final long excTime = stop2 - stop1;

        // make sure I use the results in an way 
        // such that methods are not optimized away
        final Iterator iter = list.iterator();
        int count = 0;
        while (iter.hasNext()) {
            iter.next();
            ++count;
        }

        // report it
        String res = resTime + " (iters=" + count + ")";
        System.out.println("Response Time:   " + res);
        String exc = excTime + " (iters=" + count + ")";
        System.out.println("Exception Time: " + exc);
    }

    public static void testResponse(final List list)
    {
        for (int i = 0; i < ITERATIONS; ++i) {
            final Response response = returnResponse(i);
            list.add(response.message);
        }
    }

    public static void testException(final List list)
    {
        for (int i = 0; i < ITERATIONS; ++i) {
            try {
                throwException(i);
            } catch (final TestException e) {
                // list.add(e.response.message);
                list.add(e.message);
            }
        }
    }

    public static Response returnResponse(final int id)
    {
        return new Response(id, "Message");
    }

    public static TestException throwException(final int id)
    {
        throw new TestException(id, "Message");
    }

}

Wednesday, January 20, 2010

Effective Exceptions

Do you write code that throws Exceptions for expected events?
  • Did you do it because it made your your code cleaner or easier to understand?
  • How do you think Exceptions affect performance?
  • Is using Exceptions in a well-defined, expected scenario, abusing the Exception handling mechanism?

Guidelines - XSD Elements and Attributes

What are your guidelines when defining an XML data model with respect to attributes and elements? Elements are arguably a bit more powerful - but can you specify a case where you'd prefer attributes?

Tuesday, November 10, 2009

The Case for Dependency Injection (Part 1)

Let's begin by looking at this problem from 3 perspectives:

  • assumptions
  • decisions
  • scenarios

In any code you write you make certain assumptions. You also make certain decisions and your code runs in specific scenarios. Some common scenarios include localhost development, unit testing, nightly builds, manual testing, production, maintenance, integration testing, etc … while some decisions might include what to do, when and with whom to do it.

Let's take a look at some code from the original poster's question:

    Saw saw = new HandSaw();
    SawMill sawMill = new SawMill();
    sawMill.setSaw(saw);
    sawMill.run();

Assume that this code was compiled and running in production exactly as written. Can you construct a realistic scenario where this code block would present a problem?

The Case for Dependency Injection

I noticed the following question on Stack Overflow and after about 5 minutes of drafting some ideas, I realized how complicated a good answer was.

Appreciating DI and IOR takes some perspective that I can't properly convey in a few paragraphs. Applying DI takes time. A good DI strategy can really be leveraged only when the larger family of problems it addresses is properly appreciated and understood.

How would you explain it?

Friday, November 6, 2009

Technical Docs

How many times have you tried to outline or take technical notes only to lose them. Maybe github can help you too! Feel free to browse the wikis and offer suggestions.

Thursday, November 5, 2009

Beautiful Graphs and Charts

I recently came across this very good looking charting library. Not only are the results quite professional but many of them contain several interactive elements.