zoukankan      html  css  js  c++  java
  • JavaScript 代码简洁之道

    一、用有意义且常用的单词命名变量

    二、直接了当

    bad:

    const locations = ['Austin', 'New York', 'San Francisco'];
    locations.forEach((l) => {
      doStuff();
      doSomeOtherStuff();
      // ...
      // ...
      // ...
      // 需要看其他代码才能确定 'l' 是干什么的。
      dispatch(l);
    });

    good:

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

    三、避免无意义的前缀

    bad:

    const car = {
        carMake: 'Honda',
        carModel: 'Accord',
        carColor: 'Blue'
      };
    
      function paintCar(car) {
        car.carColor = 'Red';
      }

    good:

    const car = {
      make: 'Honda',
      model: 'Accord',
      color: 'Blue'
    };
    
    function paintCar(car) {
      car.color = 'Red';
    }

    四、使用默认值

    bad:

    function createMicrobrewery(name) {
      const breweryName = name || 'Hipster Brew Co.';
      // ...
    }

    good:

    function createMicrobrewery(name = 'Hipster Brew Co.') {
      // ...
    }

    五、参数越少越好

    如果参数超过两个,使用 ES2015/ES6 的解构语法,不用考虑参数的顺序。

    bad:

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

    good:

    function createMenu({ title, body, buttonText, cancellable }) {
      // ...
    }
    
    createMenu({
      title: 'Foo',
      body: 'Bar',
      buttonText: 'Baz',
      cancellable: true
    });

    六、删除重复代码

    bad:

    function showDeveloperList(developers) {
      developers.forEach((developer) => {
        const expectedSalary = developer.calculateExpectedSalary();
        const experience = developer.getExperience();
        const githubLink = developer.getGithubLink();
        const data = {
          expectedSalary,
          experience,
          githubLink
        };
    
        render(data);
      });
    }
    
    function showManagerList(managers) {
      managers.forEach((manager) => {
        const expectedSalary = manager.calculateExpectedSalary();
        const experience = manager.getExperience();
        const portfolio = manager.getMBAProjects();
        const data = {
          expectedSalary,
          experience,
          portfolio
        };
    
        render(data);
      });
    }

    good:

    function showEmployeeList(employees) {
      employees.forEach(employee => {
        const expectedSalary = employee.calculateExpectedSalary();
        const experience = employee.getExperience();
        const data = {
          expectedSalary,
          experience,
        };
    
        switch(employee.type) {
          case 'develop':
            data.githubLink = employee.getGithubLink();
            break
          case 'manager':
            data.portfolio = employee.getMBAProjects();
            break
        }
        render(data);
      })
    }
  • 相关阅读:
    关于故事和段子
    SQLserver2008数据库备份和还原问题(还原是必须有完整备份)
    百度文库破解方法
    如何识别病毒,转自百度文库,千辛万苦破解出来的
    20个人艰不拆的事实:知道真相的我眼泪掉下来 T.T
    linux学习网站分享
    个人对于腾讯和优酷的看法
    今天去客户现场的一些感想
    基于 Blazui 的 Blazor 后台管理模板 Blazui.Admin 正式尝鲜
    新手福利!Blazor 从入门到砖家系列教程(你真的可以成为砖家)
  • 原文地址:https://www.cnblogs.com/yourName/p/10282824.html
Copyright © 2011-2022 走看看