Any way to restrict class variables only to some methods?

S

Shawn

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.
 
O

Oliver Wong

Shawn said:
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.

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
 
M

Mark Jeffcoat

Shawn said:
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.

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
 
R

Robert Klemme

Mark Jeffcoat said:
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
 
P

Patricia Shanahan

Mark said:
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

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
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top