Thursday, March 06, 2008

Create A Library For Someone Else

A coworker commented that a customer was "Using code in a way I never imagined ... that will teach me to write better code." He is right.

Everyone should write code for some one else. The back and forth of hammering out interfaces is just a part of it. The real learning comes from the other person doing things you never thought they would. When the complaints and bugs come back you get the chance to figure out where you went wrong.

Here are my favorites from C++ Win32 programming:
  • Misusing or not using an interface because they "didn't understand it"? "Then why didn't you ask", you mutter.
  • Passing the wrong parameters, but somehow it "just works but I don't know how", they quip.
  • Threading/Concurrency. This is the biggy. You probably never expected that object to be used on a different thread did you? Whose job was it to make this object safe, yours or mine?
  • Blocking, a corollary to the above. Your method call blocks, but the caller doesn't want it to. They almost never do.
The point here isn't to point out all of the ways people can get confused by interfaces. When you create an interface for some one else you get in the habit of considering all of these things. After the third (or is it 10th) time you add a blocking API and some one complains you will get the hint. Finally you will consider that the caller probably doesn't want to put up with that. If everyone says your interfaces are confusing, you might want to consider what you can do differently, rather than convincing them that there is nothing wrong.

Your code becomes more useful because it is easier to use. It solves more people’s problems and they look forward to using it.

In the end you become a better programmer. You find even the interface that you create just for you are more useful. "Cool, I can use that same widget X in program Y without any changes." That is when you know you have got it right.

So, all I am asking is spend 5 minutes during the design and think, "How could someone else abuse this?" It will make for a better design. Besides, who knows, you might be the one that wants to "abuse" the interface later on. Let's make sure you don't have to.

No comments: