Why the "private" keyword is the modern day "goto"(davidarno.org) |
Why the "private" keyword is the modern day "goto"(davidarno.org) |
Then there's a bunch of stuff about properties that seems to go around in a circle...
It seems that his rationale for this rant stems from poor API design by others---where class modification through sub-classing is impossible to achieve because of opaque interfaces. This has nothing to do with people using private variables, and all to do with people not designing proper and extensible interfaces.
Firstly, right at the end, he says: "Remember, unless you unlucky enough to be using Java or another archaic language with no proper property support, you can always refactor your class to replace a public field with a protected one with a public accessor property if requirements change". Is Java really considered archaic nowadays? C++ and Java probably make up a fairly large portion of programs written today, and this article doesn't address them. Probably should have mentioned that at the top of the article.
Secondly, his example of why the principle of “open for extension but closed for modification” is a not great. He gives an example of a class with a "div" method that is found to contain a bug. Instead of fixing the bug, a "div2" method is added which fixes that bug.
The problem is, that's exactly the approach I would advocate in plenty of situations. When I was in charge of QA for an embedded system, if he'd have come to me with a bug 1 week before a release, I would never have agreed to fix it. In a big enough system, you can never tell what code relies on bugs to work, and would break if that bug was removed. This is a lesson that was hard to teach most of the programmers, but in the end, whether the system actually works or not matters a lot more than whether the code contains any bugs.
Unlike the blog author, I'm not sure the most important part of it is data hiding.
Alan Kay said in 2003: "OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late binding of all things."
In terms of the last part of that definition (the late binding aspect), I saw Kay give a demo where he described a framework wherein any method can act on any object.
I.e., he rejected the C++ style idea that a method is bound to a specific class, and can only act on that class.
This is an idea from Smalltalk, and you can also do it easily in Lisp.
BTW, if this seems unnatural, think about the Visitor pattern, which allows you to do exactly this, within the framework of C++ style class definitions.
It's not about treating everyone like idiots. It's about responsibly designing for and managing change over time.
And thus you should almost always make fields private, make methods non-virtual, make classes final / sealed, etc., except in those situations where you've carefully considered the ramifications and designed for them.
So, the neat, robust, scalable solution is to subclass his immutable value class to make it mutable and then inject it into code expecting the immutable class and change it behind their backs?
Step away from the compiler...
As I moved away from java and settled into the vibe with python, I've abandoned inheritance. I use simple objects, but tend to orient encapsulation around modules, at to do real encapsulation at this higher level (rather than with classes). It's faster to write, easier to read and much easier to refactor.
I like the Kay quotes that dpapathanasiou raises in another comment here. Are there books or other references that seek to teach "message-oriented programming" with mainstream languages like C or Java?
(On the author's main point I disagree. If you're going to do a lot of inheritance you need private variables, otherwise it's easy for descendants to create hard-to-diagnose bugs.)
Before we go any further, let me make one thing clear: the well thought out use of public properties and hidden fields is good practice as it hides the implementation. What I’m suggesting is that blindly using them in a simple full-mutable record-style (or tuple-style if you prefer) class is bad practice.
"Private" Keyword Considered Harmful
In Java, I use package visibility a lot and since starting to use Ruby in a big way about 5 years ago, I often just make attributes public - no getters/setters. A bit sloppy, but makes code shorter and easier to read. I admit that this makes APIs less stable, etc.
On large or huge projects, I would follow conventional wisdom for Java best practices, but for smaller projects, shorter code and better readability works for me.
I'm sure he was the kid on the playground who took his ball and left if someone didn't play by his demanding rules.
"The 'goto' statement took a long time to die, but it has gone from being a fundamental programming concept to a programming pariah used by only the most misguided of developers."
Rather ignorant, and just plain incorrect.