PUBLIC OBJECT

Abstractions are difficult

Someone recently sent me a link to an IBM slide deck on collections implementations. It has examples of the many ways Java programs can waste memory.

It reminds me of why coding is difficult:

  • Using abstraction is expensive. Even careful, appropriate abstractions slow down the application by adding load to the hardware. This may cost each user tens or hundreds of milliseconds each time they run the application.
  • Not using abstraction is expensive. Avoiding careful, appropriate abstractions slows down the application's development. This may cost each developer several minutes or hours each time they modify the application.

I love the rare abstractions that are implemented more efficiently than the corresponding low level code. Here's three of my favorites:

  • Guava's ImmutableSet. It gives you an unbreakable promise that the data won't change. And it has space and time-efficient implementations for empty, singleton, and multiple-element collections.
  • Gson 2's new data binding. It gives you automatic conversion from a JSON stream to a Java object. And by knowing the target's type, it can aggressively skip parsing values that it knows won't be used.
  • Android's String.split(). It gives you a short, efficient way to break up a string with a regex delimiter. And by special-casing splits on single-character literals, it avoids creating a java.util.regex.Pattern if one is not needed.

To me, discovering one of these too-good-to-be-true abstractions is like a coal miner finding a diamond.