Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is refactoring?

Tags:

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?

like image 297
exebook Avatar asked Dec 17 '13 01:12

exebook


People also ask

What is the purpose of refactoring?

The refactoring process makes future enhancements to such code a more pleasant experience. Refactoring is also known as reengineering. Refactoring can be a tough sell to IT department executives. First, there is the manager consensus concerning existing code modification: if the code is not broken, then a fix is not necessary.

What is refactoring in Java?

Refactoring consists of improving the internal structure of an existing program's source code, while preserving its external behavior.

What is code refactoring in software engineering?

Code refactoring in software engineering is the process of restructuring software source code with the purpose to improve its internal structure and non-functional features. At the same time, its external behavior remains unchanged. The main purpose of code refactoring is pretty clear — to make the code clean, neat, more efficient and maintainable.

What are the key elements of refactoring?

Performing refactoring step-by-step and running tests after each change are key elements of refactoring that make it predictable and safe. Code Smells. Code smells are indicators of problems that can be addressed during refactoring. Code smells are easy to spot and fix, but they may be just symptoms of a deeper problem with code.


2 Answers

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.

like image 169
Kevin Bowersox Avatar answered Nov 11 '22 03:11

Kevin Bowersox


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")

like image 37
Alex Avatar answered Nov 11 '22 03:11

Alex