Reid

vuePress-theme-reco Reid    2018 - 2025
Reid Reid

Choose mode

  • dark
  • auto
  • light
TimeLine
分类
  • 后端
  • AI
  • 英文翻译
  • 运维
标签
我的GitHub (opens new window)
author-avatar

Reid

16

文章

25

标签

TimeLine
分类
  • 后端
  • AI
  • 英文翻译
  • 运维
标签
我的GitHub (opens new window)
  • 代码换行规则

    • 前言
      • Bad Code
        • Good Code
          • 规则
            • 补充说明
              • 结尾

              代码换行规则

              vuePress-theme-reco Reid    2018 - 2025

              代码换行规则


              Reid 2023-05-31 后端 规范

              # 前言

              代码的换行是一个说小不小,说大不大的问题,换行换得好,可以让代码更清晰,今天整理一份比较基本的换行规则,欢迎大家评论补充。

              # Bad Code

              以一个非常简单的下单逻辑来解释一下(比较极端,我一个换行都不加):

              /**
               * @param userId    用户id
               * @param goods     商品信息
               * @param packages  包裹信息
               * @param couponIds 优惠券信息
               * @param os        终端
               */
              public async genOrder(userId: number, goods: [], packages: [], couponIds: [], os: string) {
                const order = {
                  userId,
                  os
                };
                const user = await this.ctx.model.User.findByPk(userId);
                if (couponIds.length > 0) {
                  const coupons = await this.getUserCoupons(couponIds);
                  await this.useCoupons(coupons);
                }
                const packageInfo = await this.convertPackageInfo(packages);
                order.packageInfo = packageInfo;
                const goodsInfo = await this.checkGoodsInfo(goods);
                order.goodsInfo = goodsInfo;
                order.id = this.genUUid();
                order.code = this.genCode();
                order.totalAmount = this.calculateAmount(goodsInfo);
                await this.ctx.model.Order.save(order);
                await this.ctx.model.Payment.create({
                  orderId: order.id,
                  totalAmount: order.
                });
              }
              

              # Good Code

              再看看这个,随后会简单讲一下换行规则。

              /**
               * @param userId    用户id
               * @param goods     商品信息
               * @param packages  包裹信息
               * @param couponIds 优惠券信息
               * @param os        终端
               */
              public async genOrder(userId: number, goods: [], packages: [], couponIds: [], os: string) {
                const order = {
                  userId,
                  os
                };
                const user = await this.ctx.model.User.findByPk(userId);
                const packageInfo = await this.convertPackageInfo(packages);
                const goodsInfo = await this.checkGoodsInfo(goods);
                
                if (couponIds.length > 0) {
                  const coupons = await this.getUserCoupons(couponIds);
                  await this.useCoupons(coupons);
                }
                
                order.packageInfo = packageInfo;
                order.goodsInfo = goodsInfo;
                order.id = this.genUUid();
                order.code = this.genCode();
                order.totalAmount = this.calculateAmount(goodsInfo);
              
                await this.ctx.model.Order.save(order);
                await this.ctx.model.Payment.create({
                  orderId: order.id,
                  totalAmount: order.
                });
              }
              

              # 规则

              1. 变量声明与变量声明上下之间不需要换行(let、const、var)
              const order = {
                userId,
                os
              };
              const user = await this.ctx.model.User.findByPk(userId);
              const packageInfo = await this.convertPackageInfo(packages);
              const goodsInfo = await this.checkGoodsInfo(goods);
              
              1. 变量声明的上下必须换行。
              const goodsInfo = await this.checkGoodsInfo(goods);
              
              if (couponIds.length > 0) {
                const coupons = await this.getUserCoupons(couponIds);
                await this.useCoupons(coupons);
              }
              
              1. 变量赋值与变量赋值上下之间不需要换行(let、const、var)。
              order.packageInfo = packageInfo;
              order.goodsInfo = goodsInfo;
              order.id = this.genUUid();
              order.code = this.genCode();
              order.totalAmount = this.calculateAmount(goodsInfo);
              
              1. 变量赋值的上下必须换行。
              order.totalAmount = this.calculateAmount(goodsInfo);
              
              await this.ctx.model.Order.save(order);
              
              1. 块级的上下必须换行(if、else、else if、switch、for、for of、for in、try catch...)
              const goodsInfo = await this.checkGoodsInfo(goods);
              
              if (couponIds.length > 0) {
                const coupons = await this.getUserCoupons(couponIds);
                await this.useCoupons(coupons);
              }
              
              order.packageInfo = packageInfo;
              
              1. 函数调用与函数调用上下不需要换行
              await this.ctx.model.Order.save(order);
              await this.ctx.model.Payment.create({
                orderId: order.id,
                totalAmount: order.
              });
              
              1. 一个块级里的代码不超过3行的话,以上的规则可选择性使用,不换行也行。
              if (couponIds.length > 0) {
                const coupons = await this.getUserCoupons(couponIds);
                await this.useCoupons(coupons);
              }
              

              # 补充说明

              除非代码的执行顺序有要求,否则我们尽量把函数内的用到的变量放到函数的开头,当然,如果变量只是在一个块级里使用,那就没必要放出来,类似上面的coupons:

                if (couponIds.length > 0) {
                  const coupons = await this.getUserCoupons(couponIds);
              
                  await this.useCoupons(coupons);
                }
              

              # 结尾

              后续会陆续更新此系列,欢迎大家评论留言。