• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Refactoring

 
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In many threads this week there have been given many different definitions. Moreover you can find some definitions on Martin Fowler site:

Refactoring

./pope
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....
 
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Siu, method calls are dirt cheap in Java. Performance is not a valid reason to not encapsulate fields.
 
blacksmith
Posts: 1332
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
um........I see
 
Daniel Mayer
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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....
reply
    Bookmark Topic Watch Topic
  • New Topic