On encapsulation
Bastiaan Veelo
Bastiaan at Veelo.net
Tue May 15 12:37:46 UTC 2018
On Friday, 11 May 2018 at 10:32:39 UTC, KingJoffrey wrote:
> On Friday, 11 May 2018 at 09:47:39 UTC, Dukc wrote:
>> You can use D private with Java-like "only for members
>> functions" meaning by putting it alone in it's module, if you
>> want.
>
> Yes. I do know this. But that is essentially.. a hack.
Funny that you call this a hack, see below.
> A 'private' declaration should be part of the implementation of
> the class, not the class+module. Some say this is just a
> philosphical difference - I say it's a key design issue for
> structured programming.
(OT: "structured programming" is generally understood as a
concept introduced by the Pascal programming language, and has
nothing to do with OOP. You probably use the term in a broader
sense here though, and that's OK.)
[...]
> So now, in D, to understand the class, you must also understand
> the module.
Yes.
> This is a backwards step in language design, in my opinion -
> for various reasons which are easy to examine further.
Sometimes it is good to do a step back, if thereby you correct
steps taken in the wrong direction. I have been watching video's
of Robert Martin lately, a.k.a. Uncle Bob. His experience is far
greater than mine, and his view on encapsulation is pretty clear.
Here are a few of his quotes, made in for example [1]:
"We had absolutely perfect encapsulation in C."
"This all got screwed up by C++."
"In C++, header files got polluted with variables and we lost
encapsulation. To fix that, we did this really horrible hack: we
invented the word 'private'."
"'private:' is a terrible hack which attempts to recapture at
least some tiny bit of the lost encapsulation that we had in C."
"Then [...] we invented the word 'protected' and ever since
then we have been figuring out other ways to open that up so now
we've got 'package scope' and this cope and that scope and God
help us all there's no encapsulation left."
"OO has ruined encapsulation."
I am not suggesting that everybody should accept that view, but
defining modules as the unit of encapsulation cleans up a lot of
the mess -- at the cost of having to design a sensible module
hierarchy. Encapsulation predates OO and it is not something that
can only be had with OO. D puts encapsulation back where it used
to be and where it works well: decoupled from OO so that the
public interface can be designed independently from inheritance
and polymorphism.
[1] https://www.youtube.com/watch?v=t86v3N4OshQ&t=20m4s
More information about the Digitalmars-d
mailing list