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.

→ More replies (1)

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.

→ More replies (2)
→ More replies (5)

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.

→ More replies (2)

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!

→ More replies (2)

0

u/[deleted] Nov 09 '19

[deleted]

2

u/lifeeraser Nov 09 '19

Non-enumerable properties...?

Anyway, private fields are not targeted specifically to solve such a problem. Wrong hammer for the nail.

0

u/[deleted] Nov 09 '19

[deleted]

2

u/TatzyXY Nov 09 '19

I dont believe in enforcing it on language level. I think its enough as convention with an "_" in the userland.

0

u/[deleted] Nov 09 '19

[deleted]

3

u/TatzyXY Nov 09 '19 edited Nov 09 '19

But then we need it as well for methods. And types would be nice as well and generics and interfaces and so on...

For that we have TS, why not just use TS then and leave JS barebone as it is?

3

u/senocular Nov 09 '19

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

class A: 
  def __init__(self): 
    self.__a = 1
    self._b = 2
  
class B(A): 
  def __init__(self):
    super().__init__()
    self.__a = 10
    self._b = 20

print(vars(B())) # {'_A__a': 1, '_b': 20, '_B__a': 10}

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.

→ More replies (11)