Skip to content
lorddev edited this page Sep 10, 2012 · 2 revisions

Code Formatting

  1. Maintain indentation standards. Always use spaces instead of tabs.

  2. Related to indentation standards is the necessity of putting JavaScript in separate .js files rather than embedded in HTML. Script that’s nested in <script/> tags automatically have an increased indentation level.

  3. In JavaScript, curly braces should be on the same line as the preceding condition or function declaration. Not only is this common practice for JavaScript, in which there is extra interest in keeping code minimized, but it is also necessary because of “implicit semicolon insertion” (see Google JavaScript Style Guide).

  4. Speaking of implicit semicolon insertion... don’t. Always use explicit semicolons.

  5. Personal preference is to indent lines of object initializers by 8 spaces and the closing curly brace by 4 (@lorddev).

    var data = {
            id: accountId,
            location: ipAddress
        };
    
  6. Closing curly braces for anonymous functions should line up with the line in which they were declared. This indentation practice is even more crucial in the days of heavy jQuery usage, as often every single function in a file will be wrapped this way.

    $.each(results, function(item, index) {
        if (item.id == data.id && item.location != data.location) {
            // Todo: update user locations.
        }
    });
    
  7. placeholder