@ngdoc tutorial @name 4 - Directory and File Organization @step 4 @description In this step, we will not be adding any new functionality to our application. Instead, we are going to take a step back, refactor our codebase and move files and code around, in order to make our application more easily expandable and maintainable. In the previous step, we saw how to architect our application to be modular and testable. What's equally important though, is organizing our codebase in a way that makes it easy (both for us and other developers on our team) to navigate through the code and quickly locate the pieces that are relevant to a specific feature or section of the application. To that end, we will explain why and how we: * Put each entity in its **own file**. * Organize our code by **feature area**, instead of by function. * Split our code into **modules** that other modules can depend on.
We will keep it short, not going into great detail on every good practice and convention. These principles are explained in great detail in the [AngularJS Style Guide][styleguide], which also contains many more techniques for effectively organizing AngularJS codebases.
## One Feature per File It might be tempting, for the sake of simplicity, to put everything in one file, or have one file per type; e.g. all controllers in one file, all components in another file, all services in a third file, and so on. This might seem to work well in the beginning, but as our application grows it becomes a burden to maintain. As we add more and more features, our files will get bigger and bigger and it will be difficult to navigate and find the code we are looking for. Instead we should put each feature/entity in its own file. Each stand-alone controller will be defined in its own file, each component will be defined in its own file, etc. Luckily, we don't need to change anything with respect to that guideline in our code, since we have already defined our `phoneList` component in its own `phone-list.component.js` file. Good job! We will keep this in mind though, as we add more features. ## Organizing by Feature So, now that we learned we should put everything in its own file, our `app/` directory will soon be full with dozens of files and specs (remember we keep our unit test files next to the corresponding source code files). What's more important, logically related files will not be grouped together; it will be really difficult to locate all files related to a specific section of the application and make a change or fix a bug. So, what shall we do? Well, we are going to group our files into directories _by feature_. For example, since we have a section in our application that lists phones, we will put all related files into a `phone-list/` directory under `app/`. We are soon to find out that certain features are used across different parts of the application. We will put those inside `app/core/`.

Other typical names for our `core` directory are `shared`, `common` and `components`. The last one is kind of misleading though, as it will contain other things than components as well.

(This is mostly a relic of the past, when "components" just meant the generic building blocks of an application.)

Based on what we have discussed so far, here is our directory/file layout for the `phoneList` "feature": ``` app/ phone-list/ phone-list.component.js phone-list.component.spec.js app.js ``` ## Using Modules As previously mentioned, one of the benefits of having a modular architecture is code reuse — not only inside the same application, but across applications too. There is one final step in making this code reuse frictionless: * Each feature/section should declare its own module and all related entities should register themselves on that module. Let's take the `phoneList` feature as an example. Previously, the `phoneList` component would register itself on the `phonecatApp` module: ```js angular. module('phonecatApp'). component('phoneList', ...); ``` Similarly, the accompanying spec file loads the `phonecatApp` module before each test (because that's where our component is registered). Now, imagine that we need a list of phones on another project that we are working on. Thanks to our modular architecture, we don't have to reinvent the wheel; we simply copy the `phone-list/` directory on our other project and add the necessary script tags in our `index.html` file and we are done, right? Well, not so fast. The new project doesn't know anything about a `phonecatApp` module. So, we would have to replace all references to `phonecatApp` with the name of this project's main module. As you can imagine this is both laborious and error-prone. Yeah, you guessed it: There is a better way! Each feature/section, will declare its own module and have all related entities registered there. The main module (`phonecatApp`) will declare a dependency on each feature/section module. Now, all it takes to reuse the same code on a new project is copying the feature directory over and adding the feature module as a dependency in the new project's main module. Here is what our `phoneList` feature will look like after this change:
**`/`:** ``` app/ phone-list/ phone-list.module.js phone-list.component.js phone-list.component.spec.js app.module.js ```
**`app/phone-list/phone-list.module.js`:** ```js // Define the `phoneList` module angular.module('phoneList', []); ```
**`app/phone-list/phone-list.component.js`:** ```js // Register the `phoneList` component on the `phoneList` module, angular. module('phoneList'). component('phoneList', {...}); ```
**`app/app.module.js`:**
_(since `app/app.js` now only contains the main module declaration, we gave it a `.module` suffix)_ ```js // Define the `phonecatApp` module angular.module('phonecatApp', [ // ...which depends on the `phoneList` module 'phoneList' ]); ``` By passing `phoneList` inside the dependencies array when defining the `phonecatApp` module, AngularJS will make all entities registered on `phoneList` available on `phonecatApp` as well.

Don't forget to also update your `index.html` adding a `