Principles of Writing Consistent, Idiomatic JavaScript
This is a living document and new ideas for improving the code around us are always welcome. Contribute: fork, clone, branch, commit, push, pull request.
All code in any code-base should look like a single person typed it, no matter how many people contributed.
The following list outlines the practices that I use in all code that I am the original author of; contributions to projects that I have created should follow these guidelines.
I do not intend to impose my style preferences on other people's code or projects; if an existing common style exists, it should be respected.
"Arguments over style are pointless. There should be a style guide, and you should follow it"
RebeccaMurphey
"Part of being a good steward to a successful project is realizing that writing code for yourself is a Bad Idea™. If thousands of people are using your code, then write your code for maximum clarity, not your personal preference of how to get clever within the spec."
The following should be considered 1) incomplete, and 2) REQUIRED READING. I don't always agree with the style written by the authors below, but one thing is certain: They are consistent. Furthermore, these are authorities on the language.
Projects should always attempt to include some generic means by which source can be linted, tested and compressed in preparation for production use. For this task, grunt by Ben Alman is second to none and has officially replaced the "kits/" directory of this repo.
Test Facility
Projects must include some form of unit, reference, implementation or functional testing. Use case demos DO NOT QUALIFY as "tests". The following is a list of test frameworks, none of which are endorsed more than the other.
The following sections outline a reasonable style guide for modern JavaScript development and are not meant to be prescriptive. The most important take-away is the law of code style consistency. Whatever you choose as the style for your project should be considered law. Link to this document as a statement of your project's commitment to code style consistency, readability and maintainability.
// if/else/for/while/try always have spaces, braces and span multiple lines// this encourages readability// 2.A.1.1// Examples of really cramped syntaxif(condition)doSomething();while(condition)iterating++;for(vari=0;i<100;i++)someIterativeFn();// 2.A.1.1// Use whitespace to promote readabilityif(condition){// statements}while(condition){// statements}for(vari=0;i<100;i++){// statements}// Even better:vari,length=100;for(i=0;i<length;i++){// statements}// Or...vari=0,length=100;for(;i<length;i++){// statements}varprop;for(propinobject){// statements}if(true){// statements}else{// statements}
B. Assignments, Declarations, Functions ( Named, Expression, Constructor )
// 2.B.1.1// Variablesvarfoo="bar",num=1,undef;// Literal notations:vararray=[],object={};// 2.B.1.2// Using only one `var` per scope (function) or one `var` for each variable,// promotes readability and keeps your declaration list free of clutter.// Using one `var` per variable you can take more control of your versions// and makes it easier to reorder the lines.// One `var` per scope makes it easier to detect undeclared variables// that may become implied globals.// Choose better for your project and never mix them.// Badvarfoo="",bar="";varqux;// Goodvarfoo="";varbar="";varqux;// or..varfoo="",bar="",qux;// or..var// Comment on thesefoo="",bar="",quux;// 2.B.1.3// var statements should always be in the beginning of their respective scope (function).// Badfunctionfoo(){// some statements herevarbar="",qux;}// Goodfunctionfoo(){varbar="",qux;// all statements after the variables declarations.}// 2.B.1.4// const and let, from ECMAScript 6, should likewise be at the top of their scope (block).// Badfunctionfoo(){letfoo,bar;if(condition){bar="";// statements}}// Goodfunctionfoo(){letfoo;if(condition){letbar="";// statements}}
// 2.B.2.1// Named Function Declarationfunctionfoo(arg1,argN){}// Usagefoo(arg1,argN);// 2.B.2.2// Named Function Declarationfunctionsquare(number){returnnumber*number;}// Usagesquare(10);// Really contrived continuation passing stylefunctionsquare(number,callback){callback(number*number);}square(10,function(square){// callback statements});// 2.B.2.3// Function Expressionvarsquare=function(number){// Return something valuable and relevantreturnnumber*number;};// Function Expression with Identifier// This preferred form has the added value of being// able to call itself and have an identity in stack traces:varfactorial=functionfactorial(number){if(number<2){return1;}returnnumber*factorial(number-1);};// 2.B.2.4// Constructor DeclarationfunctionFooBar(options){this.options=options;}// UsagevarfooBar=newFooBar({a: "alpha"});fooBar.options;// { a: "alpha" }
C. Exceptions, Slight Deviations
// 2.C.1.1// Functions with callbacksfoo(function(){// Note there is no extra space between the first paren// of the executing function call and the word "function"});// Function accepting an array, no spacefoo(["alpha","beta"]);// 2.C.1.2// Function accepting an object, no spacefoo({a: "alpha",b: "beta"});// Single argument string literal, no spacefoo("bar");// Expression parens, no spaceif(!("foo"inobj)){obj=(obj.bar||defaults).baz;}
D. Consistency Always Wins
In sections 2.A-2.C, the whitespace rules are set forth as a recommendation with a simpler, higher purpose: consistency.
It's important to note that formatting preferences, such as "inner whitespace" should be considered optional, but only one style should exist across the entire source of your project.
Whether you prefer single or double shouldn't matter, there is no difference in how JavaScript parses them. What ABSOLUTELY MUST be enforced is consistency. Never mix quotes in the same project. Pick one style and stick with it.
F. End of Lines and Empty Lines
Whitespace can ruin diffs and make changesets impossible to read. Consider incorporating a pre-commit hook that removes end-of-line whitespace and blanks spaces on empty lines automatically.
object.prop === undefined
object.hasOwnProperty( prop )
"prop" in object
B. Coerced Types
Consider the implications of the following...
Given this HTML:
<inputtype="text" id="foo-input" value="1">
// 3.B.1.1// `foo` has been declared with the value `0` and its type is `number`varfoo=0;// typeof foo;// "number"
...
// Somewhere later in your code, you need to update `foo`// with a new value derived from an input elementfoo=document.getElementById("foo-input").value;// If you were to test `typeof foo` now, the result would be `string`// This means that if you had logic that tested `foo` like:if(foo===1){importantTask();}// `importantTask()` would never be evaluated, even though `foo` has a value of "1"// 3.B.1.2// You can preempt issues by using smart coercion with unary + or - operators:foo=+document.getElementById("foo-input").value;// ^ unary + operator will convert its right side operand to a number// typeof foo;// "number"if(foo===1){importantTask();}// `importantTask()` will be called
请发表评论