Re: Buttocks tight [WAS: Re: What's the use of private?]
Daniel Pitts wrote:
I once heard (not sure if I agree with it) that
private methods needn't be as concerned with invariants. Pre-conditions
and post-conditions should be met by public methods, but private methods
may leave things hanging. Think fragments of a process.
It's the opposite. You can control invariants in private methods, because you
completely control the gozintos and comezouttas, thus you can ensure
invariants. Certainly one should be aware of the invariants of a private
method; in fact, many times the purpose of a private method is to ensure an
invariant.
With public methods, you also have to control invariants, for example by
massaging the inputs until they conform.
Wherever there is an algorithmic invariant, you have to be concerned with it.
Figuring out where and what the invariants are is an important part of
program analysis and design.
The 'assert' keyword is the Java way of enforcing invariants. You can
'assert' an invariant directly on arguments to a private method, but not on
arguments to a public method. First you have to run normal validity checks on
the inputs to a public method, then the invariant manifests as the
post-condition of the validity check.
--
Lew