Title: Managing Angular Module names in a large project
Tags: angularjs;namespaces;coding-style
Question: I'm working on a a large scale angular project with a team of devs.

the problem we run into is if you have several files for a component, say a directive.  

some-directive.js
some-directive-controller.js

in the definition of both files you would have to attach them to a module, however one file must create the module with ```[]```. If the developer forgets to poke around they will add ```[]``` in the second file called will actually overwrite the module. so now it becomes a memory game.  Each developer has to remember to only declare the module in one file ```[]```

some-directive.js

```angular.module('some-module',['some-dependencies']).directive('some-directive',function(){});
```

some-controller.js

```angular.module('some-module',[]).controller('some-controller',function(){});
```

we have been using the following approach. Is there a better way?

some-directive.js
some-directive-module.js
some-directive-controller.js  

where some-directive-module only contains the module creation, includes any dependencies, and does any .config needed. Still the dev needs to remember to 
angular.module('some-directive') in all the other files without the square brackets.

some-directive-module.js

```angular.module('some-directive',[])
.config(//someconfig stuff);
```

some-directive-module.js  

```angular.module('some-directive).directive(//declare directive);```

some-directive-controller.js 

```angular.module('some-directive).controller(//declare contrller used by directive);```

I suggested that instead we should do the following, it eliminates the issue of overwriting modules, but I received some negative feedback from one of the other devs

some-directive-module.js

```angular.module('some-directive',['some-directive.directive','some-directive.controller'])
.config(//someconfig stuff);
```

some-directive-module.js  

```angular.module('some-directive.directive',[]).directive(//declare directive);```

some-directive-controller.js 

```angular.module('some-directive.controller',[]).controller(//declare contrller used by directive);```

Is there a better way? Or is one of the above options correct?

Comment: I recommend you to take a look at the John Papa's Angular Styleguide https://github.com/johnpapa/angular-styleguide. It goes  beyond a pattern for naming stuff and gives you a whole world of good practices to Angular apps. It is also flexible so you can adapt it to your own preferences.

I'm using it on my projects and everything is going fine.

Here is the accepted answer: The recommended way (by multiple competent people) is to use the setter-getter-syntax (creating once with ```angular.module("someModule",[])``` and accessing with ```angular.module("someModule")``` from there on). Putting the module definition and configuration into one file seems very clean and is common practice between a lot of developers. But make sure not to create a module for every single directive - group services, directives, constants and so on into reasonable functionality-modules instead.

Making clear what a file contains by its name is also a good idea in my opinion, so your some-directive-module.js approach seems fine to me. If developers "poke around" and "wildly add []", they should get a slap on the wrist follwoed by an explanation how modules work in angular, so they stop doing it ;-)



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:06/17/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<angularjs><namespaces><coding-style>', 'Title': 'Managing Angular Module names in a large project', 'ViewCount': 110, 'id': 30892874, 'user_AboutMe': '', 'user_AccountId': 272805.0, 'user_DisplayName': 'Doug Chamberlain', 'user_Reputation': 11094.0, 'user_Views': 1408.0, 'user_id': 564083.0}}