How to Structure Your Code into Different Folders | Explained

TLDRLearn the two main approaches for organizing code into different folders. Approach 1: Divide by type; Approach 2: Divide by feature. Approach 1 is commonly used but lacks cohesion, while Approach 2 focuses on keeping related code together. We implement a menu Aggregate as an example.

Key insights

📂Approach 1: Divide code by type (Aggregates, Entities, Value Objects) in separate folders. Common, shared classes can be placed in a 'Common' folder. Provides a clear overview of code structure but lacks cohesion and may result in deeply nested folders.

📁Approach 2: Divide code by feature in separate folders (e.g., 'Menu', 'Guest', 'Host'). Each folder contains all related code (Entities, Value Objects, etc.) for a specific feature. Provides better cohesion and allows for easier refactoring and code reusability.

📝Creating proper folder structure and organizing code based on feature rather than type can improve code maintainability, readability, and scalability.

⚙️Implementing folder structure based on feature allows for easy navigation and understanding of the system. It also enables seamless refactoring by moving entire slices of logic to different folders or bounded contexts.

🔀Choosing the appropriate approach depends on the project's needs and requirements. Approach 1 may be suitable for smaller projects or teams with limited complexity, while Approach 2 is more beneficial in larger projects with multiple features and complex domain models.

Q&A

What are the advantages of Approach 1 (dividing code by type)?

Approach 1 provides a clear overview of code structure by dividing it into separate folders for Aggregates, Entities, Value Objects, etc. It can be useful for small projects or teams with limited complexity.

What are the advantages of Approach 2 (dividing code by feature)?

Approach 2 offers better cohesion by keeping all code related to a specific feature in one folder. It allows for easier refactoring, code reusability, and scalability. It is particularly beneficial for larger projects with multiple features and complex domain models.

Can I combine both approaches?

Yes, you can combine both approaches to suit your project's needs. It is common to have a 'Common' folder to place shared classes and utilities, while organizing the remaining code by feature.

How does organizing code into separate folders improve maintainability?

Organizing code into separate folders improves maintainability by providing a clear and consistent structure. This allows developers to quickly locate and understand the relevant code, facilitating easier maintenance and reducing the chance of introducing bugs.

When should I consider refactoring the folder structure?

Consider refactoring the folder structure when the project grows, new features are added, or the current structure no longer meets the project's needs. Refactoring allows for better organization, separation of concerns, and easier management of the codebase.

Timestamped Summary

00:00Introduction and overview of the two main approaches for organizing code into different folders: Approach 1 (dividing by type) and Approach 2 (dividing by feature).

04:55Detailed explanation and implementation of Approach 2: dividing code by feature. Example of implementing a menu Aggregate and its related Entities and Value Objects.

08:58Discussion on the advantages and disadvantages of both approaches, including code cohesion, maintainability, scalability, and reusability.

11:11Explanation of the importance of proper folder structure in improving code readability, maintainability, and scalability. Insights on when and why to consider refactoring the folder structure.