Skip to content
This repository was archived by the owner on May 16, 2023. It is now read-only.

CodeStyle

Kegsay edited this page Apr 20, 2015 · 1 revision

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 { } for if statements. 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 $window instead of window. This makes the window object 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 an init() 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 $rootScope in 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.

Clone this wiki locally