Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is having only one class in a package a code smell?

Tags:

java

oop

packages

Is it a bad practice to have a package with only one class in it? Would it make more sense just to move the single class to a util package that would contain other random useful classes?

like image 454
Edward Dale Avatar asked Sep 20 '10 09:09

Edward Dale


2 Answers

Is it a bad practice to have a package with only one class in it?

Not necessarily. It could be a sign of somebody getting obsessed with classifying things. On the other hand, it could just be a logical consequence of a sensible general classification scheme applied in an unusual case.

An example of the latter might be where you have a general API, and multiple implementations of that API, where each of the implementations consists of multiple classes. But one of those implementations (lets call it the Null implementation) consists of just one class.

The real test is whether the package structure is serving its purpose(s):

  • Is it making it easier to find library classes?

  • Do the packages organize the application classes along the lines of the application's logical module structure?

  • Does the structure allow you to effectively make use of "package private" visibility?

Would it make more sense just to move the single class to a util package that would contain other random useful classes?

Not necessarily. If the class is just another "randomly useful" leaf class, then there is a good case for moving it. On the other hand, if it has a specific function and is not intended to be used generally, then it may be better to leave it where it is.

It is best not to get too obsessed with creating elegant package hierarchies, or with rejigging them when they turn out to be not as elegant (or useful) as you first thought. There are usually more important things to do, like implementing functionality, writing tests, writing documentation and so on.

like image 124
Stephen C Avatar answered Oct 15 '22 13:10

Stephen C


No

Package is used to put similar classes together,
In your system if there is no similar class then obviously you can put it .

like image 33
jmj Avatar answered Oct 15 '22 13:10

jmj