Ask HN: Why do you make class members private?
11–20 of 117 posts
Re: Ask HN: Why do you make class members private?
#12Variables should not be accessed directly outside of the object (encapsulation principle) and thus should all be private as a general rule. Then, you will probably divide up the code into a number of separate methods for better structured code. All the methods that are purely internal and not part of the public API should be private if only to enforce the public API, this is also the encapsulation principle.
Re: Ask HN: Why do you make class members private?
#13Then one day you decide you want to aggressively refactor that class. Now you can because the interface can stay the same even though you could completely re-do the guts. Better still: all your tests will still work, as will your class documentation.
Re: Ask HN: Why do you make class members private?
#14Re: Ask HN: Why do you make class members private?
#15Re: Ask HN: Why do you make class members private?
#16Re: Ask HN: Why do you make class members private?
#17Variables should not be accessed directly outside of the object (encapsulation principle) and thus should all be private as a general rule. Then, you will probably divide up the code into a number of separate methods for better structured code. All the methods that are purely internal and not part of the public API should be private if only to enforce the public API, this is also the encapsulation principle.
Python would like a word...
I know what _underscoring does in Python and what PEP8 recommends but doing it all the time for everything is so ugly and unnecessary.
Re: Ask HN: Why do you make class members private?
#18Let's say I have a Motorcycle class with 2 Wheel members. What if in the code I assume 2 Wheels when writing the serializer? And then someone comes along, instantiates a Motorcycle and nukes a Wheel? The code will break.
You can make an argument that OOP is not what you want, and instead create something akin to namespaces+data, and that's fine. But OOP's thing is to create a public API, and controlled state behind it.
Re: Ask HN: Why do you make class members private?
#19Re: Ask HN: Why do you make class members private?
#20I used to believe in information hiding, but not anymore.