英文:
Do I have to make variables and methods private if I am the only one who touch the code?
问题
我已经创建了一个应用程序,它运行得相当不错,我是唯一为该应用程序编写代码的人,大多数变量和方法都不是私有的。即使它们没有在其他类或活动中使用,我是否应该将它们设为私有?正如我所说,该应用程序按预期运行。
英文:
I have created an app and it works pretty well, I'm the only one who write the code for the app, most of the variables and methods are not private. Should I make them private even if they haven't been used in other classes or activities? As I said the app works as expected.
答案1
得分: 1
我尽量限制对类、方法和变量的访问,即使只有我自己编辑代码。我认为这样可以避免错误(例如,意外更改不负责的代码中的数据),而且合理的封装可以更快地发现架构中的错误。过于宽松的访问权限处理在我看来会导致不干净的代码。
英文:
I try to limit access to classes, methods and variables as much as possible, even if I am the only one editing the code. I think that this way you can avoid errors (e.g. accidentally changing data from code that is not responsible for it) and, with reasonable encapsulation, errors in the architecture are noticed more quickly. Too lax handling of access rights leads to unclean code in my eyes.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论