Why explicitly write “private”?

前端 未结 8 1728
孤城傲影
孤城傲影 2020-12-17 08:46

As fields are implicitly private, why there is often explicit declaraion used in the books, articles etc.?

相关标签:
8条回答
  • 2020-12-17 09:20

    In my opinion it makes the code better readable. I don't have to think about the default access modifier. It's also enforced by StyleCop, a tool I use to ensure a consistent coding style.

    0 讨论(0)
  • 2020-12-17 09:22

    You have to remember that code can end up with someone else to reading it at some point, it might be you in 6 months and you need to understand the intent. Declaring something private means that you are not wanting that particular implementation detail to be available to all those who may use it(at this point in time), later revisions may change the way that particular thing works and if you wish to provide backward compatibility, if it's been public from the begining, it needs to remain in future revisions.

    0 讨论(0)
提交回复
热议问题