如果我是唯一接触代码的人,我需要将变量和方法设置为私有吗?

huangapple go评论55阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年6月8日 19:02:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76431167.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定