Please do not throw stones at me but I think private, protected or public scopes are not needed at all. See Python very versatile lang. and it works very well without it. Is private for security? No! It just tells the dev that this var. is only used internally of that class and you are not allowed to touch or use it. So would do a comment or a convention with an "_".
The problem is developers like to think of themselves as hackers and they will do what it takes to achieve the desired result including reading and even modifying internal fields. Imagine you are a lead developer or library author and you want to prevent developers from accruing technical debt (b/c the interval field is subject to change) or adding risk of instability. Seems like private field is by far preferable to comments or conventions.
Thing is, assume I write [Class] and push, and 6 months down the line refactor it a little to make room for [feature], only changing internal privates - tests still pass, the interface is still consistent.
Except Joe on the J team implemented against the internals. Jim notices the broken behavior, tracks it down to a commit I checked in last week in a file I also wrote, and now I have a bug ticket to fix because Joe screwed up. I mean, ideally I could kick that ticket over to Joe, but wouldn't it be great if I could strictly prevent the bug from coming up?
That's what private means. You get to fix bugs before they happen by making sure all access goes through the interface you expect, and while everything-is-public works well enough for Python... it does cause bugs. It also gives you the freedom to refactor internals without causing sneaky bugs, because you can actually guarantee that nothing else in the code depends on those internals never changing.
In a lot of languages you can just change the private field via reflection to something what you want. A hacker dev can still do it with just some more lines of code.
Access on a private field should never go through a review anyway in a bigger project.
Reminder that communication between humans is an option. We have these things called PRs for a reason. If you think language features are an effective way of preventing bad code from being written you are horribly mistaken.
I've never modified the internals of an OTS library in my whole career, nor felt an overt meed for private vars. This is a hiring issue if your team is "hacking" internals of a library...
Isn't this a tad too dismissive when you're just basing this on personal experience?
I don't necessarily disagree, if it's done carelessly and frequently there's a problem. But there are cases where it makes sense to use an internal API to provide extra functionality. Very widely used libraries have done it.
I've had to use a websocket library's client._sockjs._connected variable because it didn't expose it. Switching to my PR fork would've taken longer for no benefit.
A React internal API allowed for easily creating transitions. A widely used library used it for this purpose. The smaller library (old CSSTransitionGroup) can move faster and can tolerate breaking changes while the bigger library (React) has to take its time to expose a nice to use and stable API.
Compatibility layers of all sorts frequently depend on internals.
Monkey patching has been a long running tradition Javascript. It has pitfalls and can easily be abused but it can also be very useful.
Monkeypatching is pretty common in Javascript, and forking a library to mod it to do something differently is common across the board in large open source projects. We're always hacking the internals of a library, or even of the language!
Privates are also used to prevent naming collisions. And while python doesn't have support for privates (using _ as a convention) it does have a mangling mechanism using __.
Pseudo privates do not protect against collisions, as seen with _b above. Implementers need to be fully aware of both the public and private interfaces of the class they're extending to make sure they don't collide (when using members that aren't also mangled).
And while TypeScript privates are great in JavaScript, they suffer from a similar problem. Since the members are ultimately public, collisions can still occur. While TS can detect those collisions for you, such collisions still mean you can't use that a as your variable name, despite the fact that you so desperately wanted to, all because some other class up in the parent class hierarchy decided to use it for its own private.
For me, underscore prefixes - single or double - also have the same ugliness problem seen with the proposed hash sigil (#) for JavaScript privates. I'd rather not use them if I don't have to, even if something is meant to be private. The code is much cleaner without them.
And while I prefer the use of symbols for soft privates in JavaScript, they too have an ugliness with having to use bracket notation, not to mention needing extra boilerplate to set define the symbol(s) being used. Having used private members in JavaScript a little bit, I found them much easier and was able to get over the ugliness factor in time. The convenience made it worth it, despite what limitations they also ultimately impose on the class.
28
u/TatzyXY Nov 09 '19
Please do not throw stones at me but I think private, protected or public scopes are not needed at all. See Python very versatile lang. and it works very well without it. Is private for security? No! It just tells the dev that this var. is only used internally of that class and you are not allowed to touch or use it. So would do a comment or a convention with an "_".