手把手教学——如何写一手烂代码

前言

老司机手把手教你写一手烂代码,话很少说,上代码。javascript

一、尽可能使用母语

Good 👍🏻java

let mingzi = 'Allan';

Bad 👎🏻json

let name = 'Allan';

二、三目运算越长越好

Good 👍🏻缓存

const foo = maybe1 > maybe2 ? "bar" : value1 > value2 ? "baz" : null;

Bad 👎app

const foo = maybe1 > maybe2 ? 'bar' : maybeNull;

三、if嵌套要深

Good 👍🏻单元测试

if(condition1) {
    if(condition2) {
        if(condition3) {
            if(condition4) {
                ...
            }
        }
    }
}

Bad 👎🏻测试

if(condition1 && condition2) {
    ...
}

四、代码能写一行尽可能不写多行

Good 👍🏻fetch

<div :class="{'not-join': status === 'allow' && !item.is_select}" v-for="(item, index) in list" :key="index" @click="check(item)" class="activity-item"></div>

Bad 👎🏻this

<div
    :class="{'not-join': status === 'allow' && !item.is_select}"
    v-for="(item, index) in list"
    :key="index"
    @click="check(item)"
    class="activity-item"
></div>

五、变量名要保持神秘

Good 👍🏻url

const asdfgh = 18;

Bad 👎

const age = 18;

六、单词尽可能要少

Good 👍🏻

let a = 18;

Bad 👎🏻

let age = 18;

七、尽可能不要使用模板字符串

Good 👍

const sayHi = function (name) {
  return 'How are you, ' + name + '?';
};

Bad 👎

const sayHi = function (name) {
  return `How are you, ${name}?`;
};

八、布尔值不要简写

Good 👍

if (isValid === true) {
    // ...
}

Bad 👎

if (isValid) {
    // ...
}

九、变量类型自由“飞翔”

Good 👍

let color = '';
if(condition1) {
    color = {a: 'red'};
} else {
    color = ['red', 'yellow', 'blue'];
}

Bad 👎

let color = '';
if (condition1) {
    color = 'red';
} else {
    color = 'yellow';
}

十、回调的“形状”要优雅

Good 👍

$(".disappear").click(function(){
  $("#row20").animate({ "opacity": "0" },
    100,
    function(){$("#row19").animate({ "opacity": "0" },
      100,
      function(){$("#row18").animate({ "opacity": "0" },
        100,
        function(){$("#row17").animate({ "opacity": "0" },
          100,
          function(){$("#row16").animate({ "opacity": "0" },
            100,
            function(){$("#row15").animate({ "opacity": "0" },
              100,
              function(){$("#row14").animate({ "opacity": "0" },
                100,
                function(){$("#row13").animate({ "opacity": "0" },
                  100,
                  function(){$("#row12").animate({ "opacity": "0" },
                    100,
                    function(){$("#row11").animate({ "opacity": "0" },
                      100,
                      function(){$("#row10").animate({ "opacity": "0" },
                        100,
                        function(){$("#row9").animate({ "opacity": "0" },
                          100,
                          function(){$("#row8").animate({ "opacity": "0" },
                            100,
                            function(){$("#row7").animate({ "opacity": "0" },
                              100,
                              function(){$("#row6").animate({ "opacity": "0" },
                                100,
                                function(){$("#row5").animate({ "opacity": "0" },
                                  100,
                                  function(){$("#row4").animate({ "opacity": "0" },
                                    100,
                                    function(){$("#row3").animate({ "opacity": "0" },
                                      100,
                                      function(){$("#row2").animate({ "opacity": "0" },
                                        100,
                                        function(){$("#row1").animate({ "opacity": "0" },
                                          100)})})})})})})})})})})})})})})})})}

Bad 👎

fetch(url)
  .then(response => {
    return response.json().then(data => {
      if (response.ok) {
        return data;
      } else {
        return Promise.reject({status: response.status, data});
      }
    });
  })
  .then(result => console.log('success:', result))
  .catch(error => console.log('error:', error));

十一、清除缓存方式要妙

Good 👍

// 清除缓存最快的方法,没有之一
<header>
   <a href="javascript:alert('清除成功!')">清除缓存</a>
</header>

十一、万一没保存成功呢

Good 👍

for(let i = 0; i < 10; i++) {
    this.save();
}

十二、使用 TypeScript 多写 any

1三、eslint 全局 ignore

1四、单元测试真的没啥用

1五、代码中少写注释

Tobe continue...

相关文章
相关标签/搜索