========== Code Style ========== As a project, Horizon adheres to code quality standards. Python ------ We follow PEP8_ for all our Python code, and use ``pep8.py`` (available via the shortcut ``tox -e pep8``) to validate that our code meets proper Python style guidelines. .. _PEP8: https://www.python.org/dev/peps/pep-0008/ Django ------ Additionally, we follow `Django's style guide`_ for templates, views, and other miscellany. .. _Django's style guide: https://docs.djangoproject.com/en/dev/internals/contributing/writing-code/coding-style/ JavaScript ---------- The following standards are divided into required and recommended sections. Our main goal in establishing these best practices is to have code that is reliable, readable, and maintainable. Required ~~~~~~~~ **Reliable** * The code has to work on the stable and latest versions of Firefox, Chrome, Safari, and Opera web browsers, and on Microsoft Internet Explorer 11 and later. * If you turned compression off during development via ``COMPRESS_ENABLED = False`` in local_settings.py, re-enable compression and test your code before submitting. * Use ``===`` as opposed to ``==`` for equality checks. The ``==`` will do a type cast before comparing, which can lead to unwanted results. .. note:: If typecasting is desired, explicit casting is preferred to keep the meaning of your code clear. * Keep document reflows to a minimum. DOM manipulation is expensive, and can become a performance issue. If you are accessing the DOM, make sure that you are doing it in the most optimized way. One example is to build up a document fragment and then append the fragment to the DOM in one pass instead of doing multiple smaller DOM updates. * Use "strict", enclosing each JavaScript file inside a self-executing function. The self-executing function keeps the strict scoped to the file, so its variables and methods are not exposed to other JavaScript files in the product. .. Note :: Using strict will throw exceptions for common coding errors, like accessing global vars, that normally are not flagged. Example: :: (function(){ 'use strict'; // code... })(); * Use ``forEach`` | ``each`` when looping whenever possible. AngularJS and jQuery both provide for each loops that provide both iteration and scope. AngularJS: :: angular.forEach(objectToIterateOver, function(value, key) { // loop logic }); jQuery: :: $.each(objectToIterateOver, function(key, value) { // loop logic }); * Do not put variables or functions in the global namespace. There are several reasons why globals are bad, one being that all JavaScript included in an application runs in the same scope. The issue with that is if another script has the same method or variable names they overwrite each other. * Always put ``var`` in front of your variables. Not putting ``var`` in front of a variable puts that variable into the global space, see above. * Do not use ``eval( )``. The eval (expression) evaluates the expression passed to it. This can open up your code to security vulnerabilities and other issues. * Do not use '``with`` object {code}'. The ``with`` statement is used to access properties of an object. The issue with ``with`` is that its execution is not consistent, so by reading the statement in the code it is not always clear how it is being used. **Readable & Maintainable** * Give meaningful names to methods and variables. * Avoid excessive nesting. * Avoid HTML and CSS in JS code. HTML and CSS belong in templates and stylesheets respectively. For example: * In our HTML files, we should focus on layout. 1. Reduce the small/random ``