EmmanuelDemey / EmmanuelDemey/eslint-plugin-angular
Accessible Members Up Top (Y052)
- Dominant language
- JavaScript
- Stars
- 620
- Forks
- 127
- PR merge metrics
- No merged PRs in 30d
Description
https://github.com/johnpapa/angularjs-styleguide/edit/master/README.md
- Expose the callable members of the service (its interface) at the top, using a technique derived from the [Revealing Module Pattern](http://addyosmani.com/resources/essentialjsdesignpatterns/book/#revealingmodulepatternjavascript).
_Why?_: Placing the callable members at the top makes it easy to read and helps you instantly identify which members of the service can be called and must be unit tested (and/or mocked).
_Why?_: This is especially helpful when the file gets longer as it helps avoid the need to scroll to see what is exposed.
_Why?_: Setting functions as you go can be easy, but when those functions are more than 1 line of code they can reduce the readability and cause more scrolling. Defining the callable interface via the returned service moves the implementation details down, keeps the callable interface up top, and makes it easier to read.
``` javascript
/* avoid */
function dataService() {
var someValue = '';
function save() {
/* */
};
function validate() {
/* */
};
return {
save: save,
someValue: someValue,
validate: validate
};
}
```
``` javascript
/* recommended */
function dataService() {
var someValue = '';
var service = {
save: save,
someValue: someValue,
validate: validate
};
return service;
////////////
function save() {
/* */
};
function validate() {
/* */
};
}
```
This way bindings are mirrored across the host object, primitive values cannot update alone using the revealing module pattern
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.