屎山代码风格指南(避免被优化&&避免被接盘)

发布时间 2024-01-08 16:43:17作者: 有只橘猫

欢迎补充!!!

序言

  1. 良好的代码结构Bad ??

    • 使用有意义的变量和函数名,遵循命名规范,使代码易于理解。
    • 组织代码,使用适当的文件和文件夹结构,保持模块化。
    • 避免全局变量的滥用,尽量使用局部作用域。
  2. 单一职责原则Bad ??

    • 每个函数或模块应该只负责一个特定的功能。这样的设计使得代码更容易维护和扩展。
  3. DRY 原则(不要重复自己):Bad ??

    • 避免复制粘贴代码,尽量将重复的逻辑提取成函数、类或模块。
    • 使用函数封装通用功能,以便在整个应用程序中重复使用。
  4. 注释和文档Bad ??

    • 写清晰、有意义的注释,解释代码的目的和特殊考虑。
    • 为 API、函数等提供良好的文档,使其他开发者能够轻松理解和使用你的代码。
  5. 代码复审Bad ??

    • 进行代码复审是避免屎山代码的重要步骤。同事可以提供不同的视角和建议,有助于发现潜在的问题。
  6. 测试驱动开发(TDD)Bad ??

    • 使用测试来确保代码的质量和稳定性。在编写代码之前先写测试,以确保代码的正确性。
  7. 使用设计模式Bad ??

    • 学习并应用设计模式,它们提供了一些通用的解决方案,有助于降低复杂性和提高代码的可维护性。
  8. 版本控制Bad ??

    • 使用版本控制系统(如 Git)来跟踪代码的变化,以便轻松地回滚到之前的版本,并且能够合理管理多人协作的情况。
  9. 性能优化和代码优化Bad ??

    • 注意代码的性能,避免写出低效的代码。使用工具和技术进行性能分析和优化。
  10. 学习和持续改进Bad ??

    • 不断学习新的技术和最佳实践,关注前端领域的最新发展。
    • 定期检讨自己的代码,寻找优化的机会,并从他人的经验中吸取教训。

 

以上不要尝试学习,不要写,给出这些建议就是避免这样写,不听的话,下周就是你提桶跑路的日子(划重点)Good ??

?

以下是我精心准备的福利大礼包Good ??

 

1.通用js,css,html系列

1-1.不要注释

Bad ?(不要参考)
// 关联的项目ID
const businessId = ""

Good ??(谨记)
// 更多时候,应该包含一些“为什么”,而不是一些“是什么”。
const businessId = ""

1-2.注释撒谎 

Bad ?(不要参考)
// 关联的项目ID
const businessId = ""

Good ??(谨记)
// 列表或者详情的主键ID,传参使用  ?
const businessId = ""

1-4.变量不要语义化

Bad ?(不要参考)
// 关联的项目ID
const businessId = ""
// 不要使用什么下面的命名方法
// const camelCase = ''(小驼峰式命名法 —— 首字母小写)
// const PascalCase = ''(大驼峰式命名法 —— 首字母大写)
// const kebab-case = ''(短横线连接式)
// const t_title = ''(下划线连接式)

Good ??(谨记)
const bId = "";
const cc = "";
const Pc = "";
// 全拼音命名法
// ”毕竟都是中国人嘛,全拼音命名大家应该都看得懂吧“,举个例子:dazhe.vue。但是同一个拼音可以翻译出不同的意思出来,他们之间是一对多的关系,因此不适合作为组件名;当然,全拼音命名还算是手下留情的,有的时候全拼音命名可能会很长,那就直接取首字母吧!


// 拼音首字母命名法
// 于是dazhe.vue变成了dz.vue,这个时候就成了猜谜语,有一首歌词写得好:女孩的心思男孩你别猜别猜别猜你猜来猜去也猜不明白,到了这里就是代码的心思你别猜,直接放弃吧!


// 中西合璧命名法
// 有些同学觉得光中文那不太高大上啊,要把英语也加进来才能显示自己的水平,所以这样命名:dzList.vue,照样还是让人看不懂


// 英文首字母命名法
// 我想这种方式命名的同学应该不多吧,毕竟已经拿起翻译工具翻译了,直接cv就可以了,为什么还要摘出首字母来呢?

1-5.不要使用母语写注释

Bad ?(不要参考)
// 关联的项目ID
const businessId = ""

Good ??(谨记)
// Associated project ID(英语)
// Inside fiction ID(法语)
// (阿拉伯语)مرتبطة بهوية المشروع
const businessId = ""

1-6.代码写成一行

Bad ?(不要参考)
if (
          (
            lastKeyTime &&
            this.form.bidopenEtime &&
            new Date(this.form.bidopenEtime.replace(/-/g, "/")).getTime() < new Date(lastKeyTime.replace(/-/g, "/"))
          ) 
          || 
          textTrack 
          || 
          textTrack.length > 0 
          || 
          this.projectListDialog
        ) {

}

Good ??(谨记)
// 
if ((lastKeyTime &&this.form.bidopenEtime &&new Date(this.form.bidopenEtime.replace(/-/g, "/")).getTime() < new Date(lastKeyTime.replace(/-/g, "/"))) || textTrack || textTrack.length > 0 || this.projectListDialog) {}

1-7.不要处理错误

// 无论何时发现错误,都没有必要让任何人知道它。没有日志,没有错误弹框,最好不要写try catch
Bad ?(不要参考)
try {
  // 意料之外的情况。
} catch (error) {
  setErrorMessage(error.message);
  // and/or
  logError(error);
}

Good ??(谨记)
try {
  // 意料之外的情况。
} catch (error) {
  // tss... ?
}

1-8.尽可能的使用全局变量

Bad ?(不要参考)
let num = 5;

function square(sub) {
  return sub ** 2;
}

num = square(x); // 现在x是25



Good ??(谨记)
let a = 5;
var b = 2;

function square(b) {
  a = a ** 2;
  b = a + b;
}

square(1);

1-9.尽量多的创建你不会使用的变量

Bad ?(不要参考)
function sum(a, b, c, d) {
  return a + b;
}

Good ??(谨记)
function sum(a, b, c, d) {
  const tts = 1300;
  const arr = []; 
  const res = a + b;
  return a + b;
}

1-10.混合缩进或者有时候四格有时候两个缩进

Bad ?(不要参考)

<el-form-item label="投标人答复内容:" >
  <el-input
    type="textarea"
    disabled
    :autosize="{ minRows: 2, maxRows: 6}"
    placeholder="请输入内容"
    v-model="item.replyContent">
   </el-input>
</el-form-item>

const fruits = ['apple', 'orange', 'grape', 'pineapple'];
const toppings = ['syrup', 'cream', 'jam', 'chocolate'];
const desserts = [];

fruits.forEach(fruit => {
  toppings.forEach(topping => {
    desserts.push([fruit, topping]); 
  });
})


Good ??(谨记)
// 这种
<el-form-item label="投标人答复内容:" >
     <el-input
             type="textarea"
                                        disabled
                                        :autosize="{ minRows: 2, maxRows: 6}"
                                        placeholder="请输入内容"
                                        v-model="item.replyContent">
     </el-input>
</el-form-item>

// 这种
const fruits = ['apple',
  'orange', 'grape', 'pineapple'];
  const toppings = ['syrup', 'cream',
                    'jam',
                    'chocolate'];
const desserts = [];
fruits.forEach(fruit => {
toppings.forEach(topping => {
    desserts.push([
fruit,topping]);
    });})

1-11.写不能到达的代码

Bad ?(不要参考)
function dealData(num) {
    if (num) {
      return num ** 2;
    }
  return undefined;
}


Good ??(谨记)
function dealData(num) {
    if (typeof num === 'undefined' || num === null || null === '') {
        return undefined;
    }else {
        return num ** 2;
    }
    return null; // 我也不知道干什么
}

 1-12.不要删除代码,能保留的就保留,不能保留的注释掉,不要删除,这个是历史,是沉淀,是成长

Bad ?(不要参考)


Good ??(谨记)
if (data.code == "1") {
                            let resData= data.data;
                            if(resData && resData.length > 0){
                                resData.forEach(v => {
                                    v.titleName = v.name
                                })
                                this.attachmentList = resData;
                            }else{
                                this.attachmentList = [];
                            }
                        } else{
                            this.attachmentList = [];
                        }
                    });
                    // this.attachmentList =  [
                    //     {
                    //         "appId": "123451234567890671234567890890",
                    //         "attFiles": [
                    //             {
                    //                 "bizId": "123451234567890671234567890890",
                    //                 "createDate": "2023-09-20 10:08:35",
                    //                 "delFlag": "1",
                    //                 "filePath": "/upload/eb5_jsgc_project_plan_att/123451234567890671234567890890.pdf",
                    //                 "fileSize": 0,
                    //                 "fileType": "1",
                    //                 "hashCode": "",
                    //                 "id": "123451234567890671234567890890",
                    //                 "name": "结果公告.pdf",
                    //                 "remark": "{comeform:GC}",
                    //                 "sort": 1,
                    //                 "updateDate": "2023-09-20 10:08:35"
                    //             },
                    //             {
                    //                 "bizId": "37854334689723469098754780087",
                    //                 "createDate": "2023-09-20 10:08:35",
                    //                 "delFlag": "1",
                    //                 "filePath": "/upload/eb5_jsgc_project_plan_att/37854334689723469098754780087.pdf",
                    //                 "fileSize": 0,
                    //                 "fileType": "1",
                    //                 "hashCode": "",
                    //                 "id": "37854334689723469098754780087",
                    //                 "name": "测试 (1).pdf",
                    //                 "remark": "{comeform:GC}",
                    //                 "sort": 2,
                    //                 "updateDate": "2023-09-20 10:08:35"
                    //             }
                    //         ],
                    //         "attributes": {},
                    //         "categoryCode": "dfsdfsdsd",
                    //         "categoryId": "5656536576sasd578as86da5s65da",
                    //         "createTime": "2023-04-14 13:49:21",
                    //         "createUser": "system",
                    //         "delFlag": false,
                    //         "fileType": "1",
                    //         "id": "224234234efwer234efsd24234242",
                    //         "titleName": "测试22",
                    //         "required": false,
                    //         "showFlag": false,
                    //         "sort": 1,
                    //         "systemFlag": true,
                    //         "updateTime": "2023-08-15 18:52:03",
                    //         "updateUser": "system",
                    //         "upload": false,
                    //         "value": "1"
                    //     }
                    // ]
                }

1-12.循环里面最好if。if里面最好再有循环,这个是精髓

Bad ?(不要参考)
// 好的没有参考,自己发挥!!!

Good ??(谨记)
// 这样写别就看不懂,或者看懂的时间要花很久,这写多了就好了,最好不要对齐
function dealData(num) {
    for (循环1) {
    if (判断1) {
    if (判断2) {
    asyncFunction(params, (result) => {
    if (result) {
    for (循环2) {
    if (判断3) {
    }
    }
    }
    })
    }
    }
  }
}

 1-13.不要写ts,语言类型指定的,最好使用any大法

Bad ?(不要参考)
// 
function sum(a: number, b: number): ?number {
  // 当我们在JS中不做置换和/或流类型检查时,覆盖这种情况。
  if (typeof a !== 'number' && typeof b !== 'number') {
    return undefined;
  }
  return a + b;
}

// 这个应该在转换/编译期间失败。
const guessWhat = sum([], {}); // -> undefined



Good ??(谨记)
// 简洁,清爽,体积小,极度推荐
function sum(a, b) {
  return a + b;
}

 1-14.尽可能的使用三目运算符

Bad ?(不要参考)
// 没有正确示范,我怕你变成了高级工程师了(函数处理)


Good ??(谨记)
// 简洁,清爽,极度推荐
    <Steps
      progressDot
      current={ lastTask && lastTask.taskKey === 'apply' ? 1 : lastTask && lastTask.taskKey === 'examine' && lastTask.status == 'agree' ? 2 : lastTask && lastTask.taskKey === 'approval' ? 3 : 0 }
    >

 1.15.函数长的比短的好(不要拆分组件,越长越好)

Bad ?(不要参考)
// 没有正确示范


Good ??(谨记)
// 不要把程序逻辑分成可读的部分
// 一个文件中10000行代码是OK的。
// 一个函数体有1000行代码是OK的。

 1.16.避免代码风格统一,拒绝格式化

Bad ?(不要参考)
// 没有正确示范


Good ??(谨记)
// 不要开启eslint
// 不要安装Prettier

 1.17.构建项目不需要写 README 文档,让你猜

Bad ?(不要参考)
// 没有正确示范


Good ??(谨记)

1.18.随意读取window对象的值

Bad ?(不要参考)
// 没有正确示范


Good ??(谨记)
// 作为大型项目,很容易需要依赖别的模板挂载到window对象的内容,读取的时候需要考虑到是否有可能拿不到window对象上的内容,从而导致js报错?例如:

// window.tmeXXX.a.func();
// 如果这个tmeXXX所在的js加载失败了,或者是某个版本中没有a这个属性或者func这个函数,那么页面就会白屏。

1.19.尽量操作DOM,使之降低

Bad ?(不要参考)
// 没有正确示范


Good ??(谨记)
// 简单易懂
function manipulateDOM(selector, operation) {
  // 获取元素
  const element = document.querySelector(selector);

  // 根据操作执行相应的操作
  switch (operation) {
    case 'get':
      return element;
    case 'set':
      element.textContent = '新的内容';
      break;
    case 'add':
      element.innerHTML += '<div>新的元素</div>';
      break;
    case 'remove':
      element.removeChild(element.lastChild);
      break;
    default:
      throw new Error(`Invalid operation: ${operation}`);
  }
}

// 使用示例
manipulateDOM('.my-element', 'set'); // 设置元素内容
manipulateDOM('.my-element', 'add'); // 添加新的子元素
manipulateDOM('.my-element', 'remove'); // 删除最后一个子元素

1.20.硬编码操作

Bad ?(不要参考)
// 没有正确示范


Good ??(谨记)
// 简单易懂
function getScenevalue() {
    switch (detail.type){
        case "1":
            return 1;
        case "2":
        case "3":
            return 2;
        default:
            return 3;
    }        
}

1.21.不要封装组件,十分重要

Bad ?(不要参考)
// 没有正确示范


Good ??(谨记)
// 简单易懂
<el-table size="small" :data="tableData" v-loading="dataListLoading" ref="expertExtractTable" stripe border>
  <el-table-column
    label="序号"
    type="index"
    fixed
    :index="indexMethod"
    width="50"
    align="center"
    :show-overflow-tooltip="true"
  ></el-table-column>
  <el-table-column label="操作" prop="governmentInvest" align="left" width="200" :show-overflow-tooltip="true">
    <template slot-scope="scope">
      <el-button
        style="color: #52c41a"
        v-if="scope.row.approvalStatus == 1"
        type="text"
        size="small"
        title="同意"
        @click="exportExcel(scope.row)"
        >导出</el-button>
    </template>
  </el-table-column>
  <el-table-column label="项目编号" prop="ProjectCode" align="left" width="120" :show-overflow-tooltip="true"></el-table-column>
  <el-table-column label="项目名称" prop="ProjectName" align="left" width="200" :show-overflow-tooltip="true"></el-table-column>
  <el-table-column label="包件编号" prop="Code" align="left" width="200" :show-overflow-tooltip="true"></el-table-column>
  <el-table-column label="包件名称" prop="Name" align="left" width="200" :show-overflow-tooltip="true"></el-table-column>
  <el-table-column label="项目属性" prop="area" align="left" width="200" :show-overflow-tooltip="true"></el-table-column>
  <el-table-column label="省内企业中标" prop="isTenType" align="left" width="200" :show-overflow-tooltip="true"></el-table-column>
  <el-table-column label="价款形式" prop="isTenType" align="left" width="200" :show-overflow-tooltip="true"></el-table-column>
  <el-table-column label="金额(万元)" prop="bidAmount" align="left" width="200" :show-overflow-tooltip="true"></el-table-column>
  <el-table-column label="合同估算价(万元)" prop="bidAmount" align="left" width="200" :show-overflow-tooltip="true"></el-table-column>
  <el-table-column label="节资额(万元)" prop="bidAmount" align="left" width="200" :show-overflow-tooltip="true"></el-table-column>
  <el-table-column label="日期" prop="busuetime" align="left" width="200" :show-overflow-tooltip="true"></el-table-column>
</el-table>

1.22.乱用hooks,乱用useEffect,到处都是onMounted

Bad ?(不要参考)
// 没有正确示范


Good ??(谨记)
// 简单易懂  我想写哪里就写哪里onMounted
onMounted(() =>{
    console.log("生命周期函数:onMounted")
} )
onMounted(() =>{
    console.log("生命周期函数:onMounted")
} )
// 简单易懂  useEffect我想写什么依赖就写什么依赖,我不写也可以 ,嘿嘿
useEffect(() => {
    console.log('no deps=====')
    // code...
});

useEffect(() => {
    console.log('no deps=====')
    // code...
},[appId]);

useEffect(() => {
    console.log('no deps=====')
    // code...
},[appId]);

useEffect(() => {
    console.log('no deps=====')
    // code...
},[appId]);

const { pathname, query } = location;
useEffect(() => {
    console.log('no deps=====')
    // code...
},[window.location.hash]);
useEffect(() => {
    console.log('no deps=====')
    // code...
},[query]);

 

 

 2.小礼物

2.1.引入evil.js

 https://github.com/duo001/evil.js

什么?黑心996公司要让你提桶跑路了?

想在离开前给你们的项目留点小礼物?

偷偷地把本项目引入你们的项目吧,你们的项目会有但不仅限于如下的神奇效果:

当数组长度可以被7整除时,Array.includes 永远返回false。
当周日时,Array.map 方法的结果总是会丢失最后一个元素。
Array.filter 的结果有2%的概率丢失最后一个元素。
setTimeout 总是会比预期时间慢1秒才触发。
Promise.then 在周日时有10%不会注册。
JSON.stringify 会把I(大写字母I)变成l(小写字母L)。
Date.getTime() 的结果总是会慢一个小时。
localStorage.getItem 有5%几率返回空字符串。
...
声明:本包的作者不参与注入,因引入本包造成的损失本包作者概不负责。

 声明:本包的作者不参与注入,因引入本包造成的损失本包作者概不负责。