Starting a new project presents a unique opportunity for you to set baseline standards which will be helpful to the execution and delivery of the project.

This standards influence your project at the root level and can be tricky to be altered during the course of the project.

One of the most important aspects that usually is left out and not given plenty thought is the code structure to be followed. A well-planned code structure can save precious development time and reduce bug resolution nightmares.

Fixing 1 bug in a complicated system can introduce 3 more

Code Structures That Developers Love

Developers usually spend a good fraction of development time in structuring the code and navigating to the correct place for specific functionality. So which all attributes can be considered for making your life as a developer easier.

  • The relevant code is placed in the same package of the module or under the same hierarchy.
  • The naming of packages should be intuitive to understand the code/logic boundaries.
  • Flexibility to reuse code for similar functionality.
  • Simplicity to duplicate code for custom feature requests.

Some sample code structure which you can use are :

MVC - Grouping by functionality

MVC pattern divides the project by functionality. Models, controllers, services and repositories are divided into different packages.

This is one of the most used code structuring patterns due to its simplicity and low learning curve.

com
 +- example
     +- myapplication
         +- Application.java
         |
         +- controller
         |   +- OrderController.java
         |   +- CustomerController.java
         +- service         
         |   +- CustomerService.java
         |   +- OrderService.java
         +- repository         
         |   +- CustomerRepository.java
         |   +- OrderRepository.java
         +- model
             +- Customer.java
             +- Order.java

Advantages :

  • The Code separation by functionality enables different dev teams to work on parallel.
  • Bug fixes generally affect a single logical layer.

Disadvantages :

  • Many files for simple functionality.
  • Logic layer (service layer in the example above) easily gets bloated.  

App / Module Based - Grouping by features

Code is separated based on modules. Many fast-paced application development framework follow this style including Django.

The development process can be easily accelerated by teams working in parallel. Also, some packages could be removed for custom project implementations later on.

com
 +- example
     +- myapplication
         +- Application.java
         |
         +- customer
         |   +- Customer.java
         |   +- CustomerRepository.java
         +- order
             +- Order.java
             +- OrderRepository.java

Advantages :

  • Different teams can work on different modules in parallel.
  • Packages could be added or removed based on custom requirements later on in the project life cycle.

Disadvantages :

  • Cross module interfaces can be difficult to manage.
  • Code reuse across modules is reduced.

Hybrid

A hybrid approach combines the above two approaches. Generally, the team who need to inherit a codebase choose this approach so they get the flexibility to rearrange code.

com
 +- example
     +- myapplication
         +- Application.java
         |
         +- controller
         |   +- OrderController.java
         |   +- CustomerController.java
         +- service         
         |   +- CustomerService.java
         |   +- OrderService.java
         +- repository         
         |   +- CustomerRepository.java
         |   +- OrderRepository.java
         +- model
         |   +- Customer.java
         |   +- Order.java
         +- payment
             +- Payment.java
             +- PaymentController.java
             +- PaymentService.java
             +- PaymentRepository.java

Advantages :

  • Specific modules can be separated for concerns like security.

Disadvantages :

  • Complexity is increased.

How Should I Pick One?

Choosing the right code structure is a difficult exercise at the start of project since the project requirements are flaky.

You must spend time with development team to discussion certain factors mentioned here for deciding which one to pick:

  • Complexity in identifying module borders.
  • Architectural Patterns selected which affect the interfaces pan subprojects.
  • Team's work style to enable parallel development of modules.
  • Security concerns.

Conclusion

Planning out how exactly you are going to structure your codebase could help you save several hours in the development and bug fixing efforts.

Following simple steps mentioned in this article you can take command of your code structure to help your customers or client get results faster while also making your life simpler.