EmmanuelDemey / EmmanuelDemey/eslint-plugin-angular

Function Declarations to Hide Implementation Details (Y053)

Open
#43 0 comments 0 reactions 0 assignees View on GitHub
Rule
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
- Use function declarations to hide implementation details. Keep your accessible members of the factory up top. Point those to function declarations that appears later in the file. For more details see [this post](http://www.johnpapa.net/angular-function-declarations-function-expressions-and-readable-code).

_Why?_: Placing accessible members at the top makes it easy to read and helps you instantly identify which functions of the factory you can access externally.

_Why?_: Placing the implementation details of a function later in the file moves that complexity out of view so you can see the important stuff up top.

_Why?_: Function declaration are hoisted so there are no concerns over using a function before it is defined (as there would be with function expressions).

_Why?_: You never have to worry with function declarations that moving `var a` before `var b` will break your code because `a` depends on `b`.

_Why?_: Order is critical with function expressions

``` javascript
/**
* avoid
* Using function expressions
*/
function dataservice($http, $location, $q, exception, logger) {
var isPrimed = false;
var primePromise;

var getAvengers = function() {
// implementation details go here
};

var getAvengerCount = function() {
// implementation details go here
};

var getAvengersCast = function() {
// implementation details go here
};

var prime = function() {
// implementation details go here
};

var ready = function(nextPromises) {
// implementation details go here
};

var service = {
getAvengersCast: getAvengersCast,
getAvengerCount: getAvengerCount,
getAvengers: getAvengers,
ready: ready
};

return service;
}
```

``` javascript
/**
* recommended
* Using function declarations
* and accessible members up top.
*/
function dataservice($http, $location, $q, exception, logger) {
var isPrimed = false;
var primePromise;

var service = {
getAvengersCast: getAvengersCast,
getAvengerCount: getAvengerCount,
getAvengers: getAvengers,
ready: ready
};

return service;

////////////

function getAvengers() {
// implementation details go here
}

function getAvengerCount() {
// implementation details go here
}

function getAvengersCast() {
// implementation details go here
}

function prime() {
// implementation details go here
}

function ready(nextPromises) {
// implementation details go here
}
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.