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
  • 21,442
  • 29
  • 112
  • 288
George Grainger
  • 485
  • 4
  • 12
  • 4
    Possible duplicate of [How do you explain refactoring to a non-technical person?](http://programmers.stackexchange.com/questions/18059/how-do-you-explain-refactoring-to-a-non-technical-person) – gnat Apr 05 '16 at 16:04
  • Ahh I didn't see this! Although the above doesn't go as far as I'd like with a definition... – George Grainger Apr 05 '16 at 16:06
  • 4
    Unfortunately "refactoring" is not really a precise technical term with an unambiguous meaning like you seem to want. – Ixrec Apr 05 '16 at 16:08
  • That would be the ideal, yes. In lieu of that, if anyone could elucidate me as to what is/isn't considered refactoring (by their own loose definition), that would also be helpful. – George Grainger Apr 05 '16 at 16:10
  • 11
    The definition you posted seems spot-on to me... – Gort the Robot Apr 05 '16 at 16:44
  • At the companies I've worked in, "refactoring" has been a broad umbrella term which covers anything where existing code is changed without affecting behaviour or functionality. That pretty much wrappers up anything which is not bug fixing or implementing a change in requirements. If you want to be precise, it's probably better to use the term "refactoring" alongside specific context - e.g. *"refactoring to prune dead code"* or *"refactoring to improve readability"*, etc. – Ben Cottrell Apr 05 '16 at 16:48
  • 2
    **Refactoring** is an umbrella term that encompasses several specific code improvement techniques. For more information on those techniques and the reasons why they are used, see https://sourcemaking.com/refactoring – Robert Harvey Apr 05 '16 at 16:48
  • @gnat: see my edit, this is not a dupe – Doc Brown Apr 05 '16 at 17:01
  • Performance improvement isn't (necessarily) changing function. Of course, performance improvements can sometimes be had via cleaner design (remove unnecessary cruft), other times via messier design (multi-threading, clustering or other scale out, denormalization, caching). Still, I'd allow all of that under refactoring. – Erik Eidt Apr 05 '16 at 18:20
  • On the other hand, (improving) extensibility seems like feature/function changes. – Erik Eidt Apr 05 '16 at 18:21
  • @ErikEidt: you can use the term for whatever you want to, but if I remember correctly, in Fowler's book there is no refactoring mentioned with the goal of performance optimization. – Doc Brown Apr 05 '16 at 18:26
  • Modern language execution engines are optimized for executing well-factored idiomatic code. As a result, refactoring may improve performance, but improving performance is not refactoring. Refactoring may, however, make it easier to make performance-improving changes. – Jörg W Mittag Apr 05 '16 at 20:37
  • Define "technical definition"? The one you have given seems technical enough to me. You want something more complex? That uses more technical terms? If so, why? I'm not clear what you're looking for exactly, or what is wrong with the existing definition although you say its "not great". You want one with examples? If so, definitions don't (usually) serve the purpose of providing examples, except-rarely-to clarify their general nature. – Bradley Thomas Dec 05 '16 at 19:48
  • There seems to be a prejudice in many comments in favor of "denormalization." Is it not more than remotely possible, or even probable that a solution may be to normalize to the level that should have been in the beginning. feel free to pile on. 'thinking like a data modeler.' – Jim Dec 05 '16 at 17:40
  • 1
    Related: [Why do we Refactor?](http://stackoverflow.com/q/828764/45249) – mouviciel Dec 06 '16 at 09:48

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
  • 561
  • 3
  • 8
  • 2
    Is performance a behavior? My "gut feel" is yes - at least I'd guess users might think so. – jleach Apr 05 '16 at 18:13
  • To my understanding, performance counts definitely as behaviour when it can be noticed or measured by a user. And you typically do not try any performance optimization if you do not want to change the performance in a notable or measurable way. – Doc Brown Apr 05 '16 at 18:32
  • 1
    I have always taken exception to "yet improves" in that definition. Instead I'd like to see "but changes". The reason is that many refactors are reversible and people can legitimately debate which version is better. However the person doing the refactor is less likely to understand that if they have internalized the idea that refactors are always improvements. So, for instance, changing variable names is a refactor regardless of whether the new name is better or worse than the old. – btilly Apr 05 '16 at 23:03
  • Thank you for this, I realise the question was pertaining to a somewhat grey area. But I'm glad it sparked discussion. I'll be reading that book too. – George Grainger Apr 06 '16 at 07:48
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
  • 42,090
  • 4
  • 59
  • 119