Powered By Blogger

Wednesday, October 21, 2009

Re: To refactor or not to refactor

From Programmer to Programmer

Re: To refactor or not to refactor by KAMIL MANOJKUMAR DHULESHIA and To refactor or not to refactor by YU HONG TAN

Refactoring is changing an application’s or a bunch of code’s internal implementation while keeping its external functionalities.

Refactoring is to improve code’s quality, readability, understandability etc.

Refactoring is an important part in TDD when all the tests are written first, and then code is altered/refactored to pass all the tests.

So to refactor or not to refactor? My answer is YES

Why programmers refactor their code? Well, I believe no programmers like their code to be smelly.

Kamil said refactoring is not an ideal thing to do 5 minutes before deadline. I agree. Refactoring is time consuming, and it is too risky to alter to code at the last moment. It is better to leave it for the next release.

But Kamil also said when to refactor is when you need to fix a bug. It’s not right my friend, a bug is what causes your code to work incorrectly. Fixing a bug is not refactoring. The only exception is when you are doing TDD, while you write your unit tests first.

Another thing I don't agree with Kamil is when to refactor is when adding function. Kamil himself just said in his blog “Refactoring does not add any functionality to the code but it does make the code more efficient”. Did he confused himself? Modifying the code to add a function is not called refactor. Functionalities are to be kept unchanged while refactoring.

Refactoring is simply a process to eliminate repetitive code in a software system without changing the behavior of the actual system – Yu Hong said. It’s not completely wrong, but refactor is not that simple. How's about making your code more readable and understandale?

I strongly disagree with Yu Hong saying “There are times that the code for that system that is being refactored is too complex; it is not worth the time and money to process any further refactoring the code”. More complex the code is, more likely it needs refactor. After all, a complex code is made up from many simple one. Refactor helps simplifying the code, give it a better shape, which is ready for being reused and maintained.

There’s one important thing that both Kamil and Yu Hong missed, automated tests are essential and perquisites while refactoring. Tests guarantee refactoring won’t break the code, or make sure the code’s functionalities unchanged.

Bibliography

1. What is Refactoring http://c2.com/cgi/wiki?WhatIsRefactoring
2. Software Refactoring http://www.refactoring.be/talks/tom-mens/refactoring.pdf

No comments:

Post a Comment