What is refactoring and what only changes the code?

I know that refactoring is "changing the structure of a program so that functionality does not change." I talked to some of the guys I work with in my last project at the university, and I was surprised that they have a much broader (because of a better word) look at refactoring.

I see refactoring as things like extraction methods and renaming classes. They also suggested things like changing data structures (like Java LinkedList to ArrayList ), changing algorithms (using merge sort instead of bubble sorting), and even rewriting large code snippets as refactoring.

I was absolutely sure that they were wrong, but I could not give a good reason, because what they proposed changed the program (and, presumably, made it better) without changing its behavior. I'm right, and more importantly, why?

+67
refactoring
Jun 22 '09 at 7:15
source share
11 answers

Martin Fowler "Refactoring: Improving the Design of Existing Code" is possibly a link:

Refactoring is a controlled method for improving the design of an existing code base. Its essence lies in the application of a series of small transformations that preserve behavior, each of which is "too small to be worth doing." However, the combined effect of each of these transformations is quite significant. By taking them in small steps, you reduce the risk of mistakes. You also avoid system breakdowns during restructuring, which allows you to gradually refactor the system over a long period of time.

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

Good link: refactoring information

+64
Jun 22 '09 at 7:23
source share
— -

Fowler draws a clean line between code changes that make, and those that don't, affect his behavior. He calls those who do not, "refactoring." This is an important difference, because if we divide our work into refactoring and not refactoring code modification activities (Fowler calls this “wearing different hats”), we can use different target methods.

If we do refactoring or modifying behavior-preserving code:

  • all our unit tests must pass before and after modification
  • we do not need to change any tests or write new ones.
  • we expect cleaner code when we are done
  • we do not expect new behavior

If we make changes that change the behavior:

  • we expect new behavior
  • we have to write new tests
  • we can get a messier code when we finish (and then reorganize it)

If we lose sight of this difference, then our expectations for any given task of modifying the code are confusing and complex, or at least more confusing and more complicated than if we remember this. That is why the word and its meaning are important.

+27
Jun 22 '09 at 16:32
source share

To show your opinion:

Minor incremental changes that leave the code in a better state than was found

Definitely Yes: “Cosmetological” changes that are not directly related to the functions (i.e. they are not paid as a change request).

Definitely No: Rewriting large chunks clearly violates the "small, incremental" part. Refactoring is often used as the opposite of rewriting: instead of doing it again, improve the existing one.

Certainly Maybe: Replacing data structures and algorithms is a bit of a borderline case. The decisive difference here is IMO - these are small steps: to be ready for delivery, to be ready to work on another case.




Example: Imagine that you have a report randomizer module that slows down its use of the vector. You have profiled that inserting vectors is a bottleneck, but unfortunately the module uses many-sided memory in many places, so when using the list everything will break quietly.

Rewriting would mean throwing a module out of a building better and faster from scratch, simply by selecting some parts from the old one. Or write a new kernel, and then paste it into an existing dialog.

Refactoring means taking small steps to remove pointer arithmetic, so that switch. Perhaps you even create a utility function that wraps pointer arithmetic, replaces the direct pointer manipulation with calls to that function, and then switches to an iterator so that the compiler complains about places where pointer arithmetic is still used, then switch to list and then delete the function ultility.




The idea is that the code gets worse on its own. When fixing errors and adding functions, the quality breaks up in small steps - the value of the variable changes subtly, the functions receive an additional parameter that breaks the isolation, the cycle becomes a bit complicated, etc. None of them is a real mistake, you can’t tell the number of lines, which makes the cycle difficult, but you will damage readability and maintenance.

Similarly, changing a variable name or extracting a function are not significant improvements. But all together they fight slow erosion.

Like a pebble wall where every day falls to the ground. And every day, one passerby takes it and returns it.

+17
Jun 22 '09 at 17:11
source share

Based on Martin Fowler’s definition,

Refactoring is a disciplined technique for restructuring existing code, changing its internal structure without changing external behavior.

... I think you are clearly right.

They also suggested things like changing data structures (like Java LinkedList in ArrayList), changing algorithms (using merge bubble sorting instead) and even rewriting large chunks of code as refactoring.

Changing the algorithm to something much faster is obviously not refactoring, because the external behavior is changing! (Again, if the effect is never noticeable, perhaps you could call it refactoring after all - and also premature optimization. :-)

This is my pet; it is annoying when people use the term sloppily - I even came across those who might accidentally use refactoring for almost any changes or corrections. Yes, it is a hip and cool buzzword and all, but there is nothing wrong with the simple old terms, such as changing, rewriting, or improving performance. We should use them when necessary and reserve refactoring for cases when you really just improve the internal structure of your software. As part of the development team, it is especially important to have a common language to accurately discuss your work.

+12
Jun 22 '09 at 8:13
source share

I think you're right, but arguing about the meaning of a word is not particularly interesting or productive.

+8
Jun 22 '09 at 7:19
source share

If the interface to part of the code is changing, then I think that more than refactoring.

A typical example for refactoring is

  • "Oh, all my unit tests run, but I think my code can be made cleaner."
  • Change code to be more readable / clearer / effective
  • Re-run unit tests (without changing the tests) and verify that they still work.

This means that the term "refactoring" refers to the interface that you are discussing. then you could reorganize the code behind one interface, and change the code of the other more widely at a lower level (maybe this difference is what creates confusion between you and your colleagues here?)

+8
Jun 22 '09 at 7:23
source share

http://en.wikipedia.org/wiki/Code_refactoring

Code refactoring is the process of changing a computer's internal structure of a program without changing its external functional behavior or existing functionality, in order to improve internal non-functional properties of software, for example, to improve code readability, simplify code structure, to change code to adhere to this programming paradigm, to improve maintainability, improve performance or improve extensibility.

I agree that refactoring code involves breaking existing code. Just make sure you have unit tests so that you don't enter any errors and the rest of the code compiles. Using refactoring tools like Resharper for C # makes it so easy!

  • Making code more understandable
  • Code cleaning and simplification
  • Code Removal! Excessive, unused code and comments should be removed.
  • Productivity increase
  • Creating something more general. Start with the simplest thing and then reorganize it so that it is easier to test / isolate or generate, so that it can work differently using polymorphism.
  • Storing DRY code - Do not repeat yourself, so a refactoring session can include repeated code and refactoring it into one component / class / module.
+4
Jun 22 '09 at 7:26
source share

I disagree :

In software development, “refactoring” the source code means improving it without changing its overall results [...]

You already know the more precise terms used for subsets of refactoring, and yes, this is a very general term.

+2
Jun 22 '09 at 7:25
source share

I think that no one can use too strong a definition of the term "refactoring". The border between how you perceive it and your colleagues is blurred and may be closer to their or your opinion depending on many facts. Since it is dynamic, try defining it. First of all, identify the boundaries of the system or subsystem that you are trying to reorganize.

If it is a method, save the name, enter the arguments, return type, and possibly discard the statements. Make all the changes inside the method without changing the way it looks outside.

If you refactor a class, set its public API and use renaming variables, extraction methods and all other available methods modify the class to be more readable and / or more efficient.

If the part of the code that you are refactoring is a package or module, then inside it, refactoring can rename classes, delete, introduce interfaces, push / pull code into super / subclasses.

+1
Jun 22 '09 at 7:25
source share

If you want to read some tips on why refactoring is important and why you should always be unhappy with your old work, I can recommend a new blog post. He will answer some problems regarding refactoring, how to convince his boss of his importance, etc.

http://www.schibsted.pl/blog/back-end/refactoring /

0
Jun 20 '17 at 8:06
source share

Refactoring = improving non-functional requirements while maintaining the same functional.

Non-functional requirements = modularity, testability, maintainability, readability, separation of interests, Liskov principles and so on ...

0
May 01, '19 at 22:25
source share



All Articles