Skip to content

Instantly share code, notes, and snippets.

@sapegin
Forked from sindresorhus/codestyle.md
Last active October 14, 2015 01:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sapegin/4284255 to your computer and use it in GitHub Desktop.
Save sapegin/4284255 to your computer and use it in GitHub Desktop.
My JavaScript Code Style

JavaScript Code Style

  • Tab indentation
  • Single-quotes
  • Semicolon
  • camelCase
  • Strict mode
  • No trailing whitespace
  • Variables where needed
  • Multiple variable statements
  • Space after keywords and between arguments and operators
  • Use === and !== over == and !=.
  • Return early
  • Limit line lengths to 120 chars
  • Prefer readability over religion

Example:

'use strict';

function foo(bar, fum) {
    if (!bar) return;

    var hello = 'Hello';
    var ret = 0;
    for (var barIdx = 0; barIdx < bar.length; barIdx++) {
        if (bar[barIdx] === hello) {
            ret += fum(bar[barIdx]);
        }
    }

    return ret;
}

Read idiomatic.js for general JavaScript code style best practices.

# editorconfig.org
root = true
[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
{
"requireCurlyBraces": ["for", "while", "do"],
"requireSpaceAfterKeywords": ["if", "else", "for", "while", "do", "switch", "return"],
"requireSpacesInFunctionExpression": { "beforeOpeningCurlyBrace": true },
"disallowSpacesInFunctionExpression": { "beforeOpeningRoundBrace": true },
"disallowMultipleVarDecl": true,
"disallowSpacesInsideArrayBrackets": true,
"disallowSpaceAfterObjectKeys": true,
"disallowLeftStickedOperators": ["?", "=", "==", "===", "!=", "!==", ">", ">=", "<", "<="],
"requireRightStickedOperators": ["!"],
"disallowRightStickedOperators": ["?", ":", "=", "==", "===", "!=", "!==", ">", ">=", "<", "<="],
"requireLeftStickedOperators": [","],
"requireKeywordsOnNewLine": ["else"],
"requireLineFeedAtFileEnd": true,
"validateJSDoc": {
"checkParamNames": true,
"checkRedundantParams": true,
"requireParamTypes": true
},
"excludeFiles": ["node_modules/**"]
}
{
"browser": true,
"white": false,
"smarttabs": true,
"eqeqeq": true,
"immed": true,
"latedef": true,
"newcap": true,
"undef": true,
"jquery": true,
"globals": {
"Modernizr": true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment