Inside SWT

Wednesday, August 01, 2007

API grows like fungus!

When you define an API, if you think you are done or have covered all the cases, you're dreaming. API grows! Fortunately, there are lots of things you can do to ensure that this can happen, without resorting to the dreaded IWhatever2's.

Consider this interface method:

public void onKeyPress(Widget sender, char keyCode, int modifiers);

What's wrong here? First off, 4-byte Unicode is coming and char's won't cut it. Secondly, suppose I want to add the concept of raw keyCode (for example, to distinguish between and unshifted 'A')? There's no place for that information. How about the concept of a forwarder (ie. this key press was forwarded to this widget from another widget) or a "doit" flag to cancel the operation? Not possible.

Fortunately, the fix is easy. The method should take an object rather than a list of parameters. The object is free to grow methods and fields without breaking the method signature.

public void onKeyPress(KeyEvent keyEvent);

Seems obvious, right? Not really. This method comes from GWT. They have shipped and cannot make these sorts of changes to this method without breaking people.

Steve

4 Comments:

  • Would it be possible to just add a new method, or would that break other stuff?

    By Anonymous Anonymous, at 5:43 PM  

  • The method is in an interface. You can't add a method without breaking the implementors.

    By Blogger Steve, at 12:24 PM  

  • If people are unable or unwilling for whatever reason to maintain their client code, they're not very likely to upgrade their library JAR's either.
    So step one: deprecate, step two: remove.

    Forever maintaining backward compatibility: Microsoft
    Graceful evolution: Apple
    Java: MS wanabe?

    ;-)

    By Blogger Maarten Meijer, at 3:39 AM  

  • Gasp ... sputter ... Apple ... graceful evolution ...

    Have you ever actually programmed the Macintosh?

    By Blogger Steve, at 7:55 AM  

Post a Comment

<< Home