For Programmers: Free Programming Magazines  


Home > Archive > Java Help > July 2004 > What does "refactoring" of a project mean ?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author What does "refactoring" of a project mean ?
Anan H. Samiti

2004-07-28, 9:09 pm

Can someone explain me what "refactoring" of a (java) project mean?
I searched in google for a good explanation but found only mess (in the first 100 items).

Anan

VisionSet

2004-07-28, 9:09 pm



"Anan H. Samiti" <anan22@hotmail.com> wrote in message
news:ce5of5$5kc$04$1@news.t-online.com...
> Can someone explain me what "refactoring" of a (java) project mean?
> I searched in google for a good explanation but found only mess (in the

first 100 items).
>
> Anan
>


The process of reviewing and improving code.
There are IDE's that specialise in assisting this kind of thing.
You can review call trees, see where a method is used, rename
variables/classes more appropriately etc.

--
Mike W


Roedy Green

2004-07-28, 9:09 pm

On Tue, 27 Jul 2004 16:22:30 +0200, anan22@hotmail.com (Anan H.
Samiti) wrote or quoted :

>Can someone explain me what "refactoring" of a (java) project mean?


see http://mindprod.com/jgloss/refactoring.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Thomas Fritsch

2004-07-28, 9:09 pm

Anan H. Samiti wrote:

>Can someone explain me what "refactoring" of a (java) project mean?
>I searched in google for a good explanation but found only mess (in the first 100 items).
>
>Anan
>
>

A quite good explanation (at least in my opinion) is given at
http://en.wikipedia.org/wiki/Refactoring (espcially the first 2
paragraphs there)

--
Thomas<dot>Fritsch<squiggle>ops<dot>de

Darrell Grainger

2004-07-28, 9:09 pm

On Tue, 27 Jul 2004, Anan H. Samiti wrote:

> Can someone explain me what "refactoring" of a (java) project mean?
> I searched in google for a good explanation but found only mess (in the first 100 items).


Refactoring is improving the design of existing code. It does not add new
feature or change the behaviour of the existing features. It improves the
internal structure. It is just a way of making the code more maintainable.
Over time, software will become difficult and costly to maintain. By
occasionally refactoring it, you will be able to avoid this.

--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vice.president@whitehouse.gov
Thomas Weidenfeller

2004-07-28, 9:09 pm

Anan H. Samiti wrote:
> Can someone explain me what "refactoring" of a (java) project mean?
> I searched in google for a good explanation but found only mess (in the first 100 items).


http://www.extremeprogramming.org/rules/refactor.html

/Thomas
ak

2004-07-28, 9:09 pm

> Can someone explain me what "refactoring" of a (java) project mean?
> I searched in google for a good explanation but found only mess (in the

first 100 items).
>

http://www.refactoring.com/

--
Andrei Kouznetsov
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader


Scott Ellsworth

2004-07-28, 9:09 pm

In article <ce5of5$5kc$04$1@news.t-online.com>,
anan22@hotmail.com (Anan H. Samiti) wrote:

> Can someone explain me what "refactoring" of a (java) project mean?
> I searched in google for a good explanation but found only mess (in the first
> 100 items).


Refactoring is simply code improvement done mostly through code movement
or minor changes. For example, renaming a method from "dagnabit" to
"searchForDuplicates" is a simple refactoring. Extracting the
"searchForNextToken" guts of "searchForDuplicates" is another, and
useful if more than one routine needs it. It is something that most
developers do on a regular basis, but having a set of common names for
the actions, and some agreement on a basic set of such actions, makes it
more likely that IDE makers will support these actions.

Martin Fowler wrote a book called "Refactoring" in which he popularized
this term. He also categorized them, and suggested that IDEs should do
some of this automatically. This push led, I suspect, to its heavy
influence on XP and the inclusion of so-named refactoring support in
IDEA, Eclipse, and TogetherJ.

What makes it better than glorified cut and paste is the understanding
your IDE can have of your code. For example, you could do a search and
replace to find the "dagnabit" calls I mentioned above, but if you are
moving methods to a subclass, or perhaps adding a parameter to a method,
the search and replace gets a lot harder to write without introducing
errors. Further, computers tend to be good at repetitive tasks like
adding a default value to every use of a method.

Scott
Shane Mingins

2004-07-28, 9:09 pm

"Scott Ellsworth" <scott@alodar.com> wrote in message
news:scott-7B2302.13445527072004@news.west.cox.net...
> What makes it better than glorified cut and paste is the understanding
> your IDE can have of your code.


I agree that a good IDE makes Refactoring alot easier and safer but the
precondition to refactoring is a suite of solid tests.

Shane

--
http://www.jetbrains.com/idea/custo...stimonials.html


thufir.hawat@mail.com

2004-07-28, 9:09 pm

On Wed, 28 Jul 2004, Shane Mingins wrote:

> "Scott Ellsworth" <scott@alodar.com> wrote in message
> news:scott-7B2302.13445527072004@news.west.cox.net...
>
> I agree that a good IDE makes Refactoring alot easier and safer but the
> precondition to refactoring is a suite of solid tests.
>
> Shane
>
> --
> http://www.jetbrains.com/idea/custo...stimonials.html


In the filesystem pane Netbeans lets me rename classes and will
automatically update at least the class name, which is good, but then I
have to click around to fix the comments. It'd almost be easier to use vi
and do the /$/whatever search and replace.

This goes to what tools you know; blah blah when you have a hammer blah
blah something about nails and how everything looks like a .... nail.

I can't believe renaming a few classes counts as refactoring. Isn't
refactoring inherently painful? doesn't it more often mean re-design and
doing away with stuff and adding new stuff and simplifing the API, etc?


Thufir Hawat
<http://www.shaw.ca/members/hawat/source/>
Bryce

2004-07-28, 9:09 pm

On Wed, 28 Jul 2004 05:59:57 GMT, thufir.hawat@mail.com wrote:

>I can't believe renaming a few classes counts as refactoring. Isn't
>refactoring inherently painful? doesn't it more often mean re-design and
>doing away with stuff and adding new stuff and simplifing the API, etc?


It goes beyond that. Imagine you have a class package.A. Class A is
referenced in no less than 100 other classes. One day, you decide you
want to rename that class to packageb.B... If you just change the
class name, and the filename, you have 100+ classes now that won't
compile because they reference the old class...

In Eclipse, and i presume any IDE that implements it, when you rename
a class or method (or any element for that matter), it will also
change references to that class, comments if you want, etc.

--
now with more cowbell
Will Hartung

2004-07-28, 9:09 pm

<thufir.hawat@mail.com> wrote in message
news:Pine.LNX.4.60.0407272248330.3235@F0106000nr6n02427.ip.funjpnoyr.arg...

> I can't believe renaming a few classes counts as refactoring. Isn't
> refactoring inherently painful? doesn't it more often mean re-design and
> doing away with stuff and adding new stuff and simplifing the API, etc?


Not partucularly painful, it's more based on how pervasive the bit you're
refactoring is.

In essence, all refactoring really is is simply making an effort to
eliminate redundant code and replace it with a better abstraction.

It's running through your code base and trying to enforce a "write it once"
principle.

Many times during the haste of coding, people run about going on "cut and
paste" paste sessions and changing some little bit, but leaving a lot of
boiler plate. Many times that boiler plate can be pulled into a common
method with the little change being parameterized. Then you go through and
replace those instances with simply a method call.

Simply put, you have a process that you've written and everything is fine.
Then requirements come for a very similar process. Now, you have the option
of simply cut-n-pasting the original into the new version and beating on it
to fit, or you can pull out the key abstractions of the process so you can
share the generic/similar parts between the first version and the new
version. That's the basic premise behind refactoring.

Regards,

Will Hartung
(willh@msoft.com)


Shane Mingins

2004-07-28, 9:09 pm

"Will Hartung" <willh@msoft.com> wrote in message
news:2mqbugFokeocU1@uni-berlin.de...
>
> It's running through your code base and trying to enforce a "write it

once"
> principle.
>


In another discussion some time ago Robert C.Martin passed on something that
Kent Beck had said:

"Kent has asserted something very interesting. He said that all design
principles boil down to the elimination of duplication."

Shane


--
"there's a lot of bad code out there, and someone must be writing it" - Ron
Jeffries



Roedy Green

2004-07-28, 9:09 pm

On Thu, 29 Jul 2004 08:45:58 +1200, "Shane Mingins"
<shanemingins@yahoo.com.clothes> wrote or quoted :

>"Kent has asserted something very interesting. He said that all design
>principles boil down to the elimination of duplication."


Java has a lot of unavoidable duplication and bubblegum:

e.g.

Vector<Dog> dogs = new Vector<Dog>( 101 );

-- rhs identically repeats info on lhs.


i = (int) somelong();

-- you tell Java that i is an int on every cast rather than just one
place.


no way to declare bounds on a variable.
-- you tell Java the bounds every time you explicitly check them.

no way to declare aux variables LIKE another.
-- you build in a type in hundreds of places in local variables with
no logical link to their base.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Grant Wagner

2004-07-29, 3:58 pm

Roedy Green wrote:

> Java has a lot of unavoidable duplication and bubblegum:
>
> i = (int) somelong();
>
> -- you tell Java that i is an int on every cast rather than just one
> place.


If you're saying what I think you're saying, you're proposing something
like:

(int) i; ? or maybe int (int) i; ?

In other words, anytime something is assigned to i, cast it to an int?
Interesting, I think I like it. Wouldn't it just mean the compiler would
have to do a substitution on each assignment to i, replacing i = somelong();
with i = (int) somelong(); prior to compiling? I mean, assuming you could
come up with a declaration syntax that the compiler could recognize without
confusing it with something else, it doesn't seem like it should be too hard
to implement.

I suppose a lot of your ideas would be very easy to implement if Java had a
preprocessor.

> no way to declare bounds on a variable.
> -- you tell Java the bounds every time you explicitly check them.


If variables can have bounds, then Java would need to check them on each
assignment. Even if no bounds are specified for that particular variable,
Java would have to check to see if there were any bounds defined for the
variable. I think this might have a performance impact.

In other words, at the very least:

int x = 0;
for (int i = 0; i < 100000; i++) {
// this assignment requires a comparison to see if any bounds
// were defined for "x"; none were, okay to do the assignment
x += 1;
}

It seems to me the VM would have to do extra work to decide if a variable is
bound or unbound before it's used. Oh, wait, the bounds checking bytecode
would be added at compile time wouldn't it? So no performance impact for
unbound variables. What happens if an assignment exceeds the bounds declared
on a variable? A runtime exception gets thrown? Now you need to wrap every
bound variable assignment in a try/catch block in case the assignment
exceeds the bounds.

You've added a try/catch block and exception handling to remove code that
read "if (y > 5 && y < 10) x = y;". I don't know if you've gained anything.

Or am I misunderstanding what you're saying?

--
Grant Wagner <gwagner@agricoreunited.com>

Roedy Green

2004-07-29, 8:57 pm

On Thu, 29 Jul 2004 17:26:58 GMT, Grant Wagner
<gwagner@agricoreunited.com> wrote or quoted :

>
>(int) i; ? or maybe int (int) i; ?
>
>In other words, anytime something is assigned to i, cast it to an int?


My proposal was to use a generic cast, where you acknowledge a
data-loss may occur, but the type information comes from the rhs.

e.g.

int i = 42;
long ll = daysSinceEaster;

i = (cast)ll;

which means
i = (int)ll;

This code will continue to work unmodified if you narrow i.

short i = 42;
long ll = daysSinceEaster;

i = (cast)ll;


In the current way, you have to comb the code changing SOME of the
(int) to (short).

(cast) also helps widening in an even more important way.

What happens if you realize that i has to be wider because say the
national debt has ballooned past 32 bits.

if you wrote code like this:

int debt = ...;

debt = (int)debtOfMongolia;

Then you modified it to:

long debt = ...;

but forgot to change

debt = (int)debtOfMongolia;

You won't get a compile error.

However, had you written

debt = (cast)debtOfMongolia;

Then the code could work, and you would get a lint error -- a nugatory
(cast).

Further, I would give (cast) even more work to do, also handling what
we consider simple conversion, e.g. String <-> int.


You would have a uniform syntax to do all the conversions described by
the conversion Amanuensis in their irregular French verb profusion.

http://mindprod.com/converter.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Roedy Green

2004-07-29, 8:57 pm

On Thu, 29 Jul 2004 17:26:58 GMT, Grant Wagner
<gwagner@agricoreunited.com> wrote or quoted :

>I suppose a lot of your ideas would be very easy to implement if Java had a
>preprocessor.


the cast idea could be handled with a preprocessor or a SCID.

It requires not change to the JVM. It is merely a maintenance
convenience. What drives me nuts is how blind the Java designers seem
to be to the needs of maintenance programmers.


--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Roedy Green

2004-07-29, 8:57 pm

On Thu, 29 Jul 2004 17:26:58 GMT, Grant Wagner
<gwagner@agricoreunited.com> wrote or quoted :

>If variables can have bounds, then Java would need to check them on each
>assignment. Even if no bounds are specified for that particular variable,
>Java would have to check to see if there were any bounds defined for the
>variable. I think this might have a performance impact.


When I was designing Abundance I worried about that and created two
sorts of number, kinds with bounds and rules, and raw computational
ones like Java primitives. It turns out the overhead was a lot less
than I imagined it might be, much the way people fret over array
bounds checking, not being aware of all the other housekeeping goes on
under the covers.

The advantage is you glue the bounds to the variables in one place.
From then on that variable CAN'T go out of bounds, until you change
the bounds. My smart objects are usually GUI objects as well, so these
rules also apply to data entry. This is like array bounds checking on
steroids.

Further you have two options when you have overflow, throwing an
exception or corraling the value. In many cases, substituting the min
or max value is the best thing to do avoid aborting a space mission.
It isn't supposed to happen, but that is the most reasonable thing to
do given you did not plan for it, e.g. a force reading off the scale,
number of pieces of carry on luggage, weight of a person.

The other thing is, when you know that safety net is there, you can
rely on it in ordinary coding to handle the unusual conditions for
you. E.g. if you have room for only 3 telephone numbers, the safety
net will by default just index on the first or last if you try to
scroll past them.


--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Chris Smith

2004-07-29, 8:57 pm

Roedy Green wrote:
> Java has a lot of unavoidable duplication and bubblegum:
>
> e.g.
>
> Vector<Dog> dogs = new Vector<Dog>( 101 );
>
> -- rhs identically repeats info on lhs.


I'm sure you'll hear some correction that the left hand side merely
needs to be compatible with the right-hand side. For example, it would
be far better to say List<Dog> instead of Vector<Dog>, or maybe even
List<? extends Animal>, if that's what you mean. There is a conscious
decision-making process going on there.

Nevertheless, yes there is redundancy. The trick is to decide if it's
good redundancy or bad redundancy. Bad redundancy is the kind of
redundancy that makes it more difficult to change something that needs
to be changed. Good redundancy is the kind of redundancy that makes it
more difficult to change something that shouldn't be changed. Good
redundancy essentially lets you continually let the compiler know about
your assumptions, so that the compiler (which makes far fewer mistakes
than you do) can check those assumptions for you, and ensure that they
are never violated by yourself or a maintenance programmer five years
down the road. The more little pieces of information have to match up
in your program (in a way that's consistent with the actual assumptions
you've made in developing), the more likely you are to catch a mistake
that violates that consistency. Yet you could reasonably call all of
that information "redundant".

This often seems to boil down to the same thing as redundancy of
functional code versus redundancy of information. That's not a full
equivalence, though, because even information-redundancy can be bad if
it's not at a high enough level of abstraction. There are other
considerations as well, such as how easy it is to change the assumptions
when appropriate; so ironically refactoring support in a browser makes
it more acceptable to duplicate more things throughout your code. And,
of course, duplication that isn't checked by the compiler, or
duplication below a level of abstraction, is useless.

There are those, used to working in type-less languages and especially
active in advocacy groups for typeless languages, that sometimes come up
with the idea that all redundancy is a bad thing. If you believe that,
then Java is a ludicrously poor language for you to have chosen for a
project. Go learn ye some Smalltalk!

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Chris Smith

2004-07-29, 8:57 pm

thufir.hawat@mail.com wrote:
> I can't believe renaming a few classes counts as refactoring. Isn't
> refactoring inherently painful? doesn't it more often mean re-design and
> doing away with stuff and adding new stuff and simplifing the API, etc?


This is an important point, to me. Renaming a class is not really what
refactoring is about. Refactoring is about cleaning up abstractions and
improving the design of code.

However, renaming (and similar things) is exactly what IDE refactoring
support is about! That's because as you redesign, code will become more
general and take a larger role, and its previous abstraction will have
changed. The natural thing to do is rename it. (After all, what would
you think of code that has a class called BreakfastPlanner generating
menus for a restaurant that stopped serving breakfast years ago... just
because someone modified it to handle lunch and dinner as well?) And
yet, with a manual approach, renaming that class and the hundreds of
places that refer to it would take hours! (assuming you carefully avoid
coincidental naming conflicts that would trip up text search and
replace, and also look for potential issues such as naming conflicts in
the result ahead of time and deal with them).

Unfortunately, because of this, tens of thousands of people who have
used the "rename" feature of their IDE now think that they know what
refactoring is all about. Bull! Refactoring is about thinking about
the design of your code, not choosing a menu item to rename a class.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Karl von Laudermann

2004-07-30, 8:57 pm

anan22@hotmail.com (Anan H. Samiti) wrote in message news:<ce5of5$5kc$04$1@news.t-online.com>...
> Can someone explain me what "refactoring" of a (java) project mean?
> I searched in google for a good explanation but found only mess (in the first 100 items).


There's an excellent essay about refactoring at
http://www.joelonsoftware.com/artic...0000000348.html
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com