This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes OO, Patterns, UML and Refactoring and the fly likes Refactoring ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Refactoring ??" Watch "Refactoring ??" New topic
Author

Refactoring ??

Fei Ng
Ranch Hand

Joined: Aug 26, 2000
Posts: 1242
I see alot of posts on Patterns and UML on this fourm but with Refactoring it doesn't get so much. Does anyone feel it is a important topic "Refactoring" on OOA/D? I haven't had a chance to read something on Refactoring. But in most of Patterns books i see it sometime. It seems Refactoring code is to improve your existing code. If implement with Patterns in mind do you think Refactoring is still important? Does Patterns and Refactoring do the same thing or are the different concepts?
[This message has been edited by FEI NG (edited November 14, 2001).]
Tiger Scott
Ranch Hand

Joined: Mar 01, 2001
Posts: 223
Patterns and Refactoring are two different things. But there are refactoring paterns. I have lost the link- if I find it I would post it.
HTH
Sanjay
Fei Ng
Ranch Hand

Joined: Aug 26, 2000
Posts: 1242
Would you agree if you implement a design with Patterns in mind that refactoring is not needed. Don't know much about refactoring at the point but when refactoring existing code can Patterns apply?
Tiger Scott
Ranch Hand

Joined: Mar 01, 2001
Posts: 223
Any code may require refactoring at a later date- whether it has patterns or not. But when you are refactoring- you may use some patterns that help in refactoring.
HTH
Sanjay
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
Refactoring is how you discover you need patterns. Patterns are often not used the first time you write code because you don't see the need for it.
Consider the strategy pattern. Let's say I'm building a system that needs to render pages as HTML. At some later point I need to build PDF pages. At this point I might start out by copying all of my old code into a new version that does PDF. But then I find I need to do XML also...
So, I introduce the strategy pattern and refactor my old code to use a "RenderingStrategy".
This approach assumes you are not omniscient (a good assumption). I find this is the most common way to use patterns...
Kyle
------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
Fei Ng
Ranch Hand

Joined: Aug 26, 2000
Posts: 1242
Originally posted by Kyle Brown:
Refactoring is how you discover you need patterns. Patterns are often not used the first time you write code because you don't see the need for it.
Consider the strategy pattern. Let's say I'm building a system that needs to render pages as HTML. At some later point I need to build PDF pages. At this point I might start out by copying all of my old code into a new version that does PDF. But then I find I need to do XML also...
So, I introduce the strategy pattern and refactor my old code to use a "RenderingStrategy".
This approach assumes you are not omniscient (a good assumption). I find this is the most common way to use patterns...
Kyle

So, refactorying is a way to refine existing code as needed (when u see the need in the future) and Patterns is a technique in or helping refactorying existing code? Is there special refactoring techniques? Sanjay mentioned refactoring patterns,
Can anyone explain that pattern?

thanks guys!
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4446
    
    5

Originally posted by FEI NG:
Is there special refactoring techniques? Sanjay mentioned refactoring patterns, Can anyone explain that pattern?

You'll find more information about refactoring at http://www.refactoring.com
I don't know if they are what Sanjay was referring to but there are certain "patterns" that refactorings follow. Refactorings are given descriptive names such as "Replace Temp with Query" and "Move Method". This way, developers can discuss the refactorings at a high level without having to go into specifics. This is one of the reasons for having patterns: to facilitate communication.
As we know, software may have to undergo changes because of new requirements. A pattern that led to a good design before may not be able to suitably accomodate changes needed for new requirements. Thus the old pattern becomes an anti-pattern. This is where refactoring comes in. You find suitable refactorings that will modify the design a bit so that you can add functionality without messing up the design.
Junilu

[This message has been edited by JUNILU LACAR (edited November 14, 2001).]


Junilu - [How to Ask Questions] [How to Answer Questions]
Fei Ng
Ranch Hand

Joined: Aug 26, 2000
Posts: 1242
Originally posted by JUNILU LACAR:
You'll find more information about refactoring at http://www.refactoring.com
I don't know if they are what Sanjay was referring to but there are certain "patterns" that refactorings follow. Refactorings are given descriptive names such as "Replace Temp with Query" and "Move Method". This way, developers can discuss the refactorings at a high level without having to go into specifics. This is one of the reasons for having patterns: to facilitate communication.
As we know, software may have to undergo changes because of new requirements. A pattern that led to a good design before may not be able to suitably accomodate changes needed for new requirements. Thus the old pattern becomes an anti-pattern. This is where refactoring comes in. You find suitable refactorings that will modify the design a bit so that you can add functionality without messing up the design.
Junilu

[This message has been edited by JUNILU LACAR (edited November 14, 2001).]

"Thus the old pattern becomes an anti-pattern. This is where refactoring comes in. You find suitable refactorings that will modify the design a bit so that you can add functionality without messing up the design."
Thanks, I been wondering what is antipattern. On the above,
can finding suitable refactoring mean looking for another pattern that more suitable to this problem even thought it was some patterns causing the problem??? It seems learning Patterns is a MUST. Thank for the link, I am going to check out later.
Thats for clearing up about refactoring.
marc bowden
Greenhorn

Joined: May 10, 2001
Posts: 15
Hi
What does 'refactor' mean - is it synonymous with
rewrite - ie the process or rewriting (re-design/
programming) programs, modules, sub-systems or
other chunks of code?
Marc
Fei Ng
Ranch Hand

Joined: Aug 26, 2000
Posts: 1242
Originally posted by marc bowden:
Hi
What does 'refactor' mean - is it synonymous with
rewrite - ie the process or rewriting (re-design/
programming) programs, modules, sub-systems or
other chunks of code?
Marc

I think of it as refining existing code.
Rewrite doesn't seem fair for refactoring code.
Improving the design, performance, and manageability of object-oriented code without altering its interfaces or behavior. Refactoring shows you exactly how to spot the best opportunities for refactoring and exactly how to do it-step by step.
Thus i wouldn't call redesign either.
Thats only my opinion since i am new to refactoring too.

Doug Wang
Ranch Hand

Joined: Oct 05, 2001
Posts: 445
Originally posted by marc bowden:

What does 'refactor' mean - is it synonymous with
rewrite - ie the process or rewriting (re-design/
programming) programs, modules, sub-systems or
other chunks of code?

The word Refactoring has two difinitins. From the book "Refactoring", Martin Fowler wrote:
Refactoring(noun): a change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior.
Reforctor(verb): to restructure software by applying a series of refactorings without changing its observable behavior.


Doug.

Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep
Doug Wang
Ranch Hand

Joined: Oct 05, 2001
Posts: 445
Originally posted by Kyle Brown:
Refactoring is how you discover you need patterns. Patterns are often not used the first time you write code because you don't see the need for it.
Consider the strategy pattern. Let's say I'm building a system that needs to render pages as HTML. At some later point I need to build PDF pages. At this point I might start out by copying all of my old code into a new version that does PDF. But then I find I need to do XML also...
So, I introduce the strategy pattern and refactor my old code to use a "RenderingStrategy".
Excellent illustration of Patterns and Refactorings. Also Refactorings are needed as long as you add function to existing code while maybe no patterns are applied.

Doug.
Tiger Scott
Ranch Hand

Joined: Mar 01, 2001
Posts: 223
Refactoring probably excludes enhancements- by martin Fowler's defn:
The word Refactoring has two difinitins. From the book "Refactoring", Martin Fowler wrote:
Refactoring(noun): a change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior.
Reforctor(verb): to restructure software by applying a series of refactorings without changing its observable behavior
But in real life refactoring and enhancements go hand in hand. When ever you have an enhancement to make- you look at the code and refactor it too. Else there is no oportunity to refactor: after all it costs and has risk.
Sanjay
Fei Ng
Ranch Hand

Joined: Aug 26, 2000
Posts: 1242
Originally posted by Sanjay Bahal:
Refactoring probably excludes enhancements- by martin Fowler's defn:
The word Refactoring has two difinitins. From the book "Refactoring", Martin Fowler wrote:
Refactoring(noun): a change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior.
Reforctor(verb): to restructure software by applying a series of refactorings without changing its observable behavior
But in real life refactoring and enhancements go hand in hand. When ever you have an enhancement to make- you look at the code and refactor it too. Else there is no oportunity to refactor: after all it costs and has risk.
Sanjay

Hi Sanjay
how is the refactorying book by by martin Fowler.
Is it a good refactorying book for newcomer in refactorying?
I wonder if refactorying helps you in OOAD like patterns does. By looking and the definition i dont see it.
thanks guys
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by FEI NG:
Hi Sanjay
how is the refactorying book by by martin Fowler.
Is it a good refactorying book for newcomer in refactorying?
I wonder if refactorying helps you in OOAD like patterns does. By looking and the definition i dont see it.

IMO, "Refactoring" is a must-read. There is no book I learned more from about OOD than "Refactoring" (yes, "Design Patterns" is only second... )

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
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4446
    
    5

Originally posted by FEI NG:
I wonder if refactorying helps you in OOAD like patterns does. By looking and the definition i dont see it.

The book's refactoring examples are in Java and the reasons for refactoring are mostly based on good OO design principles so Yes, they do help you make your designs more OO.
Some samples:
Move Method - to eliminate the "feature envy" code smell.
Pull Up Constructor Body
Pull Up Method
Extract Class
Inline Class
Inline Method
Preserve Whole Object
Replace Record with Data Class
Replace Parameter with Method
etc.
Junilu
Tiger Scott
Ranch Hand

Joined: Mar 01, 2001
Posts: 223
Refactroing book is an excellent book, 2nd only to the Anti Patterns book, may be 3rd after the GOF book.
Sanjay
Fei Ng
Ranch Hand

Joined: Aug 26, 2000
Posts: 1242
Anti Patterns book?? the author?
thanks guys....

[This message has been edited by FEI NG (edited November 15, 2001).]
Tiger Scott
Ranch Hand

Joined: Mar 01, 2001
Posts: 223
Refactoring To Patterns by Joshua Kerievsky http://industriallogic.com/xp/refactoring/
HTH
Sanjay
Fei Ng
Ranch Hand

Joined: Aug 26, 2000
Posts: 1242
Refactoring To Patterns , interesting!! thanks for the link.

what about the Anti Patterns book ? who is the author?
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4446
    
    5

AntiPatterns: Refactoring Software, Architectures, and Projects in Crisis by William J. Brown, et. al.
Good book. You'll find out about "The Blob", "Lava Flow Code", "Poltergeists", and the "Golden Hammer". More commonly known ones discussed are "Spaghetti Code", "Analysis Paralysis" and "Cut-and-Paste Programming".
Junilu
Fei Ng
Ranch Hand

Joined: Aug 26, 2000
Posts: 1242
thank JUNILU
Rich Cohen
Greenhorn

Joined: Oct 01, 2001
Posts: 6
Originally posted by marc bowden:
Hi
What does 'refactor' mean - is it synonymous with
rewrite - ie the process or rewriting (re-design/
programming) programs, modules, sub-systems or
other chunks of code?
Marc

Refactoring is is one way of rewriting a program. It has a few
key characteristics. First, it preserves the program functionality. Second, it is done incrementally in small steps.
Other messages in this thread have quoted examples, so I won't.
Each step leaves the program fully functional. So you can stop
your refactoring work at (almost) any point and the program remains fully usable. This is in contrast with a traditional rewrite, in which the program may be unusable for long periods of time.
Refactoring is often associated with enhancing programs. This is because adding a new capability to a program may require some internal restructuring. You can refactor the program before adding your enhancement, so that making the enhancement is easier.
Practical use of refactoring is also associated with unit testing (and often with the JUnit testing framework), because after each refactoring step you want to verify that the program still works. The easiest way to do that is to run your regression tests. If you have a good set of regression tests for your program, then after each refactoring step you rerun the tests. If the tests pass, then you know your refactoring did not break the code. If the tests fail, then you know that the problem resides with the small code change you just made. You can either back it out or figure out why it was wrong. Since each refactoring step is small, this is pretty easy and the traditional "search for the bug" is largely eliminated.
This implicitly addresses one of the problems with a massive rewrite of code -- the resulting new mass of code requires massive testing and debugging. By performing the testing as you make the incremental code changes, you avoid the massive debugging step. Further, after a massive rewrite, the new code cannot be released until it has been entirely tested. So if you have to stop short of the complete effort, no part of the new code may be available for release. The incremental nature of refactoring avoids this all-or-nothing problem.
Another nice side effect of incremental refactoring is that you can improve an existing code base when you only have a few hours or a day to spend on it. For example, in the 3 or 4 days you spend building an enhancement, you might spend 1/2 day refactoring the existing code along the way.
david chen
Ranch Hand

Joined: Nov 30, 2001
Posts: 44
Is refactoring part of IBM Test 486 (OOAD/UML)?
Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Refactoring ??
 
Similar Threads
This weeks book Giveaway:
Refactoring
possibility of getting user name from session inside getconnection method
refactoring beginner
Head First Design Patterns: tests and patterns