Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Refactoring benefits

 
kundan varma
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI All
Since JOshua is here,i wanna know basic things of refactoring.How refactoring helps in real life scenarios.Where can we use it to get the maximum benefit???
THanks
kundan
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, you should be think in why i use refactor ?

Why i use refactor ???


why ? why ? why ?


My answer :

1. Easy to add new code.
2. Improve design of exist code.
3. For better understand your code.
4. Code clearify.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by kundan varma:
HI All
Since JOshua is here,i wanna know basic things of refactoring.How refactoring helps in real life scenarios.Where can we use it to get the maximum benefit???
THanks
kundan


One of the main problems is that code degenerates while working on it. During development you find that the initial design wasn't optimal for things that you need to do, or even that you need to add things that the original design wasn't made for.

Common practice is "don't touch running code", so to not break existing things. Therefore you somehow have to workaround design short comings, adding crutches to the code.

As those crutches build up, the code degenerates more and more, adding or changing things becoming harder and harder. The system becomes rigid to the point that you can't sensibly change it any longer.

Fortunately, you can work against this. By making refactoring an intrinsic part of your daily work, the code doesn't need to degenerate - actually the code can benefit from new insights you get, and can become more and more generic, more and more easy to change and enhance.

That's what I think is the fundamental benefit of Refactoring: protecting our investment into the code from becoming worthless because of code cruft. And the best way to do this is doing it immediately when you see a way to improve your code.
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In such sense, we should try to refactor the system as early as possible, so that it will not affect much of the system?

Nick
 
kundan varma
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi llja Preuss
So we apply refactoring to the existing code.Do we change the full code itself or some inheritence to modify it.
Thanks
kundan
 
Joshua Kerievsky
author
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ilja's responses to this thread are very good. I'll add that refactoring is something we do all of the time -- there is no phase for it. You could improve the name of a variable and you are refactoring. You could collapse two hierarchies into a single hierarchy and you are refactoring. When we refactor we strive to make our systems simple, easy to understand and free of duplicated code.

In practice, the best programmers are the ones who refactor at micro and macro levels. They are always making small improvements and they are also looking for big improvements, which they can safely make by implement small, safe refactorings.

Refactoring to Patterns shows how to apply compositions of refactorings to make design improvements. Most of the examples discuss my reasons for wanting to improve a real-world design. The introductory chapters from Refactoring to Patterns discuss some useful reasons for refactoring, as do some of the introductory chapters in Fowler's Refactoring.
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by kundan varma:
So we apply refactoring to the existing code.Do we change the full code itself or some inheritence to modify it.
We change the code itself. Sometimes the refactoring may involve introducing a new abstraction, a change to an inheritance hierarchy, etc. but that's just a minor detail. The important thing is that we make the codebase better in terms of structure, readability, and so forth.
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think one of the best moments to refactor in at the end of a full iteration. At that moment a complete set of features was added, the set of tests is available and you are waiting for the tech guys to come up with the future requirements.

./pope
 
Alvin chew
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, will refactoring benefit us in term of performance ? or it only benefit us in readability ?
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Alvin chew:
hi, will refactoring benefit us in term of performance ? or it only benefit us in readability ?


Both.....
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Alvin chew:
hi, will refactoring benefit us in term of performance ? or it only benefit us in readability ?
Improving the performance of the system is rarely a reason for refactoring. It does happen, but only rarely in my experience. Most of the time, refactorings are applied to achieve a structure that's more readable and/or contains less duplication. Sometimes improved performance is a welcome side effect.
 
kundan varma
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joshua Kerievsky
Do you have any indian edition of your book. I want to study your book but most of the time i see that Addision-wesley edition dont allow indian editions.AS i tried for Effective Java of joshua Bloch, it was also having the same problem.And these book become really costly if we buy from amazon because of currency difference.
Thanks
kundan
 
Daniel Mayer
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ali Pope:
I think one of the best moments to refactor in at the end of a full iteration.


Well, for me that would be way too much time to wait. I prefer to refactor at least every couple of minutes, literally. That way, refactorings are typically quite small, and I can benefit from them now, not only in the next iteration.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic