Javascript 我如何重构我的方法?

Javascript 我如何重构我的方法?,javascript,oop,typescript,refactoring,Javascript,Oop,Typescript,Refactoring,我有代码,并考虑使其更纯净和干净 我觉得在这个方法中去掉多个返回是很好的 它怎么可能被重构呢? 也许我应该用些图案? 请告知。提前谢谢 class Test{ private client; private concreteMixer; constructor(client, concreteMixer){ this.client = client; this.concreteMi

我有代码,并考虑使其更纯净和干净

我觉得在这个方法中去掉多个返回是很好的

它怎么可能被重构呢? 也许我应该用些图案? 请告知。提前谢谢

class Test{    
          private client;
          private concreteMixer;
          constructor(client, concreteMixer){
            this.client = client;
            this.concreteMixer = concreteMixer;
          }

          public method(){
            let form = new Form();
            if(form.isSubmitted()){
              if(form.isValid()){

                let field = form.getField();
                let infoField = this.client.testField(field);
                if(!infoField){
                  form.setError('This is not valid field');
                  return form;
                }

                let coffee = this.concreteMixer.makeСoffee();
                //two days have passed
                if(!coffee){
                  form.setError('I want coffee');
                  return form;
                }

                this.concreteMixer.pourInThermosBottle();
                //two days have passed

                return coffee;
              }
            }

            return form;
          }
        }
我会那样做的

    /**
     * Comment
     */
    class Test {
      /**
       * Comment
       */
      protected client;

      /**
       * Comment
       */
      protected concreteMixer;

      /**
       * Comment
       */
      constructor(client, concreteMixer) {
        this.client = client;

        this.concreteMixer = concreteMixer;
      }

      /**
       * Comment
       */
      public method() {
        const form = new Form();

        // Comment
        if (!form.isSubmitted() || !form.isValid()) {
          return form;
        }

        // Comment
        const field = form.getField();

        // Comment
        const infoField = this.client.testField(field);

        // Comment
        if (!infoField) {
          form.setError(ERROR_CODE_01);

          return form;
        }

        // Comment
        const coffee = this.concreteMixer.makeСoffee();

        //two days have passed
        if (!coffee) {
          form.setError(ERROR_CODE_02);

          return form;
        }

        // Comment
        this.concreteMixer.pourInThermosBottle();

        // two days have passed

        return coffee;
      }
    }

我用
const
替换了
let
发生率。当变量不改变常量的用法时。当它确实使用
时,让


当您出现以下情况时:

if (something) { 
   ... a lot of lines
} 

// end of function
您可以使用:

 if (!something) { 
    return;
 } 
所以你赢得了很多行的缩进级别


使用错误代码而不是直接的错误字符串。因此,应用程序的用户可以用编程的方式处理它


注释代码以解释发生了什么。因此,如果你的同事或你未来的自己发现了这段代码,它将更容易理解。至少:类做什么?方法
做什么?


我将
private
替换为
protected
,因此,如果有一天你想从你的类继承,你可以不做进一步的更改就继承它。

我会这样做

    /**
     * Comment
     */
    class Test {
      /**
       * Comment
       */
      protected client;

      /**
       * Comment
       */
      protected concreteMixer;

      /**
       * Comment
       */
      constructor(client, concreteMixer) {
        this.client = client;

        this.concreteMixer = concreteMixer;
      }

      /**
       * Comment
       */
      public method() {
        const form = new Form();

        // Comment
        if (!form.isSubmitted() || !form.isValid()) {
          return form;
        }

        // Comment
        const field = form.getField();

        // Comment
        const infoField = this.client.testField(field);

        // Comment
        if (!infoField) {
          form.setError(ERROR_CODE_01);

          return form;
        }

        // Comment
        const coffee = this.concreteMixer.makeСoffee();

        //two days have passed
        if (!coffee) {
          form.setError(ERROR_CODE_02);

          return form;
        }

        // Comment
        this.concreteMixer.pourInThermosBottle();

        // two days have passed

        return coffee;
      }
    }

我用
const
替换了
let
发生率。当变量不改变常量的用法时。当它确实使用
时,让


当您出现以下情况时:

if (something) { 
   ... a lot of lines
} 

// end of function
您可以使用:

 if (!something) { 
    return;
 } 
所以你赢得了很多行的缩进级别


使用错误代码而不是直接的错误字符串。因此,应用程序的用户可以用编程的方式处理它


注释代码以解释发生了什么。因此,如果你的同事或你未来的自己发现了这段代码,它将更容易理解。至少:类做什么?方法
做什么?



我将
private
替换为
protected
,因此如果有一天你想从你的类继承,你可以不做进一步的更改就继承它。

你可能会更好地发布这个。你可能会更好地发布这个。这到底是什么?你没有提供任何解释。在我看来,这确实像是重构了。可能有一点格式化,但您仍然有一个方法,在一种情况下返回
coffee
,在另一种情况下返回
Form
,这对我来说似乎是一个重大的危险信号。@GeertBellekens我没有触及逻辑。Idk如何使用它。你的观点是正确的:)K.igor应该看看。重新格式化不是重构。来自维基百科:“代码重构是在不改变外部行为的情况下重构现有计算机代码的过程——改变因子分解。”这到底是什么?你没有提供任何解释。在我看来,这确实像是重构了。可能有一点格式化,但您仍然有一个方法,在一种情况下返回
coffee
,在另一种情况下返回
Form
,这对我来说似乎是一个重大的危险信号。@GeertBellekens我没有触及逻辑。Idk如何使用它。你的观点是正确的:)K.igor应该看看。重新格式化不是重构。来自维基百科:“代码重构是在不改变其外部行为的情况下重构现有计算机代码的过程——改变因子分解”。