Back

Code Style Guidelines

 

Code Style Guidelines

Contents

Introduction

A uniform style guide is useful to promote readability and maintainability of code. Developers should keep these goals in mind when applying the guidelines, since there are always circumstances where a naive application will be counter-productive. You're expected to think!

Note too that our best practices have evolved over time, and will doubtless continue to evolve and grow. If you encounter code that doesn't follow these guidelines, you'll need to make a judgment call about whether to follow the conventions of the file you are in, or to do a style cleanup.

Text File Format

Basic Formatting

Code Correctness

Some style guidelines are about potential bugs rather than simple style. Here's what we flag with jshint.

    {
  /*
   * Enforcing options
   */
  
  "bitwise": true,    // disallow bitwise operators
  
  "curly": false,     // require curly braces for single-statement blocks
  
  "eqeqeq": true,     // Require === and !==
  
  "es3": true,        // Support IE 6/7/8/9 and other legacy environments (e.g. trailing commas)
  
  "forin": false,     // for..in loops require hasOwnProperty()
  
  "immed": true,      // Immediate function invocations must be wrapped in parentheses
  
  "latedef": true,    // Vars must be declared before first use
  
  "newcap": true,     // capitalize constructor functions
  
  "noarg": true,      // disallow arguments.caller and arguments.callee
  
  "noempty": true,    // prohibit empty blocks
  
  "nonew": true,      // disallow use of new solely for side effects
  
  "plusplus": false,  // prohibit ++ and -- operators
  
  "quotmark": false,  // enforce quotation mark consistency ("true", "false", "single", "double")
  
  "undef": true,      // report undefined variables
  
  "unused": "vars",   // "false" to report neither unused local variables nor unused arguments
                      // "vars" to report unused local variables but not unused arguments
  
  /*
   * Relaxing options
   */
  
  "boss": false,      // allow assignments where comparisons expected
  
  "debug": false,     // allow debugger statements
  
  "eqnull": true,     // allow "== null" comparisons
  
  "evil": false,      // allow eval
  
  "expr": false,      // allow use of expressions where normally you would expect to see assignments or function calls
  
  "funcscope": false, // allow vars declared inside controls structures to be used outside
  
  "laxbreak": true,   // allow "possibly unsafe" line breaks
  
  "laxcomma": true,   // allow comma-first coding style
  
  "loopfunc": false,  // allow functions defined in loops
  
  "multistr": false,  // suppress warnings about multi-line strings
  
  "smarttabs": true,  // allow mixed spaces and tabs for alignment

  "noarg": false,     // IE8 support needs arguments.callee for base clas calling idiom
  
  /*
   * Ignore warnings without options
   */
  
  "-W099": false,     // ignore warnings about mixed spaces and tabs