exebook
exebook

Reputation: 33900

What is refactoring?

I hear the word refactoring everywhere. Any programming tool has some blah-blah about how it helps refactoring, every programmer or a manager will tell me something about refactoring. But to me it still sounds like a magic word without any meaning. It seems that refactoring is just editing your code or what?

Wikipedia quote

Code refactoring is a "disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior",[1] undertaken in order to improve some of the nonfunctional attributes of the software. Advantages include improved code readability and reduced complexity to improve the maintainability of the source code, as well as a more expressive internal architecture or object model to improve extensibility.

WHAT? Does every(any)body understand this? Are all those people who talk to me about refactoring, really do mean this?

And why is the name? What's "factoring" then?

Upvotes: 7

Views: 3965

Answers (5)

Shraddha
Shraddha

Reputation: 884

Refactoring has been very clearing explained in the following link: Refactoring: Guru. But, for a quick overview, have created a diagram that would help in basic understanding -

enter image description here

Upvotes: 0

Md. Masudur Rahman
Md. Masudur Rahman

Reputation: 1078

“Refactoring is the process of changing a software system in such a way that it does not alter the external behavior of the code, yet improves its internal structure. It is a disciplined way to clean up code that minimizes the chances of introducing bugs. In essence when you refactor you are improving the design of the code after it has been written.” - Martin Fowler (Father of Code Smell).

An example of a refactoring could be extract method (Figure 1): If a method is too long, it should be decomposed, using this refactoring technique. Find a clump of code (within the long method) that goes well together, create a new method with a descriptive name and move the code into the new method. If local variables are being used, these need to be passed as parameters. The last step is to add a invoke to the new method and test the code. Example of Code Refactoring

Although refactoring does not add features or functionalities in a software system, it is sharp weapon for developers in their maintenance activities. It makes a software system easier to understand and cheaper to modify without changing its observable behavior by changing its internal structure.

The purposes of refactoring according to M. Fowler are stated in the following:

  1. Refactoring Improves the Design of Software
  2. Refactoring Makes Software Easier to Understand
  3. Refactoring Helps Finding Bugs
  4. Refactoring Helps Programming Faster

Upvotes: 0

Alex
Alex

Reputation: 1007

Refactoring is all about making your code more maintainable.

Practically, the requirements of the software are changing continuously that leads to continuous changes in the software. Over a period of time, the software starts becoming complex. As correctly illustrated by Lehman in his excellent work on software evolution that "as a system evolves, its complexity increases unless work is done to maintain or reduce it". Hence, we have to reduce the complexity of the software periodically and that's why refactoring is required.

Let us consider an example: Insufficient Modularization (or God class) design smell occurs when a class is too huge and/or complex. If you have such a class in your design then you will face multiple problems (such as understandability is poor - you will find difficult to understand your code, reliability issues - since the class is complex you may change the code incorrectly or change in one aspect leads to bug in another aspect). Therefore, it is better to refactor the class using techniques such as "Extract-class" refactoring.

(More information about "Insufficient Modularization" can be found in the book "Refactoring for software design smells")

Upvotes: 1

Rock Lee
Rock Lee

Reputation: 9566

From Wiktionary: http://en.wiktionary.org/wiki/refactor

(computing) to rewrite existing source code in order to improve its readability, reusability or structure without affecting its meaning or behaviour

"Refactor" is also the name of a menu of tools in Eclipse.

I will explain the "Rename" Eclipse tool as an example of the tools under the Eclipse "Refactor" menu.

In Eclipse, you can "refactor -> rename" your variables by highlighting the variable, right-clicking and going to "refactor" and "rename":

Eclipse: refactor -> rename

Instead of renaming each one by hand, it detects which variables refer to the same object, so you can change all of the applicable variables' names at once:

enter image description here

It's really convenient to prevent bugs that would happen when you forget to rename a variable.

Upvotes: 0

Kevin Bowersox
Kevin Bowersox

Reputation: 94429

Refactoring is modifying existing code to improve its readability, re-usability, performance, extensibility and maintainability. Have you ever looked at code and thought, "Wow this is a mess" or "this could be done better"? When you start to clean up the code and improve different aspects of it, this is considered refactoring. Many times code will often repeat itself, requiring you to create abstractions to adhere to the DRY principle, another demonstration of refactoring. During most refactoring it is important to not break anything, which can be assured by using good unit tests.

Sometimes its best just to get some working code established that solves a particular problem. Think of this as a rough draft, it just gets the basic ideas established and allows you to think about the problem at hand. After the rough draft is finished, you return to the code and edit it, making improvements that leads to a final copy (refactoring). You may eventually receive further requirements that require further code modifications. At this point the cycle repeats. Get the initial ideas down in code, then revisit the code and clean it up (refactor it).

One of the main premises behind refactoring is that code can always be improved. When you make these improvements its refactoring.

Upvotes: 11

Related Questions