Whether private getter methods is Evil?

Hi!

I was wondering whether it is bad or good to use private getter methods in a class rather than directly accessing private fields?

For instance, I have a getter method for each Swig component used in my program:

private JButton run;

private JButton getRun(){

if (run !=null){

return run;

}

run =new JButton("Run");

run.setToolTipText("Run");

run.setIcon(new ImageIcon(ResourceHelper.getImageResource("run.png")));

run.setEnabled(false);

run.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent event){

MessageDialog.showPleaseWait(TestRunnerUI.this,

"creating logger",new Runnable(){

publicvoid run(){

// bla bla bla

}

}

);

}

});

return run;

}

And so for each swing component....

But for example I have such a method:

privatevoid disconnect(){

mRunner =null;

mLogger =null;

log.log(Logger.INFO,"disconnected");

tree.setModel(defaultModel);

tree.setEnabled(true);

save.setEnabled(false);

saveAs.setEnabled(false);

load.setEnabled(false);

options.setEnabled(false);

run.setEnabled(false);

breakTests.setEnabled(false);

connect.setEnabled(true);

disconnect.setEnabled(false);

// ...

}

which changes properties of a lot of components...

I don't know whether I should use there private getter methods or not. From the design perspective it seems I should but it also adds some overhead I suppose...

[3086 byte] By [Hombrea] at [2007-11-27 1:58:55]
# 1
no, not evil. with a bit of a refactor, you could have a single getButton method, which you pass basic info into, such as the label for the button, and maybe an action listener. quite useful if your component has several buttons on it
georgemca at 2007-7-12 1:36:08 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

If your getter look like asprivate JButton getRun() {

return run;

}

Then it was fairly useless. But I don't see why they should be evil. Please elaborate "evil".

And please also elaborate "overhead". I also don't see any serious overhead.

Personally I should have called your "getRun" method "createButton", or "loadButton", or so. Because this is strictly looking not a getter.

BalusCa at 2007-7-12 1:36:08 > top of Java-index,Other Topics,Patterns & OO Design...
# 3
Nothing wrong with it. I often use them. In fact I prefer them. The JVM should be able to inline a private getter anyway. Plus they are much easier to refactor.
_dnoyeBa at 2007-7-12 1:36:09 > top of Java-index,Other Topics,Patterns & OO Design...
# 4
One might suppose that if there is anything more than just a trivial advantage found in using them then it would seem possible that something is wrong with the class design.After all if it is used 3 places does it matter?If it is used 300 then what exactly is the class
jschella at 2007-7-12 1:36:09 > top of Java-index,Other Topics,Patterns & OO Design...
# 5
Can't say I have ever found the need for private accessor methods. Writing them is all well and good until someone else comes along to maintain your code and just accesses the variable directly.-
YoGeea at 2007-7-12 1:36:09 > top of Java-index,Other Topics,Patterns & OO Design...
# 6

> I use accessor methods when writing persistent

> classes and javabeans. The key is not to use them by

> default, only when required. IMO this does not

> violate encapsulation, it hides implementation.

Agree. Inside the bean itself you shouldn't use it's accessors either.

> The

> main benefit being that should you need to refactor

> the way a value is retrieved or set you have

> localized the changes. Holub will tell you that if

> you are using an accessor method to retrieve a value

> from an object, in order to do some computation on

> that value, you should move the computation into the

> object's class instead (hence "getter and setter

> methods are evil").

Summarized, the public accessors are evil when the code behind does not *only* return/set the encapsulated value. Solution: refactor, split and rename them to "createSomething", "lookupSomething", "buildSomething", or so.

In this case the topicstarter was talking about private getters tho.

BalusCa at 2007-7-12 1:36:09 > top of Java-index,Other Topics,Patterns & OO Design...
# 7
BalusC- Sorry I edited my post when I noticed he was talking about private accessor methods!
YoGeea at 2007-7-12 1:36:09 > top of Java-index,Other Topics,Patterns & OO Design...
# 8
I had such an idea ;)
BalusCa at 2007-7-12 1:36:09 > top of Java-index,Other Topics,Patterns & OO Design...
# 9

Not evil, but it does not 'buy' you anything either. Private accessors will return private instance (or static) variables.However, you can access these directly in the class already.

There are other use cases, such as exposing a private instance variable via a protected or package-private accessor to subclasses or package classes. Here, the other classes cannot (and should not) access the private instance variable in the first place. So, you have better control over an object's state by exposing the variable in this way (same with public accessors, really).

But for a private variable, you cannot 'hide' the variable and 'force' other developers to use your method. In terms of run-time performance, I agree that this will be negligible. But in terms of increasing development time, you may end up producing less functionality overall simply to expose variables that are already exposed to the enclosing class.

- Saish

Saisha at 2007-7-12 1:36:09 > top of Java-index,Other Topics,Patterns & OO Design...
# 10

> Can't say I have ever found the need for private

> accessor methods. Writing them is all well and good

> until someone else comes along to maintain your code

> and just accesses the variable directly.

>

> -

Could be useful if you have something you don't want to expose to the outside world YET.

Make it private, and at some later date change that to public.

But I'd more likely make it protected or package private so that I can at least write a test harness for it.

jwentinga at 2007-7-12 1:36:09 > top of Java-index,Other Topics,Patterns & OO Design...