Earlier quoted context omitted.
Python would like a word...
While we're on Python and private methods - what's with everyone just _underscoring every method and variable by default? Does anyone teach people to do this or have I just ran into a few people with this habit by chance? I know what _underscoring does in Python and what PEP8 recommends but doing it all the time for everything is so ugly and unnecessary.
Ask HN: Why do you make class members private?
41–50 of 117 posts
Re: Ask HN: Why do you make class members private?
#42Easy way to avoid this problem: don't use classes.
Re: Ask HN: Why do you make class members private?
#43> You have been taught to do it so you just do it without thinking. Ouch, that's a pretty careless reason to start with. Why does insulting your audience seem like the right first move? In much of the classist (classy? You decide) code I've written, the entire reason that I've chosen to encapsulate the data into a class is that maintaining a consistent data structure requires book-keeping. The fields and methods asso…
That may be so, but it is akin to using an undocumented system call. And good luck if that class ever gets refactored or turned into a service.
Re: Ask HN: Why do you make class members private?
#44To hide implementation details from class users. You ideally only want to have a documented interface and all implementation details should be opaque to class users. This can be quite tricky and usually classes will export some state, typically done through getters and setters. Then one day you decide you want to aggressively refactor that class. Now you can because the interface can stay the same even though you cou…
Admittedly, my opinion is mainly based around working mostly in C++, but every codebase I work in of significant age is a knitted castle of interlocking classes with poorly thought out accessors and private variables. While the vision of carefully encapsulating implementation details is nice, in practice the discipline required to do so seems unachievable.
Instead of using private members, if I want an interface I explicitly make an abstract base class interface and inherit from it.
This has two advantages: (1) unit tests are simpler when I can poke/prod the concrete internals because they're public. (2) I can make an "include" directory with the interfaces in it when I want to distribute a shared library and those interfaces will be more concise and less likely to leak all sorts of implementation details in the headers.
Re: Ask HN: Why do you make class members private?
#45We do it because that's how it's always been done. There is no why of OOP, there is only do.
You really think that in the 50+ years of OOP and modular programming no one has considered why? That they've really just thrown shit at the wall and seen what stuck and never considered what to throw at the wall or why it stuck?
Re: Ask HN: Why do you make class members private?
#46https://en.wikipedia.org/wiki/Encapsulation_(computer_progra...
That's what it's called when you do things like that but what's the reason? From the linked page I read: "Encapsulation is used to hide the values or state of a structured data object inside a class, preventing direct access to them by clients in a way that could expose hidden implementation details or violate state invariance maintained by the methods." So there are two possible reasons here: 1. The access could "ex…
Making the list or the dictionary public opens you up to situations where consumers of the code modify either datastore independently and corrupt the consistency of the class.
Re: Ask HN: Why do you make class members private?
#47> You have been taught to do it so you just do it without thinking. Ouch, that's a pretty careless reason to start with. Why does insulting your audience seem like the right first move? In much of the classist (classy? You decide) code I've written, the entire reason that I've chosen to encapsulate the data into a class is that maintaining a consistent data structure requires book-keeping. The fields and methods asso…
Re: Ask HN: Why do you make class members private?
#48Re: Ask HN: Why do you make class members private?
#49Re: Ask HN: Why do you make class members private?
#50-To hide implementation details from users of the library. We don't want to unnecessarily expose functions -Reduce the amount tests, as the private functions wouldn't have to be explicitly tested
Outside of library usage though? Mostly counterproductive in my experience.