in reply to Re^6: Why encapsulation matters
in thread Make everything an object?
you take me to task for disagreeing with an article that you didn't understand.
Of course I understood it. Else why would I have made such efforts to emphasis the following (re-quoted with my original emphasis intact):
If, on the other hand, that function can be implemented as a non-member, non-friend, "free" function (sub) it won't have to change if the data format changes, so doing so increases encapsulation.
No one, neither he not I, are arguing that encapsulation isn't important. It's all about how you achieve it. My point was and is clear. It is not necessary, or desirable, to stuff everything inside the class in order to achieve encapsulation.
His point was that encapsulation can be maintained, and even enhanced, by keeping stuff out of the class. Contrast that approach with your "My argument goes like this: expose nothing" approach?
You also say: He's arguing that "encapsulation is king" and I agree with him ..., but I think we must have read different articles.
There are four tenants of OO, and if any of them are king, it has to be abstraction. The trick to effective use of OO (remember the key word in the titles of his series of books?), is to balance those four tenants.
But it is well documented that over-use of inheritance leads to a big ball of OO spaghetti (to mix a few metaphors).
But using it incorrectly leads to dichotomies that are unresolvable.
Eg. Building a Point3D class on top of a Point2D class doesn't work, because a 3D-point is not a specialisation of a 2D-point. Any given 2D point will map to an infinite number of 3D-points from any given observers point of view. And that same 2D-point can be mapped to any single 3D-point, from an infinite number of planes of observation. However, a 2D-point can always be derived from a 3D-point successfully.
But they nearly always come with an up-front cost in increased development and complexity. Balancing the needs of the (foreseeable) future against the costs of knowable now, is the trickiest decision every architect has to make.
Done right, encapsulation minimises the costs of change by containing them, thus decreasing coupling. Done wrong--eg. stuffing everything into one big class--it increases coupling, increases the length of dependency chains, and so increases the costs of change.
The target of encapsulation, its raison d'etre, is to simplify maintenance. Not to hide everything from everyone.
Files and folders (or directories) are simpler concepts to think about and use than i-nodes, free-space chains and disk-blocks. Arrays are simpler to program than pointers and index register manipulations. CGI forms are easier to use and program than the query strings they hide. Pull-down menus, check-boxes and dialogs are easier to navigate and get right than endless streams of scrolling man pages and huge long strings of nearly meaningless option strings (What do you think the option is to disable spurious warnings of uncheckable memory bounds?).
But even an abstraction comes with a cost. Simplicity for the user is traded against complexity for the implementor. Again, a balance must be sought.
But the other three tenants serve only to service the needs of the abstraction. Once sight of that fact is lost, and they start to becomes ends in their own right, the very purpose of OO (simplification through abstraction) is lost also. Thence comes complexity for only the sake of dogma.
There's a long, wordy, difficult and sometimes very repetitive, but oh so insightful document, Object Orientation Redefined I highly commend to you. It's a difficult but very well worth while read.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^8: Why encapsulation matters
by Ovid (Cardinal) on May 20, 2008 at 06:04 UTC | |
by BrowserUk (Patriarch) on May 20, 2008 at 08:15 UTC | |
by Ovid (Cardinal) on May 20, 2008 at 10:16 UTC |