Hi,
I have a huge class, which has about 30 class variables. And the class
has many methods. Each method doesn't need all the 30 class variables,
only need a subset of them. In another word, say the 30 class variables
can be grouped into 6 groups (say group A, group B, .., group F).
public class MyHugeClass
{
//group A class variables
private double a, b, c, d, e;
//group B class variables
private double w, t;
..
//group F class variables
private double t1, t2,t3;
public void method1() //only need group A and F variables, but it can
access all, unfortunately
{
...//code
}
public void method2() //only need group B and F variables, but it can
access all, unfortunately
{
...//code
}
...//more methods omitted
} //end of class
The method method1() only need group A and group F class variables,
doesn't need others. The method method2() only need group B and group F
class variables, doesn't need others. But since method1() and method2()
all below to the class, so they have access to all the class variables
(from group A to group F), which is more than they need and could
introduce errors. As you can imagine, this is a huge class. More
restrictions, better, because it could force the errors to be detected.
Thank you very much.
Oliver Wong - 27 Oct 2006 20:03 GMT
> Hi,
>
[quoted text clipped - 41 lines]
>
> Thank you very much.
There's no way to solve the problem as you've stated. However, if the
"higher level goal" is to force more errors to be detected, perhaps you
could elaborate on what kind of error you suspect might occur, and we can
discuss designs or programming practices which might minimize the chance of
those errors occuring (or at least, being able to detect those errors as
soon as possible).
- Oliver
Mark Jeffcoat - 27 Oct 2006 21:08 GMT
> The method method1() only need group A and group F class variables,
> doesn't need others. The method method2() only need group B and group F
[quoted text clipped - 3 lines]
> introduce errors. As you can imagine, this is a huge class. More
> restrictions, better, because it could force the errors to be detected.
You're very close to the right answer. You know you've
got a class that's too big, and doing too much. You've
got a decently clear rule for figuring out which methods
belong together (they use the same subset of class variables).
So split it into two classes.
http://www.refactoring.com/catalog/extractClass.html

Signature
Mark Jeffcoat
Austin, TX
Robert Klemme - 28 Oct 2006 13:12 GMT
> You're very close to the right answer. You know you've
> got a class that's too big, and doing too much. You've
> got a decently clear rule for figuring out which methods
> belong together (they use the same subset of class variables).
Absolutely!
> So split it into two classes.
... or rather n classes if there are n sets of variables.
robert
Patricia Shanahan - 28 Oct 2006 13:36 GMT
>> The method method1() only need group A and group F class variables,
>> doesn't need others. The method method2() only need group B and group F
[quoted text clipped - 12 lines]
>
> http://www.refactoring.com/catalog/extractClass.html
Other refactorings may be needed first to improve the separation. For
example, it may be desirable to split up methods that operate on more
than one logical group of variables, such as method1 and method2.
I would start by thinking about the variables, and select a group of
variables that you know belong together.
Next, decide what operations they should support. Do extractMethod
http://www.refactoring.com/catalog/extractMethod.html
until the only access by the rest of the class to those variables is
through those methods, and those methods do not interact directly with
any other variables in the class.
Once you have a logical group of variables and associated methods such
that the only interactions with the rest of the class are through method
calls, do the extractClass.
Repeat as needed until you no longer feel any urge to limit access
within a class.
Patricia