成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

JavaScript風(fēng)格指南

tianhang / 1201人閱讀

摘要:中文版地址風(fēng)格指南介紹作者根據(jù)代碼整潔之道總結(jié)了適用于的軟件工程原則。此時應(yīng)考慮對函數(shù)進行再次劃分。用且只用一個完成這一需求。繼承該類時可以重載默認行為。此時減少對配置參數(shù)數(shù)量的需求是有益的。

Original Repository: ryanmcdermott/clean-code-javascript
中文版GitHub地址

JavaScript風(fēng)格指南 介紹

作者根據(jù)Robert C. Martin《代碼整潔之道》總結(jié)了適用于JavaScript的軟件工程原則《Clean Code JavaScript》。

本文是對其的翻譯。

不必嚴(yán)格遵守本文的所有原則,有時少遵守一些效果可能會更好,具體應(yīng)根據(jù)實際情況決定。這是根據(jù)《代碼整潔之道》作者多年經(jīng)驗整理的代碼優(yōu)化建議,但也僅僅只是一份建議。

軟件工程已經(jīng)發(fā)展了50多年,至今仍在不斷前進。現(xiàn)在,把這些原則當(dāng)作試金石,嘗試將他們作為團隊代碼質(zhì)量考核的標(biāo)準(zhǔn)之一吧。

最后你需要知道的是,這些東西不會讓你立刻變成一個優(yōu)秀的工程師,長期奉行他們也并不意味著你能夠高枕無憂不再犯錯。千里之行,始于足下。我們需要時常和同行們進行代碼評審,不斷優(yōu)化自己的代碼。不要懼怕改善代碼質(zhì)量所需付出的努力,加油。

變量 使用有意義,可讀性好的變量名

反例:

var yyyymmdstr = moment().format("YYYY/MM/DD");

正例:

var yearMonthDay = moment().format("YYYY/MM/DD");

回到目錄

使用ES6的const定義常量

反例中使用"var"定義的"常量"是可變的。

在聲明一個常量時,該常量在整個程序中都應(yīng)該是不可變的。

反例:

var FIRST_US_PRESIDENT = "George Washington";

正例:

const FIRST_US_PRESIDENT = "George Washington";

回到目錄

對功能類似的變量名采用統(tǒng)一的命名風(fēng)格

反例:

getUserInfo();
getClientData();
getCustomerRecord();

正例:

getUser();

回到目錄

使用易于檢索名稱

我們需要閱讀的代碼遠比自己寫的要多,使代碼擁有良好的可讀性且易于檢索非常重要。閱讀變量名晦澀難懂的代碼對讀者來說是一種相當(dāng)糟糕的體驗。
讓你的變量名易于檢索。

反例:

// 525600 是什么?
for (var i = 0; i < 525600; i++) {
  runCronJob();
}

正例:

// Declare them as capitalized `var` globals.
var MINUTES_IN_A_YEAR = 525600;
for (var i = 0; i < MINUTES_IN_A_YEAR; i++) {
  runCronJob();
}

回到目錄

使用說明變量(即有意義的變量名)

反例:

const cityStateRegex = /^(.+)[,s]+(.+?)s*(d{5})?$/;
saveCityState(cityStateRegex.match(cityStateRegex)[1], cityStateRegex.match(cityStateRegex)[2]);

正例:

const cityStateRegex = /^(.+)[,s]+(.+?)s*(d{5})?$/;
const match = cityStateRegex.match(cityStateRegex)
const city = match[1];
const state = match[2];
saveCityState(city, state);

回到目錄

不要繞太多的彎子

顯式優(yōu)于隱式。

反例:

var locations = ["Austin", "New York", "San Francisco"];
locations.forEach((l) => {
  doStuff();
  doSomeOtherStuff();
  ...
  ...
  ...
  // l是什么?
  dispatch(l);
});

正例:

var locations = ["Austin", "New York", "San Francisco"];
locations.forEach((location) => {
  doStuff();
  doSomeOtherStuff();
  ...
  ...
  ...
  dispatch(location);
});

回到目錄

避免重復(fù)的描述

當(dāng)類/對象名已經(jīng)有意義時,對其變量進行命名不需要再次重復(fù)。

反例:

var Car = {
  carMake: "Honda",
  carModel: "Accord",
  carColor: "Blue"
};

function paintCar(car) {
  car.carColor = "Red";
}

正例:

var Car = {
  make: "Honda",
  model: "Accord",
  color: "Blue"
};

function paintCar(car) {
  car.color = "Red";
}

回到目錄

避免無意義的條件判斷

反例:

function createMicrobrewery(name) {
  var breweryName;
  if (name) {
    breweryName = name;
  } else {
    breweryName = "Hipster Brew Co.";
  }
}

正例:

function createMicrobrewery(name) {
  var breweryName = name || "Hipster Brew Co."
}

回到目錄

函數(shù) 函數(shù)參數(shù) (理想情況下應(yīng)不超過2個)

限制函數(shù)參數(shù)數(shù)量很有必要,這么做使得在測試函數(shù)時更加輕松。過多的參數(shù)將導(dǎo)致難以采用有效的測試用例對函數(shù)的各個參數(shù)進行測試。

應(yīng)避免三個以上參數(shù)的函數(shù)。通常情況下,參數(shù)超過兩個意味著函數(shù)功能過于復(fù)雜,這時需要重新優(yōu)化你的函數(shù)。當(dāng)確實需要多個參數(shù)時,大多情況下可以考慮這些參數(shù)封裝成一個對象。

JS定義對象非常方便,當(dāng)需要多個參數(shù)時,可以使用一個對象進行替代。

反例:

function createMenu(title, body, buttonText, cancellable) {
  ...
}

正例:

var menuConfig = {
  title: "Foo",
  body: "Bar",
  buttonText: "Baz",
  cancellable: true
}

function createMenu(menuConfig) {
  ...
}

回到目錄

函數(shù)功能的單一性

這是軟件功能中最重要的原則之一。

功能不單一的函數(shù)將導(dǎo)致難以重構(gòu)、測試和理解。功能單一的函數(shù)易于重構(gòu),并使代碼更加干凈。

反例:

function emailClients(clients) {
  clients.forEach(client => {
    let clientRecord = database.lookup(client);
    if (clientRecord.isActive()) {
      email(client);
    }
  });
}

正例:

function emailClients(clients) {
  clients.forEach(client => {
    emailClientIfNeeded(client);
  });
}

function emailClientIfNeeded(client) {
  if (isClientActive(client)) {
    email(client);
  }
}

function isClientActive(client) {
  let clientRecord = database.lookup(client);
  return clientRecord.isActive();
}

回到目錄

函數(shù)名應(yīng)明確表明其功能

反例:

function dateAdd(date, month) {
  // ...
}

let date = new Date();

// 很難理解dateAdd(date, 1)是什么意思

正例:

function dateAddMonth(date, month) {
  // ...
}

let date = new Date();
dateAddMonth(date, 1);

回到目錄

函數(shù)應(yīng)該只做一層抽象

當(dāng)函數(shù)的需要的抽象多于一層時通常意味著函數(shù)功能過于復(fù)雜,需將其進行分解以提高其可重用性和可測試性。

反例:

function parseBetterJSAlternative(code) {
  let REGEXES = [
    // ...
  ];

  let statements = code.split(" ");
  let tokens;
  REGEXES.forEach((REGEX) => {
    statements.forEach((statement) => {
      // ...
    })
  });

  let ast;
  tokens.forEach((token) => {
    // lex...
  });

  ast.forEach((node) => {
    // parse...
  })
}

正例:

function tokenize(code) {
  let REGEXES = [
    // ...
  ];

  let statements = code.split(" ");
  let tokens;
  REGEXES.forEach((REGEX) => {
    statements.forEach((statement) => {
      // ...
    })
  });

  return tokens;
}

function lexer(tokens) {
  let ast;
  tokens.forEach((token) => {
    // lex...
  });

  return ast;
}

function parseBetterJSAlternative(code) {
  let tokens = tokenize(code);
  let ast = lexer(tokens);
  ast.forEach((node) => {
    // parse...
  })
}

回到目錄

移除重復(fù)的代碼

永遠、永遠、永遠不要在任何循環(huán)下有重復(fù)的代碼。

這種做法毫無意義且潛在危險極大。重復(fù)的代碼意味著邏輯變化時需要對不止一處進行修改。JS弱類型的特點使得函數(shù)擁有更強的普適性。好好利用這一優(yōu)點吧。

反例:

function showDeveloperList(developers) {
  developers.forEach(developer => {
    var expectedSalary = developer.calculateExpectedSalary();
    var experience = developer.getExperience();
    var githubLink = developer.getGithubLink();
    var data = {
      expectedSalary: expectedSalary,
      experience: experience,
      githubLink: githubLink
    };

    render(data);
  });
}

function showManagerList(managers) {
  managers.forEach(manager => {
    var expectedSalary = manager.calculateExpectedSalary();
    var experience = manager.getExperience();
    var portfolio = manager.getMBAProjects();
    var data = {
      expectedSalary: expectedSalary,
      experience: experience,
      portfolio: portfolio
    };

    render(data);
  });
}

正例:

function showList(employees) {
  employees.forEach(employee => {
    var expectedSalary = employee.calculateExpectedSalary();
    var experience = employee.getExperience();
    var portfolio;

    if (employee.type === "manager") {
      portfolio = employee.getMBAProjects();
    } else {
      portfolio = employee.getGithubLink();
    }

    var data = {
      expectedSalary: expectedSalary,
      experience: experience,
      portfolio: portfolio
    };

    render(data);
  });
}

回到目錄

采用默認參數(shù)精簡代碼

反例:

function writeForumComment(subject, body) {
  subject = subject || "No Subject";
  body = body || "No text";
}

正例:

function writeForumComment(subject = "No subject", body = "No text") {
  ...
}

回到目錄

使用Object.assign設(shè)置默認對象

反例:

var menuConfig = {
  title: null,
  body: "Bar",
  buttonText: null,
  cancellable: true
}

function createMenu(config) {
  config.title = config.title || "Foo"
  config.body = config.body || "Bar"
  config.buttonText = config.buttonText || "Baz"
  config.cancellable = config.cancellable === undefined ? config.cancellable : true;

}

createMenu(menuConfig);

正例:

var menuConfig = {
  title: "Order",
  // User did not include "body" key
  buttonText: "Send",
  cancellable: true
}

function createMenu(config) {
  config = Object.assign({
    title: "Foo",
    body: "Bar",
    buttonText: "Baz",
    cancellable: true
  }, config);

  // config now equals: {title: "Order", body: "Bar", buttonText: "Send", cancellable: true}
  // ...
}

createMenu(menuConfig);

回到目錄

不要使用標(biāo)記(Flag)作為函數(shù)參數(shù)

這通常意味著函數(shù)的功能的單一性已經(jīng)被破壞。此時應(yīng)考慮對函數(shù)進行再次劃分。

反例:

function createFile(name, temp) {
  if (temp) {
    fs.create("./temp/" + name);
  } else {
    fs.create(name);
  }
}

正例:

function createTempFile(name) {
  fs.create("./temp/" + name);
}

function createFile(name) {
  fs.create(name);
}

回到目錄

避免副作用

當(dāng)函數(shù)產(chǎn)生了除了“接受一個值并返回一個結(jié)果”之外的行為時,稱該函數(shù)產(chǎn)生了副作用。比如寫文件、修改全局變量或?qū)⒛愕腻X全轉(zhuǎn)給了一個陌生人等。

程序在某些情況下確實需要副作用這一行為,如先前例子中的寫文件。這時應(yīng)該將這些功能集中在一起,不要用多個函數(shù)/類修改某個文件。用且只用一個service完成這一需求。

反例:

// Global variable referenced by following function.
// If we had another function that used this name, now it"d be an array and it could break it.
var name = "Ryan McDermott";

function splitIntoFirstAndLastName() {
  name = name.split(" ");
}

splitIntoFirstAndLastName();

console.log(name); // ["Ryan", "McDermott"];

正例:

function splitIntoFirstAndLastName(name) {
  return name.split(" ");
}

var name = "Ryan McDermott"
var newName = splitIntoFirstAndLastName(name);

console.log(name); // "Ryan McDermott";
console.log(newName); // ["Ryan", "McDermott"];

回到目錄

不要寫全局函數(shù)

在JS中污染全局是一個非常不好的實踐,這么做可能和其他庫起沖突,且調(diào)用你的API的用戶在實際環(huán)境中得到一個exception前對這一情況是一無所知的。

想象以下例子:如果你想擴展JS中的Array,為其添加一個diff函數(shù)顯示兩個數(shù)組間的差異,此時應(yīng)如何去做?你可以將diff寫入Array.prototype,但這么做會和其他有類似需求的庫造成沖突。如果另一個庫對diff的需求為比較一個數(shù)組中收尾元素間的差異呢?

使用ES6中的class對全局的Array做簡單的擴展顯然是一個更棒的選擇。

反例:

Array.prototype.diff = function(comparisonArray) {
  var values = [];
  var hash = {};

  for (var i of comparisonArray) {
    hash[i] = true;
  }

  for (var i of this) {
    if (!hash[i]) {
      values.push(i);
    }
  }

  return values;
}

正例:

class SuperArray extends Array {
  constructor(...args) {
    super(...args);
  }

  diff(comparisonArray) {
    var values = [];
    var hash = {};

    for (var i of comparisonArray) {
      hash[i] = true;
    }

    for (var i of this) {
      if (!hash[i]) {
        values.push(i);
      }
    }

    return values;
  }
}

回到目錄

采用函數(shù)式編程

函數(shù)式的編程具有更干凈且便于測試的特點。盡可能的使用這種風(fēng)格吧。

反例:

const programmerOutput = [
  {
    name: "Uncle Bobby",
    linesOfCode: 500
  }, {
    name: "Suzie Q",
    linesOfCode: 1500
  }, {
    name: "Jimmy Gosling",
    linesOfCode: 150
  }, {
    name: "Gracie Hopper",
    linesOfCode: 1000
  }
];

var totalOutput = 0;

for (var i = 0; i < programmerOutput.length; i++) {
  totalOutput += programmerOutput[i].linesOfCode;
}

正例:

const programmerOutput = [
  {
    name: "Uncle Bobby",
    linesOfCode: 500
  }, {
    name: "Suzie Q",
    linesOfCode: 1500
  }, {
    name: "Jimmy Gosling",
    linesOfCode: 150
  }, {
    name: "Gracie Hopper",
    linesOfCode: 1000
  }
];

var totalOutput = programmerOutput
  .map((programmer) => programmer.linesOfCode)
  .reduce((acc, linesOfCode) => acc + linesOfCode, 0);

回到目錄

封裝判斷條件

反例:

if (fsm.state === "fetching" && isEmpty(listNode)) {
  /// ...
}

正例:

function shouldShowSpinner(fsm, listNode) {
  return fsm.state === "fetching" && isEmpty(listNode);
}

if (shouldShowSpinner(fsmInstance, listNodeInstance)) {
  // ...
}

回到目錄

避免“否定情況”的判斷

反例:

function isDOMNodeNotPresent(node) {
  // ...
}

if (!isDOMNodeNotPresent(node)) {
  // ...
}

正例:

function isDOMNodePresent(node) {
  // ...
}

if (isDOMNodePresent(node)) {
  // ...
}

回到目錄

避免條件判斷

這看起來似乎不太可能。

大多人聽到這的第一反應(yīng)是:“怎么可能不用if完成其他功能呢?”許多情況下通過使用多態(tài)(polymorphism)可以達到同樣的目的。

第二個問題在于采用這種方式的原因是什么。答案是我們之前提到過的:保持函數(shù)功能的單一性。

反例:

class Airplane {
  //...
  getCruisingAltitude() {
    switch (this.type) {
      case "777":
        return getMaxAltitude() - getPassengerCount();
      case "Air Force One":
        return getMaxAltitude();
      case "Cessna":
        return getMaxAltitude() - getFuelExpenditure();
    }
  }
}

正例:

class Airplane {
  //...
}

class Boeing777 extends Airplane {
  //...
  getCruisingAltitude() {
    return getMaxAltitude() - getPassengerCount();
  }
}

class AirForceOne extends Airplane {
  //...
  getCruisingAltitude() {
    return getMaxAltitude();
  }
}

class Cessna extends Airplane {
  //...
  getCruisingAltitude() {
    return getMaxAltitude() - getFuelExpenditure();
  }
}

回到目錄

避免類型判斷(part 1)

JS是弱類型語言,這意味著函數(shù)可接受任意類型的參數(shù)。

有時這會對你帶來麻煩,你會對參數(shù)做一些類型判斷。有許多方法可以避免這些情況。

反例:

function travelToTexas(vehicle) {
  if (vehicle instanceof Bicycle) {
    vehicle.peddle(this.currentLocation, new Location("texas"));
  } else if (vehicle instanceof Car) {
    vehicle.drive(this.currentLocation, new Location("texas"));
  }
}

正例:

function travelToTexas(vehicle) {
  vehicle.move(this.currentLocation, new Location("texas"));
}

回到目錄

避免類型判斷(part 2)

如果需處理的數(shù)據(jù)為字符串,整型,數(shù)組等類型,無法使用多態(tài)并仍有必要對其進行類型檢測時,可以考慮使用TypeScript。

反例:

function combine(val1, val2) {
  if (typeof val1 == "number" && typeof val2 == "number" ||
      typeof val1 == "string" && typeof val2 == "string") {
    return val1 + val2;
  } else {
    throw new Error("Must be of type String or Number");
  }
}

正例:

function combine(val1, val2) {
  return val1 + val2;
}

回到目錄

避免過度優(yōu)化

現(xiàn)代的瀏覽器在運行時會對代碼自動進行優(yōu)化。有時人為對代碼進行優(yōu)化可能是在浪費時間。

這里可以找到許多真正需要優(yōu)化的地方

反例:

// 這里使用變量len是因為在老式瀏覽器中,
// 直接使用正例中的方式會導(dǎo)致每次循環(huán)均重復(fù)計算list.length的值,
// 而在現(xiàn)代瀏覽器中會自動完成優(yōu)化,這一行為是沒有必要的
for (var i = 0, len = list.length; i < len; i++) {
  // ...
}

正例:

for (var i = 0; i < list.length; i++) {
  // ...
}

回到目錄

刪除無效的代碼

不再被調(diào)用的代碼應(yīng)及時刪除。

反例:

function oldRequestModule(url) {
  // ...
}

function newRequestModule(url) {
  // ...
}

var req = newRequestModule;
inventoryTracker("apples", req, "www.inventory-awesome.io");

正例:

function newRequestModule(url) {
  // ...
}

var req = newRequestModule;
inventoryTracker("apples", req, "www.inventory-awesome.io");

回到目錄

對象和數(shù)據(jù)結(jié)構(gòu) 使用getters和setters

JS沒有接口或類型,因此實現(xiàn)這一模式是很困難的,因為我們并沒有類似publicprivate的關(guān)鍵詞。

然而,使用getters和setters獲取對象的數(shù)據(jù)遠比直接使用點操作符具有優(yōu)勢。為什么呢?

當(dāng)需要對獲取的對象屬性執(zhí)行額外操作時。

執(zhí)行set時可以增加規(guī)則對要變量的合法性進行判斷。

封裝了內(nèi)部邏輯。

在存取時可以方便的增加日志和錯誤處理。

繼承該類時可以重載默認行為。

從服務(wù)器獲取數(shù)據(jù)時可以進行懶加載。

反例:

class BankAccount {
  constructor() {
       this.balance = 1000;
  }
}

let bankAccount = new BankAccount();

// Buy shoes...
bankAccount.balance = bankAccount.balance - 100;

正例:

class BankAccount {
  constructor() {
       this.balance = 1000;
  }

  // It doesn"t have to be prefixed with `get` or `set` to be a getter/setter
  withdraw(amount) {
      if (verifyAmountCanBeDeducted(amount)) {
        this.balance -= amount;
      }
  }
}

let bankAccount = new BankAccount();

// Buy shoes...
bankAccount.withdraw(100);

回到目錄

讓對象擁有私有成員

可以通過閉包完成

反例:

var Employee = function(name) {
  this.name = name;
}

Employee.prototype.getName = function() {
  return this.name;
}

var employee = new Employee("John Doe");
console.log("Employee name: " + employee.getName()); // Employee name: John Doe
delete employee.name;
console.log("Employee name: " + employee.getName()); // Employee name: undefined

正例:

var Employee = (function() {
  function Employee(name) {
    this.getName = function() {
      return name;
    };
  }

  return Employee;
}());

var employee = new Employee("John Doe");
console.log("Employee name: " + employee.getName()); // Employee name: John Doe
delete employee.name;
console.log("Employee name: " + employee.getName()); // Employee name: John Doe

回到目錄

單一職責(zé)原則 (SRP)

如《代碼整潔之道》一書中所述,“修改一個類的理由不應(yīng)該超過一個”。

將多個功能塞進一個類的想法很誘人,但這將導(dǎo)致你的類無法達到概念上的內(nèi)聚,并經(jīng)常不得不進行修改。

最小化對一個類需要修改的次數(shù)是非常有必要的。如果一個類具有太多太雜的功能,當(dāng)你對其中一小部分進行修改時,將很難想象到這一修夠?qū)Υa庫中依賴該類的其他模塊會帶來什么樣的影響。

反例:

class UserSettings {
  constructor(user) {
    this.user = user;
  }

  changeSettings(settings) {
    if (this.verifyCredentials(user)) {
      // ...
    }
  }

  verifyCredentials(user) {
    // ...
  }
}

正例:

class UserAuth {
  constructor(user) {
    this.user = user;
  }

  verifyCredentials() {
    // ...
  }
}


class UserSettings {
  constructor(user) {
    this.user = user;
    this.auth = new UserAuth(user)
  }

  changeSettings(settings) {
    if (this.auth.verifyCredentials()) {
      // ...
    }
  }
}

回到目錄

開/閉原則 (OCP)

“代碼實體(類,模塊,函數(shù)等)應(yīng)該易于擴展,難于修改。”

這一原則指的是我們應(yīng)允許用戶方便的擴展我們代碼模塊的功能,而不需要打開js文件源碼手動對其進行修改。

反例:

class AjaxRequester {
  constructor() {
    // What if we wanted another HTTP Method, like DELETE? We would have to
    // open this file up and modify this and put it in manually.
    this.HTTP_METHODS = ["POST", "PUT", "GET"];
  }

  get(url) {
    // ...
  }

}

正例:

class AjaxRequester {
  constructor() {
    this.HTTP_METHODS = ["POST", "PUT", "GET"];
  }

  get(url) {
    // ...
  }

  addHTTPMethod(method) {
    this.HTTP_METHODS.push(method);
  }
}

回到目錄

利斯科夫替代原則 (LSP)

“子類對象應(yīng)該能夠替換其超類對象被使用”。

也就是說,如果有一個父類和一個子類,當(dāng)采用子類替換父類時不應(yīng)該產(chǎn)生錯誤的結(jié)果。

反例:

class Rectangle {
  constructor() {
    this.width = 0;
    this.height = 0;
  }

  setColor(color) {
    // ...
  }

  render(area) {
    // ...
  }

  setWidth(width) {
    this.width = width;
  }

  setHeight(height) {
    this.height = height;
  }

  getArea() {
    return this.width * this.height;
  }
}

class Square extends Rectangle {
  constructor() {
    super();
  }

  setWidth(width) {
    this.width = width;
    this.height = width;
  }

  setHeight(height) {
    this.width = height;
    this.height = height;
  }
}

function renderLargeRectangles(rectangles) {
  rectangles.forEach((rectangle) => {
    rectangle.setWidth(4);
    rectangle.setHeight(5);
    let area = rectangle.getArea(); // BAD: Will return 25 for Square. Should be 20.
    rectangle.render(area);
  })
}

let rectangles = [new Rectangle(), new Rectangle(), new Square()];
renderLargeRectangles(rectangles);

正例:

class Shape {
  constructor() {}

  setColor(color) {
    // ...
  }

  render(area) {
    // ...
  }
}

class Rectangle extends Shape {
  constructor() {
    super();
    this.width = 0;
    this.height = 0;
  }

  setWidth(width) {
    this.width = width;
  }

  setHeight(height) {
    this.height = height;
  }

  getArea() {
    return this.width * this.height;
  }
}

class Square extends Shape {
  constructor() {
    super();
    this.length = 0;
  }

  setLength(length) {
    this.length = length;
  }

  getArea() {
    return this.length * this.length;
  }
}

function renderLargeShapes(shapes) {
  shapes.forEach((shape) => {
    switch (shape.constructor.name) {
      case "Square":
        shape.setLength(5);
      case "Rectangle":
        shape.setWidth(4);
        shape.setHeight(5);
    }

    let area = shape.getArea();
    shape.render(area);
  })
}

let shapes = [new Rectangle(), new Rectangle(), new Square()];
renderLargeShapes(shapes);

回到目錄

接口隔離原則 (ISP)

“客戶端不應(yīng)該依賴它不需要的接口;一個類對另一個類的依賴應(yīng)該建立在最小的接口上?!?/p>

在JS中,當(dāng)一個類需要許多參數(shù)設(shè)置才能生成一個對象時,或許大多時候不需要設(shè)置這么多的參數(shù)。此時減少對配置參數(shù)數(shù)量的需求是有益的。

反例:

class DOMTraverser {
  constructor(settings) {
    this.settings = settings;
    this.setup();
  }

  setup() {
    this.rootNode = this.settings.rootNode;
    this.animationModule.setup();
  }

  traverse() {
    // ...
  }
}

let $ = new DOMTraverser({
  rootNode: document.getElementsByTagName("body"),
  animationModule: function() {} // Most of the time, we won"t need to animate when traversing.
  // ...
});

正例:

class DOMTraverser {
  constructor(settings) {
    this.settings = settings;
    this.options = settings.options;
    this.setup();
  }

  setup() {
    this.rootNode = this.settings.rootNode;
    this.setupOptions();
  }

  setupOptions() {
    if (this.options.animationModule) {
      // ...
    }
  }

  traverse() {
    // ...
  }
}

let $ = new DOMTraverser({
  rootNode: document.getElementsByTagName("body"),
  options: {
    animationModule: function() {}
  }
});

回到目錄

依賴反轉(zhuǎn)原則 (DIP)

該原則有兩個核心點:

高層模塊不應(yīng)該依賴于低層模塊。他們都應(yīng)該依賴于抽象接口。

抽象接口應(yīng)該脫離具體實現(xiàn),具體實現(xiàn)應(yīng)該依賴于抽象接口。

反例:

class InventoryTracker {
  constructor(items) {
    this.items = items;

    // BAD: We have created a dependency on a specific request implementation.
    // We should just have requestItems depend on a request method: `request`
    this.requester = new InventoryRequester();
  }

  requestItems() {
    this.items.forEach((item) => {
      this.requester.requestItem(item);
    });
  }
}

class InventoryRequester {
  constructor() {
    this.REQ_METHODS = ["HTTP"];
  }

  requestItem(item) {
    // ...
  }
}

let inventoryTracker = new InventoryTracker(["apples", "bananas"]);
inventoryTracker.requestItems();

正例:

class InventoryTracker {
  constructor(items, requester) {
    this.items = items;
    this.requester = requester;
  }

  requestItems() {
    this.items.forEach((item) => {
      this.requester.requestItem(item);
    });
  }
}

class InventoryRequesterV1 {
  constructor() {
    this.REQ_METHODS = ["HTTP"];
  }

  requestItem(item) {
    // ...
  }
}

class InventoryRequesterV2 {
  constructor() {
    this.REQ_METHODS = ["WS"];
  }

  requestItem(item) {
    // ...
  }
}

// By constructing our dependencies externally and injecting them, we can easily
// substitute our request module for a fancy new one that uses WebSockets.
let inventoryTracker = new InventoryTracker(["apples", "bananas"], new InventoryRequesterV2());
inventoryTracker.requestItems();

回到目錄

使用ES6的classes而不是ES5的Function

典型的ES5的類(function)在繼承、構(gòu)造和方法定義方面可讀性較差。

當(dāng)需要繼承時,優(yōu)先選用classes。

但是,當(dāng)在需要更大更復(fù)雜的對象時,最好優(yōu)先選擇更小的function而非classes。

反例:

var Animal = function(age) {
    if (!(this instanceof Animal)) {
        throw new Error("Instantiate Animal with `new`");
    }

    this.age = age;
};

Animal.prototype.move = function() {};

var Mammal = function(age, furColor) {
    if (!(this instanceof Mammal)) {
        throw new Error("Instantiate Mammal with `new`");
    }

    Animal.call(this, age);
    this.furColor = furColor;
};

Mammal.prototype = Object.create(Animal.prototype);
Mammal.prototype.constructor = Mammal;
Mammal.prototype.liveBirth = function() {};

var Human = function(age, furColor, languageSpoken) {
    if (!(this instanceof Human)) {
        throw new Error("Instantiate Human with `new`");
    }

    Mammal.call(this, age, furColor);
    this.languageSpoken = languageSpoken;
};

Human.prototype = Object.create(Mammal.prototype);
Human.prototype.constructor = Human;
Human.prototype.speak = function() {};

正例:

class Animal {
    constructor(age) {
        this.age = age;
    }

    move() {}
}

class Mammal extends Animal {
    constructor(age, furColor) {
        super(age);
        this.furColor = furColor;
    }

    liveBirth() {}
}

class Human extends Mammal {
    constructor(age, furColor, languageSpoken) {
        super(age, furColor);
        this.languageSpoken = languageSpoken;
    }

    speak() {}
}

回到目錄

使用方法鏈

這里我們的理解與《代碼整潔之道》的建議有些不同。

有爭論說方法鏈不夠干凈且違反了德米特法則,也許這是對的,但這種方法在JS及許多庫(如JQuery)中顯得非常實用。

因此,我認為在JS中使用方法鏈?zhǔn)欠浅:线m的。在class的函數(shù)中返回this,能夠方便的將類需要執(zhí)行的多個方法鏈接起來。

反例:

class Car {
  constructor() {
    this.make = "Honda";
    this.model = "Accord";
    this.color = "white";
  }

  setMake(make) {
    this.name = name;
  }

  setModel(model) {
    this.model = model;
  }

  setColor(color) {
    this.color = color;
  }

  save() {
    console.log(this.make, this.model, this.color);
  }
}

let car = new Car();
car.setColor("pink");
car.setMake("Ford");
car.setModel("F-150")
car.save();

正例:

class Car {
  constructor() {
    this.make = "Honda";
    this.model = "Accord";
    this.color = "white";
  }

  setMake(make) {
    this.name = name;
    // NOTE: Returning this for chaining
    return this;
  }

  setModel(model) {
    this.model = model;
    // NOTE: Returning this for chaining
    return this;
  }

  setColor(color) {
    this.color = color;
    // NOTE: Returning this for chaining
    return this;
  }

  save() {
    console.log(this.make, this.model, this.color);
  }
}

let car = new Car()
  .setColor("pink")
  .setMake("Ford")
  .setModel("F-150")
  .save();

回到目錄

優(yōu)先使用組合模式而非繼承

在著名的設(shè)計模式一書中提到,應(yīng)多使用組合模式而非繼承。

這么做有許多優(yōu)點,在想要使用繼承前,多想想能否通過組合模式滿足需求吧。

那么,在什么時候繼承具有更大的優(yōu)勢呢?這取決于你的具體需求,但大多情況下,可以遵守以下三點:

繼承關(guān)系表現(xiàn)為"是一個"而非"有一個"(如動物->人 和 用戶->用戶細節(jié))

可以復(fù)用基類的代碼("Human"可以看成是"All animal"的一種)

希望當(dāng)基類改變時所有派生類都受到影響(如修改"all animals"移動時的卡路里消耗量)

反例:

class Employee {
  constructor(name, email) {
    this.name = name;
    this.email = email;
  }

  // ...
}

// Bad because Employees "have" tax data. EmployeeTaxData is not a type of Employee
class EmployeeTaxData extends Employee {
  constructor(ssn, salary) {
    super();
    this.ssn = ssn;
    this.salary = salary;
  }

  // ...
}

正例:

class Employee {
  constructor(name, email) {
    this.name = name;
    this.email = email;

  }

  setTaxData(ssn, salary) {
    this.taxData = new EmployeeTaxData(ssn, salary);
  }
  // ...
}

class EmployeeTaxData {
  constructor(ssn, salary) {
    this.ssn = ssn;
    this.salary = salary;
  }

  // ...
}

回到目錄

測試

一些好的覆蓋工具.

一些好的JS測試框架

單一的測試每個概念

反例:

const assert = require("assert");

describe("MakeMomentJSGreatAgain", function() {
  it("handles date boundaries", function() {
    let date;

    date = new MakeMomentJSGreatAgain("1/1/2015");
    date.addDays(30);
    date.shouldEqual("1/31/2015");

    date = new MakeMomentJSGreatAgain("2/1/2016");
    date.addDays(28);
    assert.equal("02/29/2016", date);

    date = new MakeMomentJSGreatAgain("2/1/2015");
    date.addDays(28);
    assert.equal("03/01/2015", date);
  });
});

正例:

const assert = require("assert");

describe("MakeMomentJSGreatAgain", function() {
  it("handles 30-day months", function() {
    let date = new MakeMomentJSGreatAgain("1/1/2015");
    date.addDays(30);
    date.shouldEqual("1/31/2015");
  });

  it("handles leap year", function() {
    let date = new MakeMomentJSGreatAgain("2/1/2016");
    date.addDays(28);
    assert.equal("02/29/2016", date);
  });

  it("handles non-leap year", function() {
    let date = new MakeMomentJSGreatAgain("2/1/2015");
    date.addDays(28);
    assert.equal("03/01/2015", date);
  });
});

回到目錄

并發(fā) 用Promises替代回調(diào)

回調(diào)不夠整潔并會造成大量的嵌套。ES6內(nèi)嵌了Promises,使用它吧。

反例:

require("request").get("https://en.wikipedia.org/wiki/Robert_Cecil_Martin", function(err, response) {
  if (err) {
    console.error(err);
  }
  else {
    require("fs").writeFile("article.html", response.body, function(err) {
      if (err) {
        console.error(err);
      } else {
        console.log("File written");
      }
    })
  }
})

正例:

require("request-promise").get("https://en.wikipedia.org/wiki/Robert_Cecil_Martin")
  .then(function(response) {
    return require("fs-promise").writeFile("article.html", response);
  })
  .then(function() {
    console.log("File written");
  })
  .catch(function(err) {
    console.error(err);
  })

回到目錄

Async/Await是較Promises更好的選擇

Promises是較回調(diào)而言更好的一種選擇,但ES7中的async和await更勝過Promises。

在能使用ES7特性的情況下可以盡量使用他們替代Promises。

反例:

require("request-promise").get("https://en.wikipedia.org/wiki/Robert_Cecil_Martin")
  .then(function(response) {
    return require("fs-promise").writeFile("article.html", response);
  })
  .then(function() {
    console.log("File written");
  })
  .catch(function(err) {
    console.error(err);
  })

正例:

async function getCleanCodeArticle() {
  try {
    var request = await require("request-promise")
    var response = await request.get("https://en.wikipedia.org/wiki/Robert_Cecil_Martin");
    var fileHandle = await require("fs-promise");

    await fileHandle.writeFile("article.html", response);
    console.log("File written");
  } catch(err) {
    console.log(err);
  }
}

回到目錄

錯誤處理

錯誤拋出是個好東西!這使得你能夠成功定位運行狀態(tài)中的程序產(chǎn)生錯誤的位置。

別忘了捕獲錯誤

對捕獲的錯誤不做任何處理是沒有意義的。

代碼中try/catch的意味著你認為這里可能出現(xiàn)一些錯誤,你應(yīng)該對這些可能的錯誤存在相應(yīng)的處理方案。

反例:

try {
  functionThatMightThrow();
} catch (error) {
  console.log(error);
}

正例:

try {
  functionThatMightThrow();
} catch (error) {
  // One option (more noisy than console.log):
  console.error(error);
  // Another option:
  notifyUserOfError(error);
  // Another option:
  reportErrorToService(error);
  // OR do all three!
}
不要忽略被拒絕的promises

理由同try/catch.

反例:

getdata()
.then(data => {
  functionThatMightThrow(data);
})
.catch(error => {
  console.log(error);
});

正例:

getdata()
.then(data => {
  functionThatMightThrow(data);
})
.catch(error => {
  // One option (more noisy than console.log):
  console.error(error);
  // Another option:
  notifyUserOfError(error);
  // Another option:
  reportErrorToService(error);
  // OR do all three!
});

回到目錄

格式化

格式化是一件主觀的事。如同這里的許多規(guī)則一樣,這里并沒有一定/立刻需要遵守的規(guī)則。可以在這里完成格式的自動化。

大小寫一致

JS是弱類型語言,合理的采用大小寫可以告訴你關(guān)于變量/函數(shù)等的許多消息。

這些規(guī)則是主觀定義的,團隊可以根據(jù)喜歡進行選擇。重點在于無論選擇何種風(fēng)格,都需要注意保持一致性。

反例:

var DAYS_IN_WEEK = 7;
var daysInMonth = 30;

var songs = ["Back In Black", "Stairway to Heaven", "Hey Jude"];
var Artists = ["ACDC", "Led Zeppelin", "The Beatles"];

function eraseDatabase() {}
function restore_database() {}

class animal {}
class Alpaca {}

正例:

var DAYS_IN_WEEK = 7;
var DAYS_IN_MONTH = 30;

var songs = ["Back In Black", "Stairway to Heaven", "Hey Jude"];
var artists = ["ACDC", "Led Zeppelin", "The Beatles"];

function eraseDatabase() {}
function restoreDatabase() {}

class Animal {}
class Alpaca {}

回到目錄

調(diào)用函數(shù)的函數(shù)和被調(diào)函數(shù)應(yīng)放在較近的位置

當(dāng)函數(shù)間存在相互調(diào)用的情況時,應(yīng)將兩者置于較近的位置。

理想情況下,應(yīng)將調(diào)用其他函數(shù)的函數(shù)寫在被調(diào)用函數(shù)的上方。

反例:

class PerformanceReview {
  constructor(employee) {
    this.employee = employee;
  }

  lookupPeers() {
    return db.lookup(this.employee, "peers");
  }

  lookupMananger() {
    return db.lookup(this.employee, "manager");
  }

  getPeerReviews() {
    let peers = this.lookupPeers();
    // ...
  }

  perfReview() {
      getPeerReviews();
      getManagerReview();
      getSelfReview();
  }

  getManagerReview() {
    let manager = this.lookupManager();
  }

  getSelfReview() {
    // ...
  }
}

let review = new PerformanceReview(user);
review.perfReview();

正例:

class PerformanceReview {
  constructor(employee) {
    this.employee = employee;
  }

  perfReview() {
      getPeerReviews();
      getManagerReview();
      getSelfReview();
  }

  getPeerReviews() {
    let peers = this.lookupPeers();
    // ...
  }

  lookupPeers() {
    return db.lookup(this.employee, "peers");
  }

  getManagerReview() {
    let manager = this.lookupManager();
  }

  lookupMananger() {
    return db.lookup(this.employee, "manager");
  }

  getSelfReview() {
    // ...
  }
}

let review = new PerformanceReview(employee);
review.perfReview();

回到目錄

注釋 只對存在一定業(yè)務(wù)邏輯復(fù)制性的代碼進行注釋

注釋并不是必須的,好的代碼是能夠讓人一目了然,不用過多無謂的注釋。

反例:

function hashIt(data) {
  // The hash
  var hash = 0;

  // Length of string
  var length = data.length;

  // Loop through every character in data
  for (var i = 0; i < length; i++) {
    // Get character code.
    var char = data.charCodeAt(i);
    // Make the hash
    hash = ((hash << 5) - hash) + char;
    // Convert to 32-bit integer
    hash = hash & hash;
  }
}

正例:

function hashIt(data) {
  var hash = 0;
  var length = data.length;

  for (var i = 0; i < length; i++) {
    var char = data.charCodeAt(i);
    hash = ((hash << 5) - hash) + char;

    // Convert to 32-bit integer
    hash = hash & hash;
  }
}

回到目錄

不要在代碼庫中遺留被注釋掉的代碼

版本控制的存在是有原因的。讓舊代碼存在于你的history里吧。

反例:

doStuff();
// doOtherStuff();
// doSomeMoreStuff();
// doSoMuchStuff();

正例:

doStuff();

回到目錄

不需要版本更新類型注釋

記住,我們可以使用版本控制。廢代碼、被注釋的代碼及用注釋記錄代碼中的版本更新說明都是沒有必要的。

需要時可以使用git log獲取歷史版本。

反例:

/**
 * 2016-12-20: Removed monads, didn"t understand them (RM)
 * 2016-10-01: Improved using special monads (JP)
 * 2016-02-03: Removed type-checking (LI)
 * 2015-03-14: Added combine with type-checking (JR)
 */
function combine(a, b) {
  return a + b;
}

正例:

function combine(a, b) {
  return a + b;
}

回到目錄

避免位置標(biāo)記

這些東西通常只能代碼麻煩,采用適當(dāng)?shù)目s進就可以了。

反例:

////////////////////////////////////////////////////////////////////////////////
// Scope Model Instantiation
////////////////////////////////////////////////////////////////////////////////
let $scope.model = {
  menu: "foo",
  nav: "bar"
};

////////////////////////////////////////////////////////////////////////////////
// Action setup
////////////////////////////////////////////////////////////////////////////////
let actions = function() {
  // ...
}

正例:

let $scope.model = {
  menu: "foo",
  nav: "bar"
};

let actions = function() {
  // ...
}

回到目錄

避免在源文件中寫入法律評論

將你的LICENSE文件置于源碼目錄樹的根目錄。

反例:

/*
The MIT License (MIT)

Copyright (c) 2016 Ryan McDermott

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE
*/

function calculateBill() {
  // ...
}

正例:

function calculateBill() {
  // ...
}

回到目錄

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/88116.html

相關(guān)文章

  • 從谷歌的JavaScript編寫風(fēng)格中,13 點值得我們注意的!

    摘要:谷歌和有兩個最受歡迎的編寫風(fēng)格指南。有趣的是,谷歌和不同意這個規(guī)則這是的規(guī)范。谷歌只是眾多科技巨頭之一,這些只是推薦。也就是說,看看谷歌這樣的公司提出的風(fēng)格建議是很有趣的,這家公司雇傭了很多才華橫溢的人,他們花了很多時間編寫優(yōu)秀的代碼。 showImg(https://segmentfault.com/img/bV7pRb?w=2000&h=800); 文中的我指原文的作者 想閱讀更多...

    zhonghanwen 評論0 收藏0
  • 從谷歌的JavaScript編寫風(fēng)格中,13 點值得我們注意的!

    摘要:谷歌和有兩個最受歡迎的編寫風(fēng)格指南。有趣的是,谷歌和不同意這個規(guī)則這是的規(guī)范。谷歌只是眾多科技巨頭之一,這些只是推薦。也就是說,看看谷歌這樣的公司提出的風(fēng)格建議是很有趣的,這家公司雇傭了很多才華橫溢的人,他們花了很多時間編寫優(yōu)秀的代碼。 showImg(https://segmentfault.com/img/bV7pRb?w=2000&h=800); 文中的我指原文的作者 想閱讀更多...

    darry 評論0 收藏0
  • 前端代碼規(guī)范 — JavaScript 風(fēng)格指南

    摘要:前言本文基于項目翻譯,也加入了一些個人理解。規(guī)范有利于我們更好的提高代碼可讀性,避免一些不必要的。但是,并沒有統(tǒng)一的標(biāo)準(zhǔn)和硬性要求,這里只是給大家提供一些參考,適合團隊和自己的才是最好的。 前言 本文基于 github 項目 airbnb/javascript 翻譯,也加入了一些個人理解。規(guī)范有利于我們更好的提高代碼可讀性,避免一些不必要的 bug。但是,并沒有統(tǒng)一的標(biāo)準(zhǔn)和硬性要求,這...

    Sleepy 評論0 收藏0
  • JavaScript 風(fēng)格指南(筆記)

    摘要:用且只用一個完成這一需求。執(zhí)行時可以增加規(guī)則對要變量的合法性進行判斷。繼承該類時可以重載默認行為。讓對象擁有私有成員。理想情況下,應(yīng)將調(diào)用其他函數(shù)的函數(shù)寫在被調(diào)用函數(shù)的上方。避免位置標(biāo)記避免在源文件中寫入法律評論參考 變量 使用有意義、可讀性好的變量 使用 ES6 的 const 聲明常量 對功能類似的變量名采用統(tǒng)一的命名風(fēng)格 使用易于檢索名稱 舉例 // 反例 for (var ...

    wind5o 評論0 收藏0
  • JavaScript編碼風(fēng)格指南

    摘要:首次發(fā)表在個人博客前言程序語言的編碼風(fēng)格指南對于一個長期維護的軟件而言是非常重要的好的編程風(fēng)格有助于寫出質(zhì)量更高錯誤更少更易于維護的程序。 首次發(fā)表在個人博客 前言 程序語言的編碼風(fēng)格指南對于一個長期維護的軟件而言是非常重要的;好的編程風(fēng)格有助于寫出質(zhì)量更高、錯誤更少、更易于 維護的程序。 團隊合作需要制定一些代碼規(guī)范還有利用一些工具來強制要求團隊代碼的風(fēng)格統(tǒng)一.畢竟很多情況下以后不...

    Sourcelink 評論0 收藏0

發(fā)表評論

0條評論

閱讀需要支付1元查看
<