p42ai / js-assistant

120+ refactorings and code-assists for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=p42ai.refactor
MIT License
119 stars 7 forks source link

Refactoring Idea: Early Return and Back #19

Closed hediet closed 2 years ago

hediet commented 2 years ago
this._store.add(
    this.inputResultView.editor.onDidScrollChange(
        reentrancyBarrier.makeExclusive((c) => {
            if (c.scrollTopChanged) {
                synchronizeScrolling(this.inputResultView.editor, this.input1View.editor, input1ResultMapping.get(), 2);
                synchronizeScrolling(
                    this.inputResultView.editor,
                    this.input2View.editor,
                    input2ResultMapping.get(),
                    2
                );
            }
        })
    )
);

<=>

this._store.add(
    this.inputResultView.editor.onDidScrollChange(
        reentrancyBarrier.makeExclusive((c) => {
            if (!c.scrollTopChanged) {
                return;
            }

            synchronizeScrolling(this.inputResultView.editor, this.input1View.editor, input1ResultMapping.get(), 2);
            synchronizeScrolling(
                this.inputResultView.editor,
                this.input2View.editor,
                input2ResultMapping.get(),
                2
            );
        })
    )
);
lgrammel commented 2 years ago

Thanks for sharing! This refactoring can already be achieved by a combo:

  1. Invert if condition (on c.scrollTopChanged)
  2. Remove unnecessary else (on else)

I agree it would be nicer to do it in one step. I'll think about how to do that best.

lgrammel commented 2 years ago

@hediet I've just released v1.111, which adds a introduce early return refactoring. Thanks for the suggestion!