4

Everyone has heard this term thrown around, and most people, I would imagine, have a conceptual idea of the meaning. I, myself, am in that latter group. However, I feel that the definition Google/Wikipedia has provided me isn't great.

Code refactoring is the process of restructuring existing computer code – changing the factoring – without changing its external behavior. Refactoring improves nonfunctional attributes of the software.

I guess I'd like a technical definition of what is and isn't considered "Refactoring" (not a definition for non-technical people). Amending code to improve performance? Improving extensibility? There's almost certainly a grey area here, but any further clarity would be much appreciated.

gnat
  • 20,543
  • 29
  • 115
  • 306

2 Answers2

10

A longer definition can be found in Martin Fowler's Refactoring book (page xvi).

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.

Fowler emphasizes design and I think that's what separates refactoring from other types of changes. Improving performance is a code change that does not alter behavior, but we typically call that "optimization" since it's more focused on implementation rather than design.

Improving extensibility could be considered part of refactoring, given how many refactoring techniques involve inheritance and polymorphism.

There's no clear-cut distinction between refactoring and not, and naming things is hard.

Eric
  • 551
0

You have the definition right in front of your eyes - there is no other definition. Code refactoring is the process of restructuring existing computer code – changing the factoring – without changing its external behavior. Refactoring improves nonfunctional attributes of the software That's it. I'd even suggest to replace "improves" with "changes".

Anything that changes your code without changing functionality is refactoring. Sometimes it happens that refactoring turns hidden flaws into obvious. I refactored some code once without changing functionality, and I ended up with a line "if (condition1 && condition2 && condition3) crash ();" Removing that line of code was not refactoring. It was a bug fix.

gnasher729
  • 49,096