Should I avoid using getX()
and setX()
as names for methods that aren't "traditional" getters or setters? (Let's define traditional as it only gets/sets the field and has no other effects.) I guess the question is does a method being named as such imply that it's a field of the object? Also, is it bad practice to do other things in them?
class Foo {
private Object bar;
private Object bazz;
// "traditional"
public Object getBar() {
return bar;
}
// bad practice?
public Object getBazz() {
doSomeOtherThing();
return bazz;
}
// not a field
public Object getJazz() {
return makeSomeJazz();
}
// ...
}
If getX()
isn't appropriate if it's not a field, what is? (Probably too broad, I realize.) Obviously something like makeJazz()
is probably better than getJazz()
if getJazz()
is considered bad here.
I guess another question is does it really matter? I like the idea of "self documenting code", but maybe I'm just worrying too much about something minor.