Criteria to decide what is private? (was: Encapsulation vs Extensibility)
- From: "Daniel T." <daniel_t@xxxxxxxxxxxxx>
- Date: Mon, 15 Jan 2007 13:17:21 GMT
"Mark Nicholls" <Nicholls.Mark@xxxxxxxxx> wrote:
But who is 'advocating' the use of private 'so much' ??
It follows from the opinion that implementation inheritance is
overused, so rather than use protected, use private and 'seal' your
implementations....
I don't think the public/protected/private choice has anything to do
with implementation inheritance being overused.
I suggest making a field private when there is some invariant
relationship between it and some other field that the class also
contains. That way no outside code can break the invariant. (If the
field in question has no such relationship with any other fields in the
class, maybe it's in the wrong class.)
I suggest making a method private when there is some invariant that the
method in question can't ensure on its own. That way no outside code can
break the invariant. (If the method can't ensure the invariant, then
maybe it's in the wrong class.)
If neither of the above pertains, then you might as well make the
field/method public. I can't think of any reason to make a field or
method protected off hand.
Based on these rules, most fields will end up private, most methods will
end up public.
.
- Follow-Ups:
- Re: Criteria to decide what is private? (was: Encapsulation vs Extensibility)
- From: Mark Nicholls
- Re: Criteria to decide what is private? (was: Encapsulation vs Extensibility)
- References:
- Encapsulation vs Extensibility
- From: Anthony Paul
- Re: Encapsulation vs Extensibility
- From: S Perryman
- Re: Encapsulation vs Extensibility
- From: Mark Nicholls
- Encapsulation vs Extensibility
- Prev by Date: Re: Encapsulation vs Extensibility
- Next by Date: Re: Encapsulation vs Extensibility
- Previous by thread: Re: Encapsulation vs Extensibility
- Next by thread: Re: Criteria to decide what is private? (was: Encapsulation vs Extensibility)
- Index(es):
Relevant Pages
|
|