More permanent stuff at http://www.dusbabek.org/~garyd

23 September 2005

getter vs protected/public final

Ok. So am programming an abstract class and I realize that a private member will need to be accessible in the children. So tell me which of these approaches is worse:

public class AbstractClass {
protected final Object makeAviailable;

public AbstractClass(Object makeAvailable) {
this.makeAvailable = makeAvailable;
}
}
or this...
public class AbstractClass {
private final Object makeAviailable;

public AbstractClass(Object makeAvailable) {
this.makeAvailable = makeAvailable;
}

protected getAvailable() { return makeAvailable; }
}


The point is to make an object available to children (bit it's available to the whole package, I know), and it has to be immutable. Each example gets that done.

Any programming instructor and many purists would tell me the first example is bad, but why? Am I exposing any more of the implementation than I was before? No. To be sure, the first example is less flexible, as the getAvailable() method can allow for data massaging to be done if it were needed.

Personally, I like the first example because there is less code. Less code means less bugs if you ask me. It is just simpler.

And I don't know that it is bad practice either. It is simply a tradeoff.

0 comments: