Why the package structure is more important?
The directory structure (i would prefer to say it as the package structure) of the application is more important because, it helps to structure the code in more logical and readable manner. In addition, it helps the developer to navigate though the code with more readable and understandable way.
In proper package may lead to series of scalability, maintainability and readability issues.
What is Package by Layers?
In this approach, we identify the core layers of the application such as model, view, controller, service and dao etc… Then we create the packages based on the those identified core layers.
This is the first approach most of the developers do when it comes to the enterprise application development. They first identify the core layers of the system and group the packages based on those layers.
The main advantage of this approach is it helps to separate the different concerns (layers) in the code in a nice manner.
e.g:- All the controllers are in the controller package, All the services are in the service package.
But this approach leads to low cohesion and high coupling. That is the inverse of what we should achieve on a proper software design. In a proper design, high cohesion and low coupling should be achieved. Therefore this approach may lead to some scalability, maintainability and readability issues.
What is Package by Features?
In package by feature approach, the packages are created based on the feature. Then everything related to the feature will be placed inside the feature package.
Since all the required classes are placed in the same package, high cohesion and low coupling can be achieved. Therefore this seems to be much interested approach than “Package by Layer” approach.
Lets look at the benefit of “Package by Feature” with compared to “Package By Layer“.
- High Cohesion and Low Coupling
- Easily Scalable:- adding new features is easy. just add a new feature with required other classes for the feature. the rest of the codes will remain untouched.
- Easy to remove feature (with minimal affect for rest of the features)
- Easy to train new developers for the existing code base. They can only look for the features that they are responsible for.
- Easy to navigate through the code with lesser time
- Easy to fix the bugs
Package by Layer or Package by Feature?
I have seen that some of the developers would prefer to go with Package by Layers approach. This is because they come up with a reasonable package structure for layers and they want to use it for every project that they are working on.
“I have created good package structure for all identified layers. This can be used as a reference template for all my projects”
Based on my personal experience, i believe that this is a wrong decision. If you just stick to the package by layer approach for all projects, it will not bring the benefits of the package by feature approach. Therefore I would prefer to go with a hybrid approach starting from package by feature. That is a mix of package by feature and package by layers approach. First package by feature and then package by layers. Again this depends on the complexity of the application. If the feature does not contain many classes for each layer, we do not need to create package by layers.
The best approach should be selected based on the application nature, requirement and complexity. Since the Package by Feature has more advantages with compared to the Package by Layers, i would prefer to go with Package by Feature approach. Based on the complexity, i will again decide whether to proceed with hybrid approach or not.If there are more classes found for each layer(under the feature), then i would definitely go with hybrid approach.