[link] Package by feature

how-to
Aug 18, 20082 mins

Stand up and be counted. Which one do you think is best of the following?

Package by layer:

  • com.app.controllers
  • com.app.model
  • com.app.repositories
  • com.app.exception

Or package by feature:

  • com.app.orders
  • com.app.invoices
  • com.app.products

I recently added a new site to my RSS reader. John O’Hanly just published a though provoking article about harmful java code idioms on JavaWorld. I don’t agree with everything he suggests, but I do find his point about “Package-by-layer: Preventing the use of package-private scope” very intriguing. John expands on this theme on his website.

The most important argument to package by feature is the following:

As a direct benefit of being at a high level of abstraction, the application becomes more self-documenting : the overall size of the application is communicated by the number of packages, and the basic features are communicated by the package names. […] The fundamental flaw with package-by-layer style, on the other hand, is that it puts implementation details ahead of high level abstractions […]

A point that he doesn’t cover, but that I would like to add is that if you organize your classes by feature, the task of delivering one specific user story will likely involve mostly one package. This is a very good test of whether the code is organized correctly.

Yet, package by layer is by far the most used way to structure code.

The question is: Will you dare to break with the herd and opt for a better solution?