Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is refactoring and what is only modifying code?

Tags:

refactoring

I know that refactoring is "changing the structure of a program so that the functionality is not changed". I was talking with some of the guys I'm working with on my final year project at university and I was surprised that they have a much more expansive (for want of a better word) view of refactoring.

I consider refactoring to be things like extracting methods and renaming classes. They also suggested things like changing data structures (like a Java LinkedList to an ArrayList), changing algorithms (using merge sort instead of bubble sort), and even rewriting large chunks of code as refactoring.

I was quite sure that they were wrong, but I wasn't able to give a good reason why because what they were suggesting did change the program (and presumably make it better) without changing its behaviour. Am I right, and more importantly, why?

like image 877
David Johnstone Avatar asked Jun 22 '09 07:06

David Johnstone


People also ask

What is the difference between refactoring and rewriting?

In a refactor, developers make microchanges to clean up the code. With a rewrite, they throw almost everything away, and the coding process essentially starts anew. Both of these options have advantages and disadvantages.

What is code refactoring examples?

Examples are: adding, removing, and introducing new parameters, replacing the parameter with the explicit method and method call, parameterize method, making a separate query from modifier, preserve the whole object, remove setting method, etc.

What is the process of refactoring?

Refactoring is the process of changing a software system so the software's structure and performance are improved without altering the functional behavior of the code. Refactoring is used to improve system maintainability and extend its usable life span.

What is code refactoring and why is it important in Java?

The basic purpose of code refactoring is to make the code more efficient and maintainable. This is key in reducing technical cost since it's much better to clean up the code now than pay for costly errors later. Code refactoring, which improves readability, makes the QA and debugging process go much more smoothly.


2 Answers

Martin Fowler's "Refactoring: Improving the Design of Existing Code" is perhaps THE reference:

Refactoring is a controlled technique for improving the design of an existing code base. Its essence is applying a series of small behavior-preserving transformations, each of which "too small to be worth doing". However the cumulative effect of each of these transformations is quite significant. By doing them in small steps you reduce the risk of introducing errors. You also avoid having the system broken while you are carrying out the restructuring - which allows you to gradually refactor a system over an extended period of time.

Refactoring goes hand-in-hand with unit testing. Write tests before you refactor and then you have a confidence level in the refactoring (proportional to the coverage of the tests).

A good reference is: Information about Refactoring

like image 187
Mitch Wheat Avatar answered Oct 11 '22 12:10

Mitch Wheat


Fowler draws a clean line between changes to code that do, and those that do not, affect its behavior. He calls those that do not, "refactoring". This is an important distinction, because if we divide our work into refactoring and non-refactoring code modification activities (Fowler calls it "wearing different hats"), we can apply different, goal-appropriate techniques.

If we are making a refactoring, or behavior-preserving code modification:

  • all our unit tests should pass before and after the modification
  • we should not need to modify any tests, or write any new ones
  • we expect cleaner code when we are done
  • we do not expect new behavior

If we are making a behavior-changing code modification:

  • we expect new behavior
  • we should write new tests
  • we may get dirtier code when we are done (and should then refactor it)

If we lose sight of this distinction, then our expectations for any given code modification task are muddled and complex, or at any rate more muddled and more complex than if we are mindful of it. That is why the word and its meaning are important.

like image 27
Carl Manaster Avatar answered Oct 11 '22 14:10

Carl Manaster