Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is having a fine-grained package structure a good or a bad thing?

Tags:

java

I recently looked at a Java application which had a very fine-grained package structure. Many packages only contained one or two classes and many sub packages. Also many packages contained more sub packages than actual classes.

Is this a good or a bad thing?

like image 703
Daniel Rikowski Avatar asked Dec 09 '09 07:12

Daniel Rikowski


People also ask

What is a package structure?

A structure package bundles a set of packages at the top level of the package hierarchy. Structure packages do not contain any repository objects except for its own package interfaces and subpackages. Instead, they hold several main packages and are used to structure software projects.

What is package structure in Java?

A Java package structure is like a directory structure. Its a tree of packages, subpackages and classes inside these classes. A Java package structure is indeed organized as directories on your hard drive, or as directories inside a zip file (JAR files).

What is import package in Java?

In java, the import keyword used to import built-in and user-defined packages. When a package has imported, we can refer to all the classes of that package using their name directly. The import statement must be after the package statement, and before any other statement.

Can Java package name contain numbers?

This links to identifier grammar that Java uses, which applies everywhere identifier is mentioned, including but not limited to package name. So 01 is definitely not a valid package name, since it's not a valid identifier.


2 Answers

IMO, it is a bad thing, though not a real show-stopper in terms of maintainability.

The disadvantages are that it makes classes harder to find, and that it makes the package names more verbose. The former applies more when you are not using an IDE.

It could be argued that it helps modularization in conjunction with "package private" scoping. But conversely, you could also argue that over-packagization actually does the opposite; i.e. forcing you to use public where you wouldn't have had to if you'd been less fine-grained / pedantic.

like image 82
Stephen C Avatar answered Nov 09 '22 22:11

Stephen C


The actual number of types that end up in one particular package is not that important. It is how you arrive at your package structure.

Things like abstraction ("what" instead of "how", essentially "public" API), coupling (the degree of how dependent a package is on other packages) and cohesion (how interrelated is the functionality in one package) are more important.

Some guidelines for package design (mostly from Uncle Bob) are for example:

  • Packages should form reusable and releasable modules
  • Packages should be focussed for good reusability
  • Avoid cyclic dependencies between packages
  • Packages should depend only on packages that change less often
  • The abstraction of a package should be in proportion to how often it changes

Do not try to flesh out an entire package structure from scratch (You Are Not Going To Need It). Instead let it evolve and refactor often. Look at the import section of your Java sources for inspiration on moving types around. Don't be distracted by packages that contain only one or a few types.

like image 38
eljenso Avatar answered Nov 09 '22 22:11

eljenso