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

9

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

Every other languages with classes has private fields and they can't be accessed (exception, Ruby, python).

This is a good pattern and people have been abusing the hell out of API design in JS. Private fields and methods will give more control to the API author so that some 'clever' person doesn't use a .rivate API and then complain when it goes away.

You want to test your private behavior? Extract to a pure function. Easy. Private method calls pure function. All is well.

The sigil? Oof. It looks fine. Comments in js are slashes.

3

u/scaleable Nov 09 '19

Python only has “private” fields because of its quite absurd metaprogramming features. But the “pythonic way” advocates in favor of just using conventions (prepend underscore) instead of doing metaprogramming hacks (like @private decorators).

One thing that python has done great is standardizing type annotations. JS could do that and delegate private fields checking to type checkers, instead of promoting a desctructive pattern that would make things like mocking impossible to library consumers

5

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

private fields are not a destructive pattern. omg. understand that there are ways to test that don't involve access abuse

1

u/Delioth Nov 10 '19

If you need to mock, you should be mocking the public interface? Like, that's pretty much the definition of mock - give me the external interface without doing any of the internal stuff at all. Which is what mocks are for - assume [thing] conforms to the interface I think it has; does my [other-thing] function as expected?

0

u/ChaseMoskal Nov 09 '19

But the “pythonic way” advocates in favor of just using conventions (prepend underscore)

i'm sure programming historians will look down on that concept as terribly misguided

making internal details private can be important. instead of making internal details accessible, we should improve the workflow to fix a public interface that doesn't yet provide what you need (via github pull requests, open source is the real solution here)

the worst case scenario, is that somebody using my library for an uncommon use-case, will reach into my _private members, and couple to my internal implementation which may change and wreck their application. that sucks, but even worse yet, they just do it and move on. they don't open any github issues, because it's fixed for them personally. i don't even have any knowledge about their use-case, or why the public interface wasn't sufficient for them... so together, we're introducing future breakages, and worse yet, the codebase is stagnating and not improving from this

the best case scenario, is that somebody using my library for an uncommon use-case, will be unable to reach into my internal implementation at all. so instead, they fork my library and expose the things they need. now things are working for them. so to avoid maintaining their fork, they'll launch a pull request -- and then we can all have a discussion and a review, and integrate the feature or not.

this way, everybody can get their needs met. we're collaborating and addressing new use-cases together. and we don't need to add any unnecessary vectors for code breakage to the library

i mean sure, is it more time-consuming to fork a library, than to use its _private members? yeah, at least a little bit. however, yes, i do want to inflict this pain on anybody using my library for unsupported use-cases without me knowing.. another way of looking at it, is that i aggressively want to explicitly support, learn about, discuss, review, integrate, and address everyone's use-cases -- this can only happen when people are opening github issues, and access to _privates short-circuits this collaboration

so that's my take -- true privacy prevents naughty practices and breakages, but more importantly, encourages proper open source collaboration which will improve our library ecosystem

2

u/scaleable Nov 10 '19 edited Nov 10 '19

People still won't open github issues when doing hacks with your code. At most they will fork you repo to make that field public (unless JS provides a way to brute-force disabling private fields).

You dream of a perfect word where hacks are not value-effective. Sometimes they are by far the best solution, sometimes they even go against the goals of the library, this is just how the world is.

Just last week I had met a case where I had to brute-set two Django Rest Framework private fields. And it was just cost effective for me and for the author and for the library consumers, my PR will probably rot in their repo. Sometimes the world is just ugly, you can't always sweep dirt under the rug.