New top story on Hacker News: Ask HN: Why do you make class members private?

Ask HN: Why do you make class members private?
14 by robalni | 24 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.

July 26, 2022 at 12:23AM robalni 14 https://ift.tt/5eT9Z42 Ask HN: Why do you make class members private? 24 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.

Nhận xét

Bài đăng phổ biến từ blog này