How often should you reorganize?

I had a discussion a few weeks ago with some refactoring staff, and I seem to be in the minority, who believes that "Refactoring at an early stage, refactoring often" is a good approach that prevents the code from becoming messy and unbearable. A number of other people thought that he simply belonged to the stages of project maintenance.

If you have an opinion, please protect it.

+51
refactoring
Sep 26 '08 at 16:59
source share
25 answers

Like you said: refactoring early, refactoring often.

Refactoring early means that the necessary changes are still fresh in my mind. Refactoring often means that the changes are usually smaller.

The delay in refactoring ends up making a big mess, which further complicates the refactoring. Cleaning, as soon as I notice that the mess is stopping him from growing and becoming a problem later.

+69
Sep 26 '08 at 17:00
source share

Refactoring code as soon as it functions (all tests pass). Thus, I cleanse it while it is still fresh in my mind, and before anyone else sees how ugly the first version was.

After the initial registration, I usually reorganize every time I touch a piece of code. Refactoring is not something you need to set aside a separate time for. It should be what you just do when you go.

+32
Sep 26 '08 at 17:01
source share

You write code with two heads. The just-for-thing hat and the I-need-to-understand-this-tomorrow hat. Obviously, the second hat is refactoring. This way you refactor every time you do something, but have (inevitably) smells introduced, such as duplicate code, long methods, fragile error handling, wrong variable names, etc ...

Refactoring when trying to get something working (i.e. wearing both hats) is impractical for non-trivial tasks. But delaying refactoring until the next day / week / iteration is very bad, because the context of the problem will disappear from your head. Therefore, switch between hats as often as possible, but never combine them.

+18
Sep 26 '08 at 17:13
source share

I will reorganize every chance I get because it allows me to hone my code at best. I do this even when actively developing to prevent the creation of unreachable code in the first place. It also often allows me to straighten out a bad design decision before it becomes unstable.

+17
Sep 26 '08 at 17:01
source share

Three good reasons to reorganize:

  • Your original design (perhaps in a very small area, but the design nonetheless) was wrong. This includes where you discover a common operation and want to share code.
  • You are developing iteratively.
  • The code is so bad that it needs a complete overhaul.

Three good reasons not to refactor:

  • "It looks a little dirty."
  • "I don't quite agree with how the last guy did it."
  • "It could be more effective." (The problem is "may").

“Merciless” controversial - there is a valid argument, which is called “fixing broken windows” or “code hygiene”, which suggests that if you let small things slip, you will also start to skip big things. It is beautiful, and it’s good to remember, but remember that it is an analogy. He does not justify shunting material endlessly, in search of the cleanest possible solution.

How often you, refactoring, should depend on how often good reasons arise and how confident you are that your test process protects you from errors.

Refactoring is never an end in itself. But if something does not work, it must be fixed, and this is true both in the initial development and in the maintenance. For nontrivial changes, it is almost always better to refactor and incorporate new concepts into cleanliness than to fix one place with large chunks of garbage to avoid any changes elsewhere.

For what it's worth, I don’t think about changing the interface, provided that I have a handle to what uses it and that the area of ​​the resulting change is manageable.

+11
Sep 26 '08 at 17:12
source share

I try to follow this motto: leave all the code you touch better than it was.

When I make a correction or add a function, I usually use this feature to do limited refactoring on affected codes. Often this makes it easier to make my intended changes, so it really doesn't cost anything.

Otherwise, you should set aside some time for refactoring, if you cannot, because you are always struggling with lights (I wonder why), then you have to force yourself to reorganize when you find that making changes becomes much more difficult than necessary, and when "code smells" are simply unbearable.

+6
Sep 26 '08 at 20:18
source share

As the book says, you refactor when

  • you add code ... new function
  • when correcting an error / defect
  • when you view code with multiple people
  • when you find yourself duplicating something a third time. 3 strike rules
+5
Sep 26 '08 at 17:03
source share

Many times, when I flush ideas, my code begins very closely and randomly. When I start polishing an idea, the logical divisions begin to become more and more clear, and I start refactoring. This is an ongoing process, and everyone believes that you should do Early and Often.

+5
Sep 26 '08 at 17:03
source share

I will reorganize when:

I am changing the code and I am confused by this. If it takes me some time to sift it out, it needs refactoring.

I am creating a new code, and after I managed to “work”. Often I get work, and as I code, I understand: "Hey, I need to repeat what I did 20 lines, with only a few changes." At this moment, I will reorganize and continue.

The only thing that, in my opinion, should prevent you from doing this is time limits. Whether you like it or not, sometimes you just don't have time for it.

+4
Sep 26 '08 at 17:09
source share

It is like national parks - always leave it a little better than you found it.

For me, this means that every time I open the code and I have to scratch my head to find out what is happening, I need to reorganize something. My main goal is readability and understanding. This is usually just a renaming of the variable for clarity. Sometimes it extracts a method -

For example (trivial) if I came across

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

I would probably replace this with the swap (i, j) method.

The compiler is likely to embed it, and swap () all semantically says what happens.

Given my own code (starting from scratch), I tend to reorganize the project. It's often easier for me to work in a particular class. When it is executed and debugged, I will pull out the old extraction interface trick.

I will leave it to the employee for refactoring for readability, as I am too close to the code to notice holes. In the end, I know what I meant.

+4
Sep 26 '08 at 17:21
source share

Reactor opportunistically! Do it when it's easy.

If refactoring is difficult, you do it at the wrong time (when he doesn’t need the code) or on the wrong part of the code (where there is more efficient efficiency that you need to get anywhere). (Or you are still not very good at refactoring.)

Saving refactoring for "maintenance" is a tautology. Refactoring is a service.

+4
Sep 26 '08 at 17:53
source share

I will reorganize every time I read , and can make it more readable . Not a big restructuring. But if I think: "What is this List containing? Oh, Integer s!" then I will change it to List<Integer> . In addition, I often retrieve methods in the IDE to indicate a good name for several lines of code.

+4
Sep 29 '08 at 20:12
source share

Every time you are faced with a need. At least when you are going to change a piece of code that needs refactoring.

+2
Sep 26 '08 at 17:01
source share

The answer is always, but more specifically:

  • Assuming you answer for each task, then for each new branch, before it moves to QA.
  • If you design everything in the trunk, then before each fix.
  • When saving the old code, use the above for new tasks, and for the old code - refactoring on major releases that will receive additional QA.
+2
Sep 26 '08 at 17:01
source share

I localize refactoring for code related to my current task. I am trying to do my refactoring ahead. I carry out these changes separately, since from a functional point of view it is not connected with the real task. This way, the code works cleaner and the change history is also cleaner.

+2
Sep 26 '08 at 17:36
source share

"Refactoring early on, refactoring often" is a productive guideline. Although this type assumes that you really know the code. The older the system gets, the more dangerous refactoring becomes, and the more deliberation is required. In some cases, the reorganization should be task-driven, with an assessment of the level of effort and time, etc.

+2
Sep 26 '08 at 17:50
source share

Constantly, within reason. You should always look for ways to improve your software, but you must be careful to avoid situations where you are refactoring for refactoring.

If you can make refactoring make the code faster, easier to read, easier to maintain, or simplify or provide some other value for the business I'm talking about, check it out!

+2
Sep 26 '08 at 20:54
source share

If you have a refactoring tool that will make changes safely, you should reorganize it whenever the code compiles , if it makes the code more understandable.

If you do not have such a tool, you should reorganize when the tests are green , if it makes the code more understandable.

Make small changes - rename the method to make it easier to understand. Extract the class to make the group of related variables clearly related. Refactoring is not about making big changes, but about making things clean in a minute. Refactoring cleans your dishes after every meal, rather than waiting for each surface to be covered with dirty plates.

+2
Jun 23 '11 at 17:31
source share

Absolutely, as soon as it seems appropriate. If you are not fighting.

Since the transition to Squeak (which I now mention every post), I realized that many design issues during prototyping are eliminated, because refactoring in this environment is very simple. To be honest, if you don’t have an environment where refactoring is mostly painless, I recommend you try squeaking to know how it can be.

+1
Sep 26 '08 at 17:02
source share

Refactoring often can often save a day, or at least some time. There was a project that I was working on, and we reworked all our code after we reached some important milestone. This was a great way, because if we needed to break code that was no longer useful, it made it easier to fix any new thing that we needed.

+1
Sep 26 '08 at 21:51
source share

We are discussing this work now. We more or less agree that "write so that it works and then fix it." But we are diverging in time. I more "fix it right away," my colleague more "fix it in the next iteration."

Some quotes that support it:

Douglas Crockford, Senior Architect Javascript Yahoo:

refactoring every 7 sprints.

Ken Thompson (unix man):

The code itself is almost rotting, and we're going to rewrite it. Even when nothing has changed, for some reason it rots.

I would like that after completing the task presented by the code, you can return after 2 months and think "yes, I'm fine here." I don’t find it easy to find time to come back later and fix it. believing that this is somewhat naive from my point of view.

Edit: spelling error

+1
Nov 11 '10 at 7:44
source share

I think you need to reorganize something when you are currently working on part of it. So, if you need to strengthen function A, you should reorganize it before (and after?). If you are not doing anything with this function, leave it as is if you have something else.

Do not reorganize the working part of the system if you no longer need to change it.

0
Sep 26 '08 at 17:03
source share

There are many opinions on this topic, some of which are related to a specific methodology or development approach. When using TDD, refactoring early and often is, as you say, the preferred approach.

In other situations, you can refactor as needed. For example, when you look at repeating code.

When using more traditional methods with detailed preliminary design, refactoring may be less common. However, I would recommend not leaving refactoring until the end of the project. Not only are you likely to run into problems, potentially post-UAT, it often also happens that refactoring is becoming more complex. For this reason, time constraints for a classic project cause refactoring and additional testing to be discarded, and when you support the service, you may already have created a spaghetti monster code.

0
Sep 26 '08 at 17:04
source share

If it is not broken, do not refactor it.

I would say that refactor time belongs to the initial stage of coding, and ou can do it as often and as many times as you like. Once it is in the hands of the client, then this becomes another matter. You do not want to do the work for yourself, “removing” the code only to find out that it was sent and broke something.

The time after the initial delivery to refactoring is when you say you will do it. When the code gets too smelly, then you have a special release containing refactoring and maybe some more important fixes. That way, if you break something, you know where it went wrong, you can fix it much easier. If you reorganize all the time, you break everything, you don’t know that it was broken until it receives QAd, and then it will be difficult for you to find out whether the changes to the error code / function have changed, or some of them you refactored many years ago.

Checking for changes in cbreaking is much easier when the code looks something like it was used. Refactoring is a lot of code structure, and you can make it almost impossible, so only refactoring when you seriously mean it. Treat it like any other product code, and you should be fine.

0
Sep 26 '08 at 17:08
source share
0
Sep 26 '08 at 17:48
source share



All Articles