Monday, February 14, 2005

Why I love code refactoring..

I love code refactoring! Code refactoring is a process by which computer source code is re-written to improve (among other things) efficiency, readability, and maintainability. In other words, to make it better. Successfully refactoring code is like the calvary riding in to save the day.

The main reason I enjoy refactoring is the chance to improve efficiency. I get a vicarious thrill from seeing a poorly written algorithm run orders of magnitude faster by changing one or two lines of code. It often seems that efficiency gains are achieved with minimum code changes. That's another reason I enjoy it so much. It's the most bang for the buck.

This leads to a question. How much electricity could we save if all computer programs were written as efficiently as was possible? Could we reduce the emissions of coal burning power plants to the point where we could eat the fish caught in our lakes and rivers?

4 comments:

  1. I think you're forgetting James's First Law of Programming, all software eventually grows to exceed the full capacity of the hardware it runs on. Refactor all you want, those CPU cycles (and electricity) you just saved will be consumed by shiny new animated buttons in the next release.

    ReplyDelete
  2. Yeah, but if all the programs ran well, all us programmers would be out of work, which would put a drain on the economy, cause panic, start a war that would eventually go nuclear and destroy most life on this planet...

    That would be bad.

    ReplyDelete
  3. It sounds like you're advocating poorly written software for the sake of world peace. Or is it just for the sake of job security? (And I don't know who this James guy is but I thought it was a Microsoft law to suck up all spare CPU cycles with animated buttons.)

    ReplyDelete
  4. I wonder if improving the effeciency of an algorithm can be categroized as refactoring. Often times, in order to make a piece of code more effecient, you have to sacrifice readability, and as a result maintainability. The key principal of refactoring is that you change the software system in such a way that it does not affect the external behavior of the code yet improves the internal structure. Making code more effecient almost always adds to the complexity of the internal structure and modifies the external behavior (it's now running faster).

    ReplyDelete