File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes OO, Patterns, UML and Refactoring and the fly likes Refactoring benefits Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Refactoring benefits" Watch "Refactoring benefits" New topic
Author

Refactoring benefits

kundan varma
Ranch Hand

Joined: Mar 08, 2004
Posts: 322
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


SCJP1.4,SCBCD,SCEA,CNA
Failures are practice shoots for success.
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
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.


SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
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


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
kundan varma
Ranch Hand

Joined: Mar 08, 2004
Posts: 322
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

Joined: Sep 08, 2004
Posts: 14
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.


Author of <a href="http://www.amazon.com/exec/obidos/ASIN/0321213351/ref=jranch-20" target="_blank" rel="nofollow">Refactoring to Patterns</a>
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
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


blog - InfoQ.com
Alvin chew
Ranch Hand

Joined: Jan 08, 2004
Posts: 834
hi, will refactoring benefit us in term of performance ? or it only benefit us in readability ?
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
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

Joined: Jan 23, 2002
Posts: 11962
    
    5
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

Joined: Mar 08, 2004
Posts: 322
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

Joined: Sep 09, 2004
Posts: 103
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Refactoring benefits