mgechev / angularjs-style-guide

đź“š Community-driven set of best practices for AngularJS application development

Home Page:https://mgechev.github.io/angularjs-style-guide/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Recommendations for module constant usage?

alejandroiglesias opened this issue · comments

Hi, this is great guidelines. One thing that i haven't found is recommendation for constants.
How should i name them? lowerCase or PascalCase? And regarding their structure i have two possibilities in mind:

1st: the all-together
myModule.constant('MyModuleConstants', {
  features: {
    featA: 'featA',
    'featB: 'featB'
  },
  options: {
    optA: 'optA',
    optB: 'optB'
  }
});
2nd: the split
myModule.constant('MyModuleConstantFeatures', {
  featA: 'featA',
  'featB: 'featB'
});
myModule.constant('MyModuleConstantOptions', {
  optA: 'optA',
  optB: 'optB'
});

Any opinions? Thanks.

Since it is practice to name our JavaScript constants UPPER_SNAKE_CASE, I'd suggest this convention but I'd also love to hear other opinions.

There is a common practice in many languages to use UPPER_CASE to name constants, but i agree with Douglas Crockford in that it's pointless and misleading for JS to name some variable in that way because there are no constants in JS and values ARE modifiable. What about Angular constants and what is the suggested approach, for naming and beyond that, for organizing them (ie, all constant data in one constant or many constants per module)?

In my opinion it is logically to spread the constants definitions across modules depending on their appurtenance, especially in eventual of lazy loading scenario. In ES6 we have a cost keyword, which will allow definition of actual constants so I think naming them UPPER_SNAKE_CASE in JavaScript and using the same approach for the AngularJS applications is appropriate and makes sense (even if we still don't use es6 (with Traceur or whatever), it is a good way to get used to this practice).