## 代碼格式化
展開見詳細描述.
主要依照[C++ 格式規范](http://docs.kissyui.com/docs/html/styleguide/google/cppguide.xml#Formatting) ( [中文版](http://yangyubo.com/google-cpp-styleguide/formatting.html) ), 針對 JavaScript, 還有下面一些附加說明.
### 大括號
分號會被隱式插入到代碼中, 所以你務必在同一行上插入大括號. 例如:
```
if (something) {
// ...
} else {
// ...
}
```
### 數組和對象的初始化
如果初始值不是很長, 就保持寫在單行上:
```
var arr = [1, 2, 3]; // No space after [ or before ].
var obj = {a: 1, b: 2, c: 3}; // No space after { or before }.
```
初始值占用多行時, 縮進2個空格.
```
// Object initializer.
var inset = {
top: 10,
right: 20,
bottom: 15,
left: 12
};
// Array initializer.
this.rows_ = [
'"Slartibartfast" <fjordmaster@magrathea.com>',
'"Zaphod Beeblebrox" <theprez@universe.gov>',
'"Ford Prefect" <ford@theguide.com>',
'"Arthur Dent" <has.no.tea@gmail.com>',
'"Marvin the Paranoid Android" <marv@googlemail.com>',
'the.mice@magrathea.com'
];
// Used in a method call.
goog.dom.createDom(goog.dom.TagName.DIV, {
id: 'foo',
className: 'some-css-class',
style: 'display:none'
}, 'Hello, world!');
```
比較長的標識符或者數值, 不要為了讓代碼好看些而手工對齊.
如:
```
CORRECT_Object.prototype = {
a: 0,
b: 1,
lengthyName: 2
};
```
不要這樣做:
```
WRONG_Object.prototype = {
a : 0,
b : 1,
lengthyName: 2
};
```
### 函數參數
盡量讓函數參數在同一行上.
如果一行超過 80 字符, 每個參數獨占一行, 并以4個空格縮進, 或者與括號對齊, 以提高可讀性. 盡可能不要讓每行超過80個字符. 比如下面這樣:
```
// Four-space, wrap at 80\. Works with very long function names, survives
// renaming without reindenting, low on space.
goog.foo.bar.doThingThatIsVeryDifficultToExplain = function(
veryDescriptiveArgumentNumberOne, veryDescriptiveArgumentTwo,
tableModelEventHandlerProxy, artichokeDescriptorAdapterIterator) {
// ...
};
// Four-space, one argument per line. Works with long function names,
// survives renaming, and emphasizes each argument.
goog.foo.bar.doThingThatIsVeryDifficultToExplain = function(
veryDescriptiveArgumentNumberOne,
veryDescriptiveArgumentTwo,
tableModelEventHandlerProxy,
artichokeDescriptorAdapterIterator) {
// ...
};
// Parenthesis-aligned indentation, wrap at 80\. Visually groups arguments,
// low on space.
function foo(veryDescriptiveArgumentNumberOne, veryDescriptiveArgumentTwo,
tableModelEventHandlerProxy, artichokeDescriptorAdapterIterator) {
// ...
}
// Parenthesis-aligned, one argument per line. Visually groups and
// emphasizes each individual argument.
function bar(veryDescriptiveArgumentNumberOne,
veryDescriptiveArgumentTwo,
tableModelEventHandlerProxy,
artichokeDescriptorAdapterIterator) {
// ...
}
```
### 傳遞匿名函數
如果參數中有匿名函數, 函數體從調用該函數的左邊開始縮進2個空格, 而不是從 function 這個關鍵字開始. 這讓匿名函數更加易讀 (不要增加很多沒必要的縮進讓函數體顯示在屏幕的右側).
```
var names = items.map(function(item) {
return item.name;
});
prefix.something.reallyLongFunctionName('whatever', function(a1, a2) {
if (a1.equals(a2)) {
someOtherLongFunctionName(a1);
} else {
andNowForSomethingCompletelyDifferent(a2.parrot);
}
});
```
### 更多的縮進
事實上, 除了[初始化數組和對象](#Array_and_Object_literals), 和傳遞匿名函數外, 所有被拆開的多行文本要么選擇與之前的表達式左對齊, 要么以4個(而不是2個)空格作為一縮進層次.
```
someWonderfulHtml = '' +
getEvenMoreHtml(someReallyInterestingValues, moreValues,
evenMoreParams, 'a duck', true, 72,
slightlyMoreMonkeys(0xfff)) +
'';
thisIsAVeryLongVariableName =
hereIsAnEvenLongerOtherFunctionNameThatWillNotFitOnPrevLine();
thisIsAVeryLongVariableName = 'expressionPartOne' + someMethodThatIsLong() +
thisIsAnEvenLongerOtherFunctionNameThatCannotBeIndentedMore();
someValue = this.foo(
shortArg,
'Some really long string arg - this is a pretty common case, actually.',
shorty2,
this.bar());
if (searchableCollection(allYourStuff).contains(theStuffYouWant) &&
!ambientNotification.isActive() && (client.isAmbientSupported() ||
client.alwaysTryAmbientAnyways()) {
ambientNotification.activate();
}
```
### 空行
使用空行來劃分一組邏輯上相關聯的代碼片段.
```
doSomethingTo(x);
doSomethingElseTo(x);
andThen(x);
nowDoSomethingWith(y);
andNowWith(z);
```
### 二元和三元操作符
操作符始終跟隨著前行, 這樣就不用顧慮分號的隱式插入問題. 如果一行實在放不下, 還是按照上述的縮進風格來換行.
```
var x = a ? b : c; // All on one line if it will fit.
// Indentation +4 is OK.
var y = a ?
longButSimpleOperandB : longButSimpleOperandC;
// Indenting to the line position of the first operand is also OK.
var z = a ?
moreComplicatedB :
moreComplicatedC;
```