How often should you refactor?

前端 未结 25 2190
耶瑟儿~
耶瑟儿~ 2020-12-13 01:36

I had a discussion a few weeks back with some co-workers on refactoring, and I seem to be in a minority that believes \"Refactor early, refactor often\" is a good approach t

相关标签:
25条回答
  • 2020-12-13 02:22

    We're having a discussion at work on this right now. We more or less agree that "write it so it works, then fix it". But we differ on the time perspective. I am more "fix it right away", my coworker is more "fix it in the next iteration".

    Some quotes that back him up:

    Douglas Crockford, Senior Javascript Architect Yahoo:

    refactor every 7th sprint.

    Ken Thompson (unix man):

    Code by itself almost rots and it's gonna be rewritten. Even when nothing has changed, for some reason it rots.

    I would like that once done with a task the code submitted is something you can come back to in 2 months and think "yes, I did well here". I do not believe that it is easy to find time to come back later and fix it. believing this is somewhat naive from my point of view.

    Edit: spelling error

    0 讨论(0)
  • 2020-12-13 02:23

    It's like the National Parks -- Always leave it a little better than you found it.

    To me, that means any time I open code, and have to scratch my head to figure out what's going on, I should refactor something. My primary goal is for readability and understanding. Usually it's just renaming a variable for clarity. Sometimes it's extracting a method -

    For example (trivial), If I came across

    temp = array[i];
    array[i] = array[j];
    array[j] = temp;
    

    I would probably replace that with a swap(i,j) method.

    The compiler will likely inline it anyways, and a swap() tells everyone semantically what's going on.

    That being said, with my own code (starting from scratch), I tend to refactor for design. I often find it easier to work in a concrete class. When its done and debugged, then I'll pull the old Extract Interface trick.

    I'll leave it to a co-worker to refactor for readability, as I'm too close to the code to notice the holes. After all, I know what I meant.

    0 讨论(0)
  • 2020-12-13 02:23

    There are many views on this topic, some linked to a particular methodology or approach to development. When using TDD, refactor early and often is, as you say, a favoured approach.

    In other situations you may refactor as and when needed. For example, when you spot repetitious code.

    When following more traditional methods with detailed up-front design, the refactoring may be less often. However, I would recommend not leaving refactoring until the end of a project. Not only will you be likely to introduce problems, potentially post-UAT, it is often also the case that refactoring gets progressively more difficult. For this reason, time constraints on the classic project cause refactoring and extra testing to be dropped and when maintenance kicks in you may have already created a spaghetti-code monster.

    0 讨论(0)
  • 2020-12-13 02:24

    Refactoring often can often save the day, or at least some time. There was a project I was working on and we refactored all of our code after we hit some milestone. It was a great way because if we needed to rip code out that was no longer useful it made it easier to patch in whatever new thing we needed.

    0 讨论(0)
  • 2020-12-13 02:26

    I refactor every chance I get because it lets me hone my code into the best it can be. I do this even when actively developing to prevent creating unmaintainable code in the first place. It also oftentimes lets me straighten out a poor design decision before it becomes unfixable.

    0 讨论(0)
  • 2020-12-13 02:29

    I refactor code as soon as it's functional (all the tests pass). This way I clean it up while it's still fresh in my mind, and before anyone else sees how ugly the first version was.

    After the initial check-in I typically refactor every time I touch a piece of code. Refactoring isn't something you should set aside separate time for. It should be something you just do as you go.

    0 讨论(0)
提交回复
热议问题