Cloning code

R

Roedy Green

Lets say you invented a class with a boolean static final. Then you
decided you wanted TWO classes, identical except for the value of that
boolean static final.

How would you go about producing the two classes without actually
duplicating all the code?

--
Roedy Green Canadian Mind Products
http://mindprod.com

We are almost certainly going to miss our [global warming] deadline.
We cannot get the 10 lost years back, and by the time a new global agreement to
replace the Kyoto accord is negotiated and put into effect, there will probably
not be enough time left to stop the warming short of the point where we must not
go. ~ Gwynne Dyer
 
R

Roedy Green

Lets say you invented a class with a boolean static final. Then you
decided you wanted TWO classes, identical except for the value of that
boolean static final.

How would you go about producing the two classes without actually
duplicating all the code?

A labour intensive way is to change the static final to a constructor
parm, and every place it is used that was static will have to be made
an instance.

I wondered if there were something easier.
--
Roedy Green Canadian Mind Products
http://mindprod.com

We are almost certainly going to miss our [global warming] deadline.
We cannot get the 10 lost years back, and by the time a new global agreement to
replace the Kyoto accord is negotiated and put into effect, there will probably
not be enough time left to stop the warming short of the point where we must not
go. ~ Gwynne Dyer
 
A

angrybaldguy

Lets say you invented a class with a boolean static final.  Then you
decided you wanted TWO classes, identical except for the value of that
boolean static final.

How would you go about producing the two classes without actually
duplicating all the code?

What's the justification for making the flag static? It seems like
the simplest solution, void of any other information, would be to make
the boolean an instance flag and set it at construction time.

-o
 
A

Andreas Leitgeb

Roedy Green said:
Lets say you invented a class with a boolean static final. Then you
decided you wanted TWO classes, identical except for the value of that
boolean static final.

Rename the given class to some "Base*"-patterned name and make
it package/default visible.

Then create two new classes that subclass the one, and to them
add only any necessary constructors and that particular static
final field (which you could then remove from the Base class).

You may also need to move other static fields to the derived
classes, if they're intended for class-specific use.

Upon usage, this sibling-relation is invisible (unless you want
it visible and make the new base-class public), so it seems
like you have two independent classes.
 
M

Mark Space

Roedy said:
Lets say you invented a class with a boolean static final. Then you
decided you wanted TWO classes, identical except for the value of that
boolean static final.

How would you go about producing the two classes without actually
duplicating all the code?

Since you seem to be worried about the labor involved, I'll point out
that the editor in most IDEs (NetBeans for sure) can extract methods and
create super or subclasses in one command. Very handy. This could be
used to create the base abstract class you need. You can also extract
an interface from a class, which might be helpful as well.

I think regardless though you'll have to convert that static field into
a final instance field. There's no way I can think of to make a base
class discriminate between two different static fields.
 
L

Lothar Kimmeringer

Roedy said:
Lets say you invented a class with a boolean static final. Then you
decided you wanted TWO classes, identical except for the value of that
boolean static final.

How would you go about producing the two classes without actually
duplicating all the code?

Short answer: You can't
For a long answer you should explain what exactly you want
to do with that boolean and how it affects other parts of
the code of the class or other classes.


Regards, Lothar
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
R

Roedy Green

For a long answer you should explain what exactly you want
to do with that boolean and how it affects other parts of
the code of the class or other classes.

The particular example came up in the context of inWords, a program
that takes numbers and spells them out in words in various languages.

There are two Spanish variants, one for counting and one for putting
words on cheques. They are almost identical. There is a slightly
different in the way a static final String[] is initialised and some
of the code for handling "one".

As a quick and dirty solution I invented a static final boolean, and
cloned the code, one with true and one false. This of course has to
go, since "identical" code never stays identical.


--
Roedy Green Canadian Mind Products
http://mindprod.com

We are almost certainly going to miss our [global warming] deadline.
We cannot get the 10 lost years back, and by the time a new global agreement to
replace the Kyoto accord is negotiated and put into effect, there will probably
not be enough time left to stop the warming short of the point where we must not
go. ~ Gwynne Dyer
 
R

Roedy Green

Lets say you invented a class with a boolean static final. Then you
decided you wanted TWO classes, identical except for the value of that
boolean static final.

How would you go about producing the two classes without actually
duplicating all the code?

I had another idea, not beautiful.

static void setMonetary ( boolean m )

Where monetary is the boolean than defines the two variants.

setMonetary has to not only set the boolean, it must adjust any
initialization that was based on the boolean -- a dangerous practice.

You have to remove some finals, but other than that the existing
static structure can be left intact.

--
Roedy Green Canadian Mind Products
http://mindprod.com

We are almost certainly going to miss our [global warming] deadline.
We cannot get the 10 lost years back, and by the time a new global agreement to
replace the Kyoto accord is negotiated and put into effect, there will probably
not be enough time left to stop the warming short of the point where we must not
go. ~ Gwynne Dyer
 
R

Roedy Green

Since you seem to be worried about the labor involved, I'll point out
that the editor in most IDEs (NetBeans for sure) can extract methods and
create super or subclasses in one command

good point. I will split it into three classes with an abstract base
of common code.
--
Roedy Green Canadian Mind Products
http://mindprod.com

We are almost certainly going to miss our [global warming] deadline.
We cannot get the 10 lost years back, and by the time a new global agreement to
replace the Kyoto accord is negotiated and put into effect, there will probably
not be enough time left to stop the warming short of the point where we must not
go. ~ Gwynne Dyer
 
D

Daniel Pitts

Roedy said:
Lets say you invented a class with a boolean static final. Then you
decided you wanted TWO classes, identical except for the value of that
boolean static final.

How would you go about producing the two classes without actually
duplicating all the code?
Not use classes, but use instances instead, Possibly using a factory method.
 
R

Roedy Green

good point. I will split it into three classes with an abstract base
of common code.

The work is complete. I created some abstract methods invoked by the
base class that the other two implement.

This nicely documents the differences. You have abstract methods
where there are differences. You have the abstract method definitions
to define the differences. The code is quite a bit more readable than
using a boolean switch.

If anyone is curious, see http://mindprod.com/products1.html#INWORDS
and look at the classes Spanish, SpanishForCheques and SpanishBase.

Nothing of interest, except perhaps to newbies who were wondering what
we were talking about.
--
Roedy Green Canadian Mind Products
http://mindprod.com

We are almost certainly going to miss our [global warming] deadline.
We cannot get the 10 lost years back, and by the time a new global agreement to
replace the Kyoto accord is negotiated and put into effect, there will probably
not be enough time left to stop the warming short of the point where we must not
go. ~ Gwynne Dyer
 
B

blue indigo

A labour intensive way is to change the static final to a constructor
parm, and every place it is used that was static will have to be made
an instance.

I wondered if there were something easier.

Easier, maybe, with global search and replace on a copied and renamed
..java file. Cleaner, no. This class needs to be refactored.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

setLocale 0
Pseudostrings 3
New JDK out 1
Cookies 2
Dir scan 1
tracking logins 20
mindprod.com outage 1
Regex Puzzle 5

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top