New top story on Hacker News: Ask HN: Why do you make class members private?
Ask HN: Why do you make class members private?
16 by robalni | 29 comments on Hacker News.
I know this is something that a lot of people do without thinking about it. So let's think about it. I want to know the main reasons for making variables and functions in a class private. How is it better? What can happen if you don't do it? Here are a few possible reasons that I can think of that someone could have: * You have been taught to do it so you just do it without thinking. * It reduces the number of files you have to search in if you want to find all uses of a member. * The member is hard to understand so you want to discourage people from using it. To clarify, I'm only talking about code in the same project that everyone has access to. I'm not talking about defining an API for other people to use that don't have access to the code, like when you make a library.
16 by robalni | 29 comments on Hacker News.
I know this is something that a lot of people do without thinking about it. So let's think about it. I want to know the main reasons for making variables and functions in a class private. How is it better? What can happen if you don't do it? Here are a few possible reasons that I can think of that someone could have: * You have been taught to do it so you just do it without thinking. * It reduces the number of files you have to search in if you want to find all uses of a member. * The member is hard to understand so you want to discourage people from using it. To clarify, I'm only talking about code in the same project that everyone has access to. I'm not talking about defining an API for other people to use that don't have access to the code, like when you make a library.
No comments