What is refactoring and what is only modifying code?

后端 未结 10 922
Happy的楠姐
Happy的楠姐 2020-12-12 13:18

I know that refactoring is \"changing the structure of a program so that the functionality is not changed\". I was talking with some of the guys I\'m working with on my fina

10条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-12 13:49

    Fowler draws a clean line between changes to code that do, and those that do not, affect its behavior. He calls those that do not, "refactoring". This is an important distinction, because if we divide our work into refactoring and non-refactoring code modification activities (Fowler calls it "wearing different hats"), we can apply different, goal-appropriate techniques.

    If we are making a refactoring, or behavior-preserving code modification:

    • all our unit tests should pass before and after the modification
    • we should not need to modify any tests, or write any new ones
    • we expect cleaner code when we are done
    • we do not expect new behavior

    If we are making a behavior-changing code modification:

    • we expect new behavior
    • we should write new tests
    • we may get dirtier code when we are done (and should then refactor it)

    If we lose sight of this distinction, then our expectations for any given code modification task are muddled and complex, or at any rate more muddled and more complex than if we are mindful of it. That is why the word and its meaning are important.

提交回复
热议问题