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 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" Watch "Refactoring" New topic
Author

Refactoring

Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi ranchers,
I know what is OO, pattern and UML but..............
What is refactoring?
Is it a kind of software engineering process to refine program?
thanks for your time to answer


Francis Siu
SCJP, MCDBA
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
In many threads this week there have been given many different definitions. Moreover you can find some definitions on Martin Fowler site:

Refactoring

./pope


blog - InfoQ.com
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
In many threads this week there have been given many different definitions.
Improving the Design of Existing Code?
But do we design the structure first and then implement the coding?
and I think about that refactor a system over an extended period of time, small program is possible but for large program, it may not be suitable in business situation. So why do you need refactoring? For interest or something else?
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
Originally posted by siu chung man:
In many threads this week there have been given many different definitions.
Improving the Design of Existing Code?
But do we design the structure first and then implement the coding?



What would be the scope of design after the coding is done?


and I think about that refactor a system over an extended period of time, small program is possible but for large program, it may not be suitable in business situation. So why do you need refactoring? For interest or something else?


Refactoring in large projects is a iterative task which improves the maintainance of the project. Moreover well scheduled refactorings can save the project.

./pope
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by siu chung man:
Improving the Design of Existing Code?
But do we design the structure first and then implement the coding?
Refactoring is the act of recognizing that the current design has a smell you'd like to get rid of and actually getting rid of it by changing the design in a way that improves it without changing/adding/removing any functionality.


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

Joined: Jun 08, 2002
Posts: 3178
Originally posted by siu chung man:
hi ranchers,
I know what is OO, pattern and UML but..............
What is refactoring?
Is it a kind of software engineering process to refine program?
thanks for your time to answer


siu,
I do want to suggest you to have a look at the sample chapter- 2 of Joshua's "Refactoring to Patterns" book.. That chapter mainly talks about the refactoring and it's really worth to have that chapter as a sample...

The book promotion was held last week in this forum... But I can say that the sample chapter is well written and easy to understand... I've read it already...

Hope it helps...

P.S., Here again is the link to the sample chapter...
[ September 11, 2004: Message edited by: Ko Ko Naing ]

Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
siu,
BTW, you might also be interested in this thread, where we discussed about the sample chapter available in the books as well as the content in the sample chapter-2 of the book....

Just my 2 cents....
Sathya Srinivasan
Ranch Hand

Joined: Jan 29, 2002
Posts: 379
To me,

Refactoring is the process of reorganizing existing code such that it improves its readability and reusability.

Martin Fowler's book names various processes for better communication.

If, during a course of writing a program, you have extracted some piece of code into a separate method so that you can call that method from some other place, you have already refactored your code.

So, the concept is not new. It has been formalized, so that people can say, 'aahhh...' and use these formal names as a means of communication.

By the above definition, refactoring is something you do on a code. Typically, this exercise is done when you want to extend an existing code to add a new feature or make an enhancement to an existing feature.

As a result, it gels well with new software methodologies like XP and Agile Development.


Cheers, Sathya Srinivasan - SCJP 1.2, SCWCD 1.2, SCMAD 1.0
Co-Author of Whizlabs SCMAD Certification Exam Simulator and SCMAD Exam Guide Book
Al Korov
Greenhorn

Joined: Sep 11, 2004
Posts: 21
Perhaps a very simple example helps to explain and learn the topic.

Pretend we've a method that applies a shipping fee to an order. The rules of the system describe that shipping is free on orders greater than or equal to $25, and $3 for orders less than $25. One implementation of this logic might be:

It occurs to me that, while this code isn't terribly complicated or confusing, one or more opportunities to better express the intent of the logic exists, if the code were only reorganized a little bit. Considering the expression preShippingCost < 25 it occurs to me that without knowledge of the business rules, this expression is perhaps not perfectly apparent. So, I might introduce a more meaningful expression of this rule, and use a refactoring to do so.

Notice that the behavior of the system hasn't changed, while the design has arguably been improved.

Does this perhaps help a bit to clarify what refactoring is and how it helps to improve the design of existing code?
Daniel Mayer
Ranch Hand

Joined: Sep 09, 2004
Posts: 103
Originally posted by siu chung man:
Improving the Design of Existing Code?
But do we design the structure first and then implement the coding?


Coding the design will teach you new things about it. Refactoring is the act of incorporating that learning into the existing design.


and I think about that refactor a system over an extended period of time, small program is possible but for large program, it may not be suitable in business situation.


Quite to the contrary: the bigger the system, the harder it is to come up with a good design upfront. Continous Refactoring really is vital to big projects!
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
thanks all
Does this perhaps help a bit to clarify what refactoring is and how it helps to improve the design of existing code?
yes, it is simple and easy to read..
I consider that the program contains some drawback as it will run a little bit slower than the first one. (may be) :roll:
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
Originally posted by siu chung man:
I consider that the program contains some drawback as it will run a little bit slower than the first one. (may be) :roll:


Why you think so? Because of the additional calls? If it would be like this any class would define its fields public in order to be accessed directly without an additional method call :-).

./pope
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
If it would be like this any class would define its fields public in order to be accessed directly without an additional method call
ar....you may be misunderstanding something, my question is not related to the accessibility. I considered about the additional function(method) call, when we call function, the program may take time to copy the value (if it is not an object) to the parameter list and some overheads that we may consider in a large program, so the running time will be longer and longer (may be). :roll:
Actually, we know that refactoring involves design, someone likes to use an additional method call and other's may like documentation.
thanks
have fun
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
About this Martin Fowler has a very simple explanation: [..] 9 times out of 10, it won't matter. [...] when you have a better factored code, you will find more powerfull optimizations. (Replace Temp with Query).

./pope
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Siu, method calls are dirt cheap in Java. Performance is not a valid reason to not encapsulate fields.
Warren Dew
blacksmith
Ranch Hand

Joined: Mar 04, 2004
Posts: 1332
    
    2
siu chung man:

I considered about the additional function(method) call, when we call function, the program may take time to copy the value (if it is not an object) to the parameter list and some overheads that we may consider in a large program, so the running time will be longer and longer (may be).

I would think a good compiler ought to be able to inline the function call, so that the resultant byte code would be the same as that for accessing a public data member (basically a fetch from memory). I'm not that familiar with Java compilers, but that's how it works in other languages.
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
um........I see
Daniel Mayer
Ranch Hand

Joined: Sep 09, 2004
Posts: 103
Originally posted by Warren Dew:
I would think a good compiler ought to be able to inline the function call, so that the resultant byte code would be the same as that for accessing a public data member (basically a fetch from memory). I'm not that familiar with Java compilers, but that's how it works in other languages.


In Java, this optimization is not done while compiling the source to byte code, but while compiling the byte code to machine code at runtime: http://java.sun.com/developer/technicalArticles/Networking/HotSpot/inlining.html
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
Originally posted by Daniel Mayer:


In Java, this optimization is not done while compiling the source to byte code, but while compiling the byte code to machine code at runtime: http://java.sun.com/developer/technicalArticles/Networking/HotSpot/inlining.html


It's a good article... I have just read it... Thanks for that Daniel....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Refactoring