What is your preferred way of structuring web code?

Exploring Optimal Web Code Architecture: Business Logic vs. Layered Approach

In the ever-evolving landscape of web development, one question consistently sparks discussion among developers: What is the most effective way to structure web application code?

Recently, a noticeable trend has emerged that warrants examination. Many developers tend to organize their code primarily by layersโ€”such as models, views, controllers, services, and use casesโ€”rather than by business-specific logic or domain context.

Comparing Different Approaches

If you examine source code from a typical game development project, you’ll often find a structure centered around the game’s core entities, like:

  • player.c
  • menu.c
  • enemy.c
  • level.c

This organization places emphasis on distinct game components, making it clear which code pertains to each part of the system based on its business functionality.

On the other hand, web development projects often reflect a different paradigm. It’s common to see a folder structure that resembles the following:

  • Models/User.php, Book.php, Payment.php
  • Views/User.php, Book.php, Payment.php
  • Controllers/UserController.php, BookController.php, PaymentController.php
  • Services/UserService.php, BookService.php, PaymentService.php
  • UseCases/UserRegistration.php, BookPurchase.php, PaymentProcessing.php

This setup tends to segment code according to technical layers, with each layer containing components related to various business entities. In this structure, the user-related logic is spread across the model, controller, service, and use case files, rather than keeping all user-related business rules within a single module.

Is This the Best Approach?

This layered structure aims to promote separation of concerns and reusability. However, it raises an important question: does segmenting code strictly by layers, independent of the domain’s core business logic, truly serve the application’s long-term maintainability and clarity?

Some argue that organizing code by business contextโ€”keeping all relevant logic, validation, and rules togetherโ€”can make understanding and evolving the system more straightforward, especially as complexity grows.

Your Perspective

As this topic invites diverse opinions, Iโ€™d love to hear your thoughts. How do you prefer to organize your web application code? Do you favor a layered architecture, or do you lean towards domain-driven, business-specific modules?

Your insights could help shape best practices and inspire others to evaluate their own code structures. Feel free to share your approach and reasoning in the comments


Leave a Reply

Your email address will not be published. Required fields are marked *