代码规范的必要性

  1. 规范的代码可以促进团队合作
  2. 规范的代码可以减少bug处理
  3. 规范的代码可以降低维护成本
  4. 养成代码规范的习惯,有助于程序员自身的成长

代码展示

超长if判断

const fn = () => {
        i.isThum = !i.isThum;
        if (i.isThum === true) {
          // 判断类型,后续要添加(没有5)
          if (i.sourceType == 5) {
            articleCommentThumb(data)
              .then((res) => {
                i.likeCount++;
                uni.showToast({
                  title: "点赞成功",
                  icon: "none",
                  duration: 1000,
                });
              })
              .catch((err) => {
                uni.showToast({
                  title: "点赞失败",
                  icon: "none",
                  duration: 1000,
                });
              });
          } else if (i.sourceType == 1) {
            perfumeCommentThumb(i.commentId)
              .then((res) => {
                i.likeCount++;
                uni.showToast({
                  title: "点赞成功",
                  icon: "none",
                  duration: 1000,
                });
              })
              .catch((err) => {
                uni.showToast({
                  title: "点赞失败",
                  icon: "none",
                  duration: 1000,
                });
              });
          } else if (i.sourceType == 2) {
            moodLike({
              moodId: i.commentId,
            })
              .then((res) => {
                i.likeCount++;
                uni.showToast({
                  title: "点赞成功",
                  icon: "none",
                  duration: 1000,
                });
              })
              .catch((err) => {
                uni.showToast({
                  title: "点赞失败",
                  icon: "none",
                  duration: 1000,
                });
              });
          } else if (i.sourceType == 4) {
            let datas = {
              id: i.commentId,
              title: i.title,
            };
            ordinaryThumb(datas)
              .then((res) => {
                i.likeCount++;
                uni.showToast({
                  title: "点赞成功",
                  icon: "none",
                  duration: 1000,
                });
              })
              .catch((err) => {
                uni.showToast({
                  title: "点赞失败",
                  icon: "none",
                  duration: 1000,
                });
              });
          }
          this.memberGrowthActivity();
        } else {
          // 判断类型,后续要添加
          if (i.sourceType == 5) {
            articleCommentThumb(data)
              .then((res) => {
                i.likeCount--;
                uni.showToast({
                  title: "取消点赞",
                  icon: "none",
                  duration: 1000,
                });
              })
              .catch((err) => {
                uni.showToast({
                  title: "取消失败",
                  icon: "none",
                  duration: 1000,
                });
              });
          } else if (i.sourceType == 1) {
            perfumeCommentThumb(i.commentId)
              .then((res) => {
                i.likeCount--;
                uni.showToast({
                  title: "取消点赞",
                  icon: "none",
                  duration: 1000,
                });
              })
              .catch((err) => {
                uni.showToast({
                  title: "取消失败",
                  icon: "none",
                  duration: 1000,
                });
              });
          } else if (i.sourceType == 4) {
            let datas = {
              id: i.commentId,
              title: i.title,
            };
            ordinaryThumb(datas)
              .then((res) => {
                i.likeCount--;
                uni.showToast({
                  title: "取消点赞",
                  icon: "none",
                  duration: 1000,
                });
              })
              .catch((err) => {
                uni.showToast({
                  title: "取消失败",
                  icon: "none",
                  duration: 1000,
                });
              });
          } else if (i.sourceType == 2) {
            moodLike({
              moodId: i.commentId,
            })
              .then((res) => {
                i.likeCount--;
                uni.showToast({
                  title: "取消点赞",
                  icon: "none",
                  duration: 1000,
                });
              })
              .catch((err) => {
                uni.showToast({
                  title: "取消失败",
                  icon: "none",
                  duration: 1000,
                });
              });
          }
        }
      }
  1. 阅读性太差
  2. 缺少注释
  3. 复用性几乎没有

修改思路

  1. 不管取消点赞还是点赞,调用的接口都是同一个,所以没必要根据现有状态判断执行两次,唯一的变化是数量的减少和状态的修改
  2. 同一个属性多个不同的状态,可以改写成object[key]()的方式调用
  3. 根据数值判断的状态需要注释,时间长了就不记得了状态值得意思
// 点击成功执行
const successPrompt = () => {
  let title = '点赞成功'
  if(i.isThum){
    i.likeCount--;
    title = '取消成功'
  } else{
    i.likeCount++;
    title = '点赞成功'
  }
  uni.showToast({
      title,
      icon: "none",
      duration: 1000,
    });
}
// 点击失败
const failurePrompt = () => {
  let title = '点赞失败'
  if(i.isThum){
    title = '点赞失败'
  } else{
    title = '取消失败'
  }
  uni.showToast({
    title,
    icon: "none",
    duration: 1000,
  });
}
// 这里写一下注释
const data = {
  1: () => {
    perfumeCommentThumb(i.commentId)
    .then((res) => {
      successPrompt()
    })
    .catch((err) => {
      failurePrompt()
    });
  },
  2:() => {
    moodLike({moodId: i.commentId})
    .then((res) => {
      successPrompt()
    })
    .catch((err) => {
      failurePrompt()
    });
  },
  4:() => {
    ordinaryThumb({
      id: i.commentId,
      title: i.title,
    })
    .then((res) => {
      successPrompt()
    })
    .catch((err) => {
      failurePrompt()
    });
  }
}
data[i.sourceType]()