How to keep one "I" from the overwhelming desire to rewrite everything?

Customization

Have you ever had the experience of going into a piece of code to make a seemingly simple change, and then realizing that you have just entered a wasteland that deserves serious attention? This is usually accompanied by an official FREAK OUT , when the overwhelming feeling of rewriting everything in sight begins to creep up.

It is important to note that this bad code does not necessarily come from others, as it may be something we wrote or contributed in the past.

Problem

Obviously, there is serious code rot, terrible architecture, etc. that you have to deal with. The real problem with this question is that this is not the time to rewrite the code . There can be many reasons for this:

  • It is currently in the middle of a release cycle, so any changes should be minimal.
  • It's 2:00 in the morning, and the brain begins to close.
  • This can have adverse effects on the schedule.
  • The rabbit hole can go much deeper than our eyes can see at this time.
  • etc...

Question

So, how should we balance the obligation to constantly improve the code, as well as be a responsible developer? How do we refrain from participating in the broken theory of windows , and also recognize the actions and potential recklessness that they may cause?




Update

Great answers! For the most part, there seem to be two schools of thought:

  • Do not resist desire, as it is good.
  • Resist the temptation, as it burned you to the ground.

It would be interesting to know if more people have any balance.

+41
language-agnostic
Nov 27 '08 at 17:47
source share
20 answers

I am a big fan of creating lists!

As soon as the desire forces you to rewrite something - spend 10 minutes making a list of things to be rewritten. Follow all the lanes that promote you to the code that needs attention and list them also.

Hopefully in a relatively short period of time you will have one of two things:

  • A very long list that completely eliminates the need to rewrite something again.
  • A list that is actually not that long, so why not treat yourself and rewrite ?!
+23
Nov 27 '08 at 18:04
source share

I found that spending two months correcting errors caused by my “harmless” rewriting was enough to cure me of the desire to do such things without a clear project mandate / plan.

+16
Nov 27 '08 at 18:01
source share

The most unforgettable project of this kind for me occurred about 4 years ago, when I was called to a remote office to "help" with a project that was supposed to be presented within 1 week for a major presentation to the client and had not yet worked at all. The project was largely disconnected from India, and IMO - the failure of project management led to a ton of spaghetti code being too fragmented to work properly in its current form.

After a full review of the day, I expressed my opinion to the management that the project just needs wholesale refactoring and reorganization, or it will never work properly. The result of this discussion was 6 days, 20 hours of work / 4 hours of sleep, 2 of which I actually spent sleeping on the couch in the hotel lobby due to the lost time when I was returning to the hotel.

Major code improvements:

  • Application of naming standards
  • Moved to source control
  • Assembly process development
  • Documentation of individual components

Most of the source code was left in place, but simply moved and reorganized / reorganized to make it sustainable in the long run. Was there a hellish week? Sure. Did this make the project more successful? Yes.

I cannot live with the spaghetti code, and I will often sacrifice my personal time to solve it.

+13
Nov 27 '08 at 18:27
source share

How to restrain yourself from the overwhelming desire to rewrite everything?

Get old *

As this happened to me, I gradually lost the desire to rewrite everything. Why?

  • Once you have done this several times, you will realize that you often find yourself worse than you started.
  • Even if you are a divine gift for programming, and your brilliant rewrite does not introduce any new errors, you simply will not notice or implement about 30% of the small functions / errors that rely on. It will cost you months to fix.
  • Nothing destroys your irrational abundance like time. This is often a sad loss, but in this case it is a victory.

* (more experience may also be a suitable replacement if you do not have enough free time to become old)

+9
Nov 27 '08 at 20:30
source share

The impulse for rewriting is righteous if:

  • “freeze” existing code (with label)
  • you start retrying in a separate branch
  • first prepare the first block test to find out the current behavior and make sure that you are reproducing existing functions ...

However, you must balance the rewriting process with the stability of measuring outdated code.
"If it is not broken, do not correct it";)

+8
Nov 27 '08 at 17:59
source share

This is not quite the answer, but by reading a great article narcissism of small code differences can help .

+7
Nov 27 '08 at 19:24
source share

You are absolutely right that there is the right time and the wrong time to rewrite (and destabilize) the code.

Reminds me of an engineer whom I knew, who had the habit of diving and doing serious transcribing whenever he pleased. This led to the fact that the QC staff climbed the wall behind their product - if he reports an error about something trivial, the error will be fixed, but the major engineer rewrites the things that he noticed, fixing one error, to introduce fifty more The errors that QA staff had to track.

Thus, one way to heal yourself from the temptation to do rewriting is to walk a mile in the shoes of a QA engineer who must check, diagnose, and report all of these new errors. Maybe even lend yourself to the quality department for several months to get a personal idea of ​​the type of work they do (if you have never done so).

Another suggestion is to make a rule for yourself to write notes to describe any changes. Perhaps post these notes when you check for code changes to source control. You can be motivated to make the changes as small as possible in this way.

+6
Nov 27 '08 at 19:17
source share

Just don't - the rabbit’s hole always goes too deep. Make small local changes that leave the place cleaner than you found, but leave a lot of refactoring when you are in a state of anxiety and you have enough time to record.

A good starter for solving large refactoring at small stages sourcemaking.com :

you have to do like Hansel and Gretel and bite around the edges, a little today, a little more tomorrow

+5
Nov 27 '08 at 18:01
source share
  • re-read "Refactoring".
  • Take a piece of paper and indicate "Bad smells." (for every smell in BadSmells () {
    print smell.name,
    }
  • Add code comments, including list items.
    while (odorPersists ()) {
  • Work on the list, with laser focus on one smell at a time.
    }
+4
Nov 27 '08 at 18:08
source share

Personally, this means that bug tracking software like JIRA or Bugzilla comes with me.

I have a difficult time NOT to fix all the broken windows when I see them, but if the situation is bad enough (and time allows), I will open a ticket and give it to me or assign it to the responsible group, so I will not leave on the tangent.

“I only do what needs to be done with them, but the problem is documented and will be fixed on time.”

- This does not mean that you need to handle small broken windows; small issues should always be recorded on the contact IMHO.

+4
Nov 27 '08 at 20:21
source share

As far as I know, if you have nothing better than rewriting the code, then you should make everyone else aware and just do it . Obviously, if it takes several days to get any result, and changes mean excessive downtime, then this is not a good idea, but in the end this code will become a problem. Imagine that others know that the code is terrible and is trying to rewrite it in order to rewrite it.

The problem with the above statement is that as a programmer / developer you will ALWAYS have other things to stay busy. Just leave it on the low priority priority list, so when you are struggling with some kind of work, you can always maintain your rhythm when dubbing.

+3
Nov 27 '08 at 18:04
source share

I have never worked in any particularly fast, so I do this:

1) Find out if there is a reasonable correction that is not related to serious rewriting. If not, then clear some time (perhaps by explaining to others how difficult it is to fix) and rewrite.

2) There is a reasonable fix without serious rewriting. Apply the fix, run the tests, check it, mark the error as fixed.

3) Now raise a new error / problem (extension request), outlining the proposed rewrite and how it will improve the code (simpler? More maintainable? Reduces communication? Affects performance or resource usage?). Assign this to yourself, CC, who cares about this bit of code.

4) Give people the opportunity to comment, and then prioritize this new mistake in my existing tasks. This usually means don't do it now, because most of the time, if I have one “correct” error to fix, then I have at least two. Do this as soon as the critical list is cleared, or the next time I need to do something that is not boring. Or maybe in a couple of days it doesn't seem to be worth doing anymore, and it won’t be done, and I saved the time that I would have spent on it.

It is important, I think, to avoid shaving the yak every time you make a small correction.

The trade-off is that if the code you want to rewrite is really bad, then rewriting is easy to redefine, and you end up spending so much time saving it that you don't have time to replace it with something that requires less service. This must be borne in mind. But no matter what priority should be rewritten, or how your organization assigns these priorities, fixing a bad design in the same area of ​​code is not the same as fixing a one-after-one error that caused a crash, with which you originally encountered dealing with. This should be taken into account in step 1: if the project means that there are a whole bunch of other errors in the code that may be related to each other, then just fixing it is probably not "reasonable". If it really was a typo, but you accidentally discovered the possibility of refactoring, because you opened the file, and fixing it and touching anything was not "reasonable".

Obviously, the more agile your store, the more significant a refactor you can do without being so destructive / time-consuming / political that it required a separate task.

+3
Nov 27 '08 at 19:23
source share

Joel has an article about this:

There is a subtle reason why programmers always want to throw away code and start over. The reason is because they think the old code is a mess. And here's an interesting point: they are probably wrong.

+2
Nov 27 '08 at 19:30
source share

If this is some kind of code that you have inherited, start making the code yourself. Write block tests, then refactoring.

+2
Nov 27 '08 at 20:37
source share

Bad code is not something that can be completely avoided, but it can be isolated by proper abstraction. If this is really a wasteland, which means there are no polygons, focusing the design process is much more effective than trying to get people to write perfect code.

+1
Nov 27 '08 at 17:55
source share

Do not think that you SHOULD stop yourself from doing this. Basically, if you FEEL for a big dub, this is basically correct. I really disagree with Joel Spolsky on this issue ...

Although this is one of the few places where I do not agree with him ...;)

+1
Nov 27 '08 at 18:29
source share

Stop rewriting when it's good enough. To do this, you need to know what a good program is for you, not only for your employer. In the end, you program for a living not for a good experience. Develop sound criteria that really tell you that you are good and the result is a descent, and it's time to move on to the next task. You need your programs no less than your boss loves them.

+1
Nov 27 '08 at 20:06
source share

Write more unit tests to see if the code is working fine.

If you still have a desire to rewrite it, you will have several tests to find out that your rewrite code is now failing;)

+1
Nov 27 '08 at 23:09
source share

Refactoring is only if your boss / company really encourages it, otherwise you will end up taking frequent extra time to bring the code to perfection ... Until someone less dedicated touches it again.

0
May 28 '10 at 18:46
source share

Initial Assumption: You are already "doing early, doing often."

Then there will never be time to start refactoring, because you can easily back off at any time. I find that at first it always seems that it will be easy, but after making a few changes and observing the effects, I begin to understand how big the work will be. The time has come to ask if the time is really right for making changes, or to live with it until the next time you come to this code is the best course.

Willingness to stop, throw away half of the refactoring and make it an unpleasant, but quick way, if necessary, is the key.

What for refactoring, where the changes are incremental, and running (or almost running) software allows you to substantiate well. Rewriting is different because the time until you find out that it takes longer than you thought was much longer. I may take the question too literally, but there is almost never time to throw it away and start again.

0
May 28 '10 at 19:02
source share



All Articles