Skip to content

Instantly share code, notes, and snippets.

@kadamwhite
Created May 22, 2013 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kadamwhite/5627762 to your computer and use it in GitHub Desktop.
Save kadamwhite/5627762 to your computer and use it in GitHub Desktop.
AngularJS Declaration Conventions
module.directive('myDirective', [
'$compile',
'$location',
'Notify',
'sharedData',
function($compile, $location, Notify, sharedData) {
// do stuff
}
);
module.directive('myDirective', [
'$location',
'$compile',
'Notify',
'sharedData',
function($location, $compile, Notify, sharedData) {
// do stuff
}
);
module.directive('myDirective', [
'$compile',
'$location',
'Notify',
'sharedData',
function(
$compile,
$location,
Notify,
sharedData
) {
// do stuff
}
);
module.directive('myDirective', ['$location', '$compile', 'Notify', 'sharedData', function($location, $compile, Notify, sharedData) {
// do stuff
});
module.directive('myDirective', ['$location', '$compile', 'Notify', 'sharedData',
function($location, $compile, Notify, sharedData) {
// do stuff
}
);
@kadamwhite
Copy link
Author

We've been regularly seeing merge conflicts on Angular module, service, directive and controller definitions, as dependencies are commonly added and removed from these lines. I've been talking with Brendan about ways we could solve this problem through convention, and made this gist to kick-start the discussion.

Personally I lean towards multi-line dependencies, alphabetically ordered (with native Angular services first)—this avoids the ) { of the fully multi-line approach while minimizing conflicts in the dependency array.

Thoughts and Strong Opinions welcome.

@dariusk
Copy link

dariusk commented May 22, 2013

Agreed we need to fix this. I am okay with multline-dependencies-alphabetical or just straight multiline-dependencies. The only reason I'm not 100% into alphabetical is that it's alphabetical-with-priority-on-native-Angular. Seems a little over complex.

@kadamwhite
Copy link
Author

The angular-first came for free b/c I was assuming symbols preceded letters—I'm fine either way. Adam and Jason seem to be on-board with either multi-line everything or multi-line alpha.

@bmac
Copy link

bmac commented May 22, 2013

Yeah when I run sort-lines in my editor the angular modules bubble to the top because of the $ symbol.

@kadamwhite
Copy link
Author

Note that for most controllers this will result in $scope not coming first. This is acceptable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment