-
Notifications
You must be signed in to change notification settings - Fork 13
Coding Guidelines
Kevin Lau edited this page May 1, 2014
·
9 revisions
We are extremely strict about the coding style and insist that contributors adopt the same style.
In general, these guidelines follow the Mozilla Webdev Bootcamp JS/CSS Style Guides found here:
http://mozweb.readthedocs.org/en/latest/js-style.html
http://mozweb.readthedocs.org/en/latest/css-style.html
- Use spaces, not tabs.
- Use 4 spaces for indentation in JavaScript files and 2 spaces for indentation in HTML and CSS/Stylus files.
- Use single quotes in JavaScript files/statements and CSS stylus files, and double quotes for attributes in HTML files.
- camelCase for variables and function names.
- Newline at end of files.
- The nunjucks templating language is being used to dynamically generate markup.
- Plain text being used in template files have to be l10n localised using
{{ _('<text here>') }}. Here's an example:<h2>{{ _('Games you are playing') }}</h2>. - Space after
{{,{%and before}},%}. - The full path for media files can be obtained using the helper function:
{{ media(<path within media folder>) }}. - Nunjuck comments are preferred over HTML comments.
- Omit the
/in self-closing tags (e.g.img,br) in HTML5. - Use 2 spaces for indentation.
- Use double quotes for HTML attributes.
- Use single quotes for nunjucks templating code (because it is considered JavaScript).
- We use the CSS pre-processor Stylus for efficient writing of CSS code. However, note that an older version (0.32) is being used, so braces are still required.
- Make your CSS edits to the
.stylfiles only. Upon modification to a*.stylfile, the corresponding*.styl.cssfile will be compiled. Note that the.styl.cssfiles are beinggitignore'd and changes made in them will not be tracked. - We insist that CSS properties within a selector should be listed in alphabetical order. Else, you will receive a 🍟 comment (meaning wrong-order) when your code is being reviewed.
- Hex values for colors should be written in lower-case.
- No inline-styles please.
- Do not add units to 0 values.
- If a particular value is being used in many places, add them to the
variables.stylfile. - Use 4 spaces for indentation.
- Use single quotes if needed.
- We write modular JavaScript using require.js to keep our JavaScript code fast and high quality.
- Use event delegates for attaching event listeners to a page; see here. Define event listeners outside of the builder callback so that the event listeners don't have to be created/destroyed on every page load/unload.
- Plain text strings have to be localized using
gettext('<text>'). - If
$(this)is being used more than once within a function, create a local variablevar $this = $(this);. - Place a dollar sign before any variable that is a JQuery Object
- Semi-colons at the end of line.
- Use the one true brace indentation style. Do not start opening braces on a new line.