-
Notifications
You must be signed in to change notification settings - Fork 23
CodeStyle
The general code style this project follows is the Google Javascript Style Guide with a few modifications:
- This project mainly prefers
"to'. Both are acceptable though, so check with surrounding code. -
Always use curly braces
{ }forifstatements. The small decrease in readability is preferable to the potential for introducing bugs later on when someone else edits the code. - Every file must specify
"use strict";to enable strict mode.
Angular specific:
-
Use explicit filter dependencies rather than
$filter. -
Use
$windowinstead ofwindow. This makes thewindowobject dependency-injectable for testing. -
If you need to perform a state modifying action in order to setup a service, prefer to delay that operation to an
init()method. This makes testing easier. For example, don't do an AJAX call when creating the service, because this will be a side-effect from having a dependency on this service. Instead, do the AJAX call on aninit()method which is called when the Angular.js app is initialised. -
Don't pollute the
$scope. Keep related variables together, e.g.// good $scope.credentials = { user: "foo", pass: "bar" }; // bad $scope.user = "foo"; $scope.pass = "bar";
-
Don't pollute the
$rootScope. It's very easy to add functions/variables to$rootScopein order to use them in multiple places (controllers/services/etc) without giving much thought to how these functions/variables interact with the rest of the code base. This is effectively creating global variables. Spend a bit of time thinking about the interactions and create explicit dependencies instead.
In general, be consistent with the surrounding code. It is unlikely that a pull request will be rejected based on trivial deviations from this code style, though you may be asked to make explicit changes if your style is confusing, misleading or error-prone.