Practical API Design
I wrote about API design guidelines before, collecting links, resources, papers and hoping someone would write a book on the subject.
The wait is over! Jaroslav Toulash published a book on Practical API Design!
Take a look at the book’s accompanying website for more details.
I’m waiting for my copy.
Violating Java's privacy 3
I found myself in the need to invoke a private method of a Java class that was out of my control. I really needed it.
So, I went ahead and violated the method’s privacy declaration via
reflection. You can (under certain circumstances) invoke methods which
are declared as private using the Reflection APIs (java.lang.reflect).
But before using reflection, I created two classes:
class A {
public String method1() {
return "Hello World!";
}
}
class B {
public static void main(String[] args) {
A a = new A();
System.out.println(a.method1());
}
}
They compiled, and java B said “Hello World!” as expected.
Then, I made A’s method private and recompiled A. And I run java B again. Nothing changed. It just worked again.
That cannot be right. This would mean you can handcraft a class with the
same name and methods as the original one but making everything
public. Then you could use this class only at compilation time,
allowing your code to call any method. (Or, one could modify
the Java compiler to ignore access declarations altogether).
On API Design Guidelines 7
Update: Good news! Jaroslav Toulash emailed me that he published a book on Practical API Design !!!
Looks like Brian McAllister may be preparing a talk on Designing Elegant APIs.
I've been very interested in good API design for a long time. But I could never find a single book on the subject. Many design and programming books provide good advice and guidelines that are essential for designing good APIs, but none of them tackles the matter directly.
I reviewed "Interface Oriented Programming" a few months back with disappointment. It may not be a bad book, but I felt it was quite basic and superficial. May be my expectations were too high, and too focused on API design.
Over time, I collected some links on the subject and shared some with Brian:
Best Practices in Javascript Library Design (via John Resig on JavaScript API Design) - A good presentation given by the author of JQuery.
API: Design Matters - Article by Michi Henning, ZeroC, for ACM Queue magazine.
How to Design a Good API and Why it Matters - Excellent deck from Joshua Bloch. There's also a video of his presentation at JavaPolis 2005.
Interface Design, Best Practices in Object-Oriented API Design in Java, by Bill Venners - This one is a Book in progress!. Unfortunately, the last updates seem to be from 2002 or so. I'm not sure if the project is still alive. Anyway, it contains many good articles.
How To Design a (module) API - A great page on good design practices for writing APIs. It's tailored for NetBeans module writers, but is still pretty general and gives very valuable guidelines.
How to Write APIs That Will Stand the Test of Time - A session I attended at JavaOne 2006. Presented by Tim Boudreau and Jaroslav Tulach, members of the NetBeans team. It focuses on API evolution and compatibility, but also covers usability and other guidelines. I guess they are the main guys behind NetBeans' How To Design a (module) API page linked above.
I had the chance to speak with Jaroslav after his session and suggested he should write a book on this topic. I emailed him the references I had, and he said he would try to draft something by the end of year.
API Usability - Focused on making APIs easy to use. The article applies general usability principles (commonly used in GUI design) to the design of APIs.
Measuring API Usability - An interesting article by a usability engineer at Microsoft, published on Dr. Dobb's. It favors the use of scenario-based design to achieve usable APIs, and explains the use of their "cognitive dimensions" framework for measuring API usability. He also blogs about API usability.
Krzysztof's Laws of API Design - From another Microsoft blogger on API design.
Java API Design Guidelines - A good article from a developer who was also surprised by the lack of a book about API design. He collected some advice and additional links.
XOM Design Principles - Some design "principles" followed by XOM (an XML parsing library).
The Most Important Design Guideline? - A short article by C++ guru Scott Meyers.
Humane Interface, Minimal Interface, Designed Inheritance, DSL Boundary, Duck Interface, Fluent Interface, Public vs. Published Interfaces - Some of the many great writings from Martin Fowler. These selection is on specific topics that are relevant to API design. Most of them are quite recent.
Programmers are People, Too - An article by Ken Arnold for ACM Queue magazine: "Programming language and API designers can learn a lot from the field of human-factors design."
So, let's hope Brian gives his talk at a big conference, signs a contract with a big publisher and fills the void.
AntDoclet 1.1
I just released a new version of AntDoclet.
No major changes. Now the comments of inherited methods are properly extracted from the parent class comments.
Thanks to Daniel Lindner for this fix.
GotAPI?
Did you know about gotAPI.com?
It provides as-you-type lookups of reference documentation for HTML, CSS, Java, Perl and other programming languages and APIs.
AntDoclet
After nearly two years of procrastination, I finally decided to spend some time on wrapping things up and putting AntDoclet online for public consumption.
AntDoclet is a little tool for documenting Ant Tasks. It automatically generates HTML and LaTeX documentation from the source code of your Tasks.
I wrote it initially in January 2004, for documenting the Ant tasks provided with the FuegoBPM product. Recently, I needed to improve it a bit, and decided to make it public.
Thanks to Fuego Inc. –my employer– who allowed me to release AntDoclet.
Graphical Thread Dumps 10
I am surprised by the high number of Java developers I meet that do not know what a Java Thread Dump is or how to generate one. I find it a very powerful tool, and it is always available as part of the JVM. I haven’t played much with Java 5 yet, but it comes with jstack, a new tool that makes it easier to generate thread dumps.
Earlier this year, I was working on a load test for for a well-known airline. We were tunning the environment all we could, monitoring and profiling to know where to focus our optimization efforts. The solution involved a fairly high stack: Apache httpd, WebSphere, FuegoBPM, Tibco messaging, Oracle RAC.
The system was holding load pretty well up to a certain point in which it immediatly halted and stopped processing new requests. Every time we run the load testing scripts we experienced the same symptoms. Not even the official testers –with allegedly powerful testing and monitoring tools– were able to identify the cause of the problem.
So, I decided to get a few Thread Dumps of WebSphere’s JVM. On Unix, you do ”kill -3 <pid>” and the dump goes to WebSphere’s native_stdout.log. We
inspected the dumps but couldn’t identify dead-locks or any other
obvious anomaly, although the answer was right before our eyes.
Micro and Blind Optimizations 1
Yesterday, a good friend of mine and ex-coworker contacted to me to share his frustration.
(he hates to be called “Polino”, so I won’t.. doh!)
He finished a software solution for a customer, and now an expert is reviewing his Java code.
The expert code reviewer insists on small performance optimizations, but he is way off target. He wants to micro-optimize, and to do it blindly.
For example, he reported that the following code was doing “inefficient String concatenations”:
String myString = "Some text here "+
"Some text there "+
"Some more... ";
And that this was an “inefficient way of creating Longs”:
myList.add(new Long(1));
These examples are probably well optimized by modern Java compilers. But even if they weren’t, they probably don’t affect much to the performance of the system as a whole.