Keep Your Methods Private and your APIs Minimal

Tuesday, November 11th, 2008

I was reminded once more today just how important it is to write minimal APIs that don’t expose more than they have to. Briefly I had code like this:

 private boolean flag;

 public boolean getFlag() {
   return this.flag;
 }

  public boolean setFlag(boolean value);
    this.flag = value;
  }

Pretty boilerplate stuff, I think you’ll agree.

However I noticed that after some refactoring that merged a couple of classes I was now only calling getFoo() from within the same class (or at least I thought I was) so I marked it private. Eclipse promptly warned me that the method was unused so I deleted it. Then Eclipse warned me the field was unread. That seemed wrong so I looked closer and yep, it was a bug. The feature the flag was supposed to control was always on. During the refactoring I had failed to move the use of the flag field into the new class. I added a test to catch this, and fixed the problem.

What’s interesting about this example is that I found the bug only because I was aggressively minimizing the non-private parts of my API. The less public API a class has, the fewer places there are for bugs to hide. The less public API there is, the easier it is for analyzers–static, dynamic, and human–to detect problems.
(more…)

Spot the Bug Part 2

Tuesday, April 1st, 2008

Another future exam question: Identify the intermediate programming error in the following screenshot from a major web standards organization:

28 March 2008: Basic XML Schema Patterns for Databinding Version 1.0 - Last Call Ends NaN

What likely happened here, and how could it have been avoided?

The Ten Commandments of Unicode

Friday, March 7th, 2008

1. I am Unicode, thy character set. Thou shalt have no other character sets before me.

2. Thou shalt carefully specify the character encoding and the character set whenever reading a text file.

3. Thou shalt not refer to any 8-bit character set as “ASCII”.

4. Thou shalt ensure that all string handling functions fully support characters from beyond the Basic Multilingual Plane. Thou shalt not refer to Unicode as a two-byte character set.

5. Thou shalt plan for additions of future characters to Unicode.

6. Thou shalt count and index Unicode characters, not UTF-16 code points.

7. Thou shalt use UTF-8 as the preferred encoding wherever possible.

8. Thou shalt generate all text in Normalization Form C whenever possible.

9. Thou shalt avoid deprecated characters.

10. Thou shalt not enter the private use area.

Spot the Bug

Sunday, February 10th, 2008

A future exam question: Identify the elementary programming error in the following actual output from a real web store.

Bonus credit: describe both the quick emergency fix for the problem, and the longterm fix for the problem.

Greetings from CellularFactory.com.

We thought you'd like to know that we shipped your items, and that this completes 
your order. Your order number is ###### Please keep this number for any future 
requests

You can track the status of this order, and all your orders, online by visiting our 
page at http://www.CellularFactory.com/help/shipping.jsp

The following items have been shipped to you by CellularFactory.com:
---------------------------------------------------------------------
Qty      Item                   Price       Shipped      Subtotal
---------------------------------------------------------------------
1      Travel Charger                   5.89      2008-02-09      5.89
---------------------------------------------------------------------

Shipped via USPS (estimated arrival date: about 4-6 days after)
---------------------------------------------------------------------
Item Subtotal
5.89
Shipping & Handling:
3.99
Total:
9.879999999999999
--------------------------------------------------------------------

This shipment was sent to:

(more…)

Operator Overloading: Trickier Than it Looks

Monday, October 8th, 2007

In an illuminating post (formerly at http://www.jroller.com/scolebourne/ but the domain was taken over by spammers) Stephen Colebourne demonstrates exactly why operator overloading is a bad idea. Now mind, you, he doesn’t think that’s what he’s doing. He actually believes he’s making the case for operator overloading. However the mistakes he makes are exactly the sort of mistakes likely to be made by almost anyone without at least an undergraduate degree in mathematics who tries to overload +, -, *, and especially /. If you don’t know the difference between a group, a ring, and a field, you have no business overloading operators.

Now I’m not saying that one has to take a course in abstract algebra before you can be a competent programmer. You don’t as long as the language you program in doesn’t support operator overloading (or as long as you’re wise enough not to use it if it does). However since most programmers are didn’t get beyond ODEs in college (if indeed they got that far–some of my comp sci friends struggled mightily with calculus and had to retake it repeatedly), one can’t responsibly design a language that requires mathematical sophistication in the 99th percentile for proper use. Probably one should stop with high school algebra.

Features that are guaranteed to be misused should be eliminated. In the specific case of operator overloading, I daresay, the feature will be misused far more often than it will be used properly. There are only about a dozen or so cases where operator overloading is called for, and only one is even remotely common. That one is complex arithmetic, and this single use case could be better handled by adding a complex type to the language. Arguably BigDecimal and BigInteger are a second and a third. The remaining cases are so incredibly esoteric that only a mathematician or an occasional physicist could possibly be interested in them. Can anyone name even one?