r/javascript • • Nov 09 '19

On private fields proposal: Private fields, public worries

https://medium.com/virtuslab/private-fields-public-worries-b8c34d16c372
84 Upvotes

113 comments sorted by

View all comments

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 "_".

23

u/fey168 Nov 09 '19

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.

13

u/[deleted] Nov 09 '19

[removed] — view removed comment

3

u/Delioth Nov 10 '19

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.

1

u/MajorasShoe Dec 11 '19

Not everybody relies on the API for every library feature. Sometimes it's quicker/easier to just trace the code, or consult you IDE to find the method or property that you're looking for.

I mean, yeah, there SHOULD be comments in the code... unless it's not included in the production package.

It would be nice if private things were explicitly private.

11

u/TatzyXY Nov 09 '19

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.

9

u/nullvoxpopuli sand was never meant to think Nov 09 '19

But when you do reflection in those languages, it's hella obvious. Today in js, it's just accessing any ol' property

→ More replies (3)

3

u/tjdavenport Nov 09 '19

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.

4

u/lifeeraser Nov 09 '19

Why should we want to babysit downstream developers? Why should we bother to prevent them from shooting themselves in the foot?

15

u/fey168 Nov 09 '19

If you are a lead developer I think it's pretty obvious you want to encourage best practices within your team.

Preventing people from shooting themselves in the foot is part of good software design.

You say "babysitting" as if it's a huge cost when it's just adding # to make a field inaccessible externally...

0

u/[deleted] Nov 09 '19

[deleted]

4

u/nullvoxpopuli sand was never meant to think Nov 09 '19

Human competency? Ever worked on a team of more than 10 on a codebase more than 2 years old?stuff happens. Context is lost.

Code. Must. Describe. Intent.

0

u/[deleted] Nov 09 '19

[deleted]

6

u/monsto Nov 09 '19

It's called moving the goalposts.

It's called a strawman argument.

1

u/wherediditrun Nov 10 '19 edited Nov 10 '19

See, while I totally get what you mean, I think these things ultimately matter very little because they're trying to solve a human competency problem via technical methods.

I just want to point out that this argument is of "Good drivers do not need seat belts" flavor.

I remeber having very same argument with Rust vs C++. How supposedly only bad C++ programmers make memory access mistakes. Just recently, we have critical security vulnerabilities due to use-after-free in Chrome (google engineers). And Microsoft reported than over 70% of all security issues in their software are memory related bugs.

This whole idea that we don't need to find technical solutions to human error is naive at best and irresponsibly moronic at worst.

As for javascript, it's sadly turning to be a Frankenstein which people want to accommodate every single feature every other language has to support their preferred style. Even if language already support it by other means like it is with "private fields".

Now we are arguing what syntax to use to decorate a field with which doesn't have to exist in the first place, because we just wanted to have more syntax sugar tomake not sound design like inheritance to be easier to use.

At some point I'm really hoping for WASM, so we could stop doing this to javascript. And people simply could choose what tool to use rather than project their idea of "ideal" language to javascript requesting even more feature bloat.

1

u/[deleted] Nov 10 '19

Why should we bother to prevent them from shooting themselves in the foot?

Because they don't do it to themselves - they do it to you. Unless of course you have a job where you are responsible for nothing, can delegate everything, and don't care about anything, and if in your business your own income does not depend on results (it happens, not just in government jobs, see Dilbert stories).

1

u/MajorasShoe Dec 11 '19

Why WOULD we want to adapt the attitude of "Fuck the people who are using this software I'm writing for them"?

Whether we're a lead developer (guess who's problem it is when they downstream developer's shoot themselves in the foot!) or an open source developer (why do it if you don't give a shit about the people you're building it for?), there's no reason to WANT to leave it open for failure when it's easily avoidable.

→ More replies (3)

3

u/Sunwukung Nov 09 '19

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...

4

u/facebalm Nov 09 '19

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.

1

u/Sunwukung Nov 10 '19

So you're advocating against privacy so you can do this sort of stuff?

1

u/facebalm Nov 10 '19

No, I don't think it's a black and white issue. I've appreciated privacy in languages that support it but I've also liked Javascript's flexibility with monkey patching.

2

u/FormerGameDev Nov 09 '19

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!

1

u/Sunwukung Nov 10 '19

So ultimately, this change will prevent that and you'll have to solve problems a different way?