4 ms·
Why are private methods good for documentation? They shouldn't even be documented save for inline comments. The whole idea of privacy in my mind is that the onl
by DavidMcLaughlin 17y ago
Why are private methods good for documentation? They shouldn't even be documented save for inline comments. The whole idea of privacy in my mind is that the only person or people who should know that these functions even exist are those who maintain the class/library/framework. This helps immensely because when you want to refactor you know EXACTLY which method signatures or attributes have to be supported for backward compatibility. You can really keep on top of things, and as long as you took the time to create a flexible API, you can handle change gracefully. I don't even unit test my private methods because I don't even care about them or what they do. I only care that some data is passed to a public API and returns the correct result. They are there, as you say, purely as a means to organise my code.
For example, in my last company I wrote this ORM in Moose. I released it. And the guys I was working with just didn't get the concept of Moose. They kept accessing the attributes from the underlying blessed hash like they did before rather than the accessors. It still worked because internally Moose stores stuff the way Conway recommended in OOP Perl (which was a weird decision imho). One day I renamed one of my database columns and so had to rename my accessor and created a manual function that warned of "obselete call to accessor x" whilst returning the new column name... backwards compatibility, yay! But they didn't see this because they didn't use the accessor and now all their manual hash accessing was returning undefined or null or whatever it is.
Of course it only took me going over to their table and explaining to them how Moose worked or getting together for a meeting. Fine. But that was a company with a technical team of around 10 people. Now I work for a big tech company, and I'm in charge of a framework again except now its JavaScript which has the same problems when people force this classical inheritance pattern and now when some guy doesn't get it he's in another building or another country and did so in a repository I didn't even know existed and his code that breaks my encapsulation is in a production device and when I refactor my code I have the possibility that I break live code on some project or some device I don't know about.
So yeah, privacy is an important concept and if you don't think so I would say it is you who lives in a superficial bubble where everyone who uses your API or works on your code can be trusted to have good sense.
- jrockway 17y agoNo programming language will solve the problem of bad programmers. If you can edit the code, private methods provide no protection -- the programmer that wants to get at that functionality will just change it to public and recompile, or worse, cut-n-paste the code into his own module. The enforced privacy, therefore, adds no safety. Not enforcing privacy at least allows you to write a subclass that carefully changes the behavior of the original, at the risk of coupling the subclass to the superclass (but this is better than cut-n-pasting). In an ideal world, you could rewrite every library that was designed incorrectly, but there is not always time for this. Intentionally limiting flexibility ("private") is worse for code reuse than allowing someone to intentionally change internal details. (Which is what privacy prevents.) A good programmer will use his judgment to determine whether to cut-n-paste, subclass, or rewrite. The programming language should not remove options from his toolbox. Now I work for a big tech company, and I'm in charge of a framework again except now its JavaScript which has the same problems when people force this classical inheritance pattern and now when some guy doesn't get it he's in another building or another country and did so in a repository I didn't even know existed and his code that breaks my encapsulation is in a production device and when I refactor my code I have the possibility that I break live code on some project or some device I don't know about. Well, the tests stop passing. The tests are what ensure your code works, not "private" directives. "private" is just documentation that suggests "you probably don't want to call this directly". "Probably." All I can say is that technical solutions to social problems never work. Rewrite your app in Haskell and I guarantee that dumbass developers will still be able to mess up your code. People are much more clever than programming languages.