开发者

What is ‘power-to-weight ratio’ of an API?

In Bloch’s presentation, he said designer should look for good power-to-weight ratio for API. Moreover, he also stressed that ‘Conceptual weight more important than bulk’. I guess the weight is for ‘Conceptual weight’, bulk is for number of methods of a class.

But I couldn’t understand what ‘Conceptual weight’ is, what ‘power-to-weight ratio’ is. Welcome to any explanation!

Bloch gave an example: List.subList() has good 'power-to-weight ratio'. If clients wants to know an index of a sub list, he doesn't need to call a low 'p2w ratio' method indexOfSubList(a,b,e), instead, he could call List.subList(a,b).indexOf(e). Bloch thought开发者_如何学JAVA this is 'power-to-weight ratio'.

Origin:

API Should Be As Small As Possible But No Smaller

  • API should satisfy its requirements
  • When in doubt leave it out
    • Functionality, classes, methods, parameters, etc.
    • You can always add, but you can never remove
  • Conceptual weight more important than bulk
  • Look for a good power-to-weight ratio


I'd interpret "conceptual weight" as the number of abstract concepts you have to learn and understand to use the API. Concepts usually map to public classes, while classes that are not public add to the bulk but not to the conceptual weight.

So if you put it technically, an API has a high conceptual weight if a typical client of the API has to explicitly use a lot of classes belonging to the API to work with it.

"good power-to-weight ratio" then means the API should use as few public classes as possible to offer as much functionality as possible. That means an API should:

  • Not add concepts or abstractions of its own that are not present in the domain
  • For complex domains, offer shortcuts to the most commonly needed functionality that allows a typical user to bypass the more complex parts of the domain


I'd say that

  • power = the amount of functionality provided by the API
  • weight = the effort required to learn the API


I guess that an API with a good power-to-weight ratio is an API that offers a lot of functionality (power) while requiring little effort to properly work (weight) with it.

This is could be done via, for example, "Convention over Configuration". (Note this is just an example, and you can achieve this in many ways.)

It would be helpful a link to Bloch's presentation, he might be referring to something else :-)


He's referring to all the stuff you get from using an API. His example is for the collections API where each time you access it, you only get specific functionality. On the other hand, some API's will load much more stuff just to give you some functionality.


Another essential aspect of the power-to-weight ratio of an API or even a language as a whole is its verbosity. That is, how much you have to type to get a task done and the readability of the resulting code. In Java, Iterator has a better power-to-weight ratio than Enumeration, the interface it was designed to replace, simply because Iterator has a shorter name and shorter method names, which are no longer than they need to be and do the same job with no loss of clarity (as well as the additional remove method).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜