异常处理和封装等功能在作为独立开发者时有什么用途?

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

What purpose do things like exception handling and encapsulation have as a solo developer?

问题

作为一个对C#(以及编程一般)新手的人,我通过尝试小型个人项目来学习它。随着我对C#的学习不断深入,我会遇到一些主题,如封装(据我理解,这是只允许访问所需的内容,并以一种防止他人破坏你的代码的方式进行操作)和异常处理(据我理解,捕获破坏代码的异常,以允许程序继续运行或提供更详细的说明,后者可能涉及手动引发异常)。

我的问题是,除了从一开始学习最佳实践之外,有没有其他原因让一个独自工作的人现在认为这些事情很重要?因为据我理解,它们似乎不影响程序的实际功能。您可以将程序中的所有访问修饰符都更改为public,它仍然会以相同的方式工作吗?此外,如果我是唯一为自己编写代码的人,我不需要更详细的错误消息,也不必担心别人滥用和破坏我的代码。我并不反对从一开始学习这些东西,因为我一直在尝试在我的代码中使用最佳实践,但特别是作为一个独自工作的人,手动引发异常对我来说似乎没有意义。

英文:

As someone who is new to C# (and programming in general), I am learning it by experimenting with small solo projects. As I continue to learn more about C# specifically, I come across topics like encapsulation (to my understanding, the process of only allowing as much access as is needed, and doing so in a way that prevents others from breaking your code) and exception handling (to my understanding, catching code-breaking exceptions to either allow a program to continue running or to provide a more detailed explanation as to what went wrong, the latter of which may involve throwing exceptions manually).

My question is, other than to learn best practices from the beginning, are there any reasons someone working alone would want to consider these things important right now? Because to my understanding, they don't seem to affect the actual functionality of a program. You can change all access modifiers to public in a program and it will still work the same way? Additionally, I don't really need a more detailed error message or have to worry about people misusing and breaking my code if I am the only person I am writing the code for. I am not against learning these things from the beginning as I have been trying to use best practices in my code, but throwing manual exceptions especially doesn't make sense to me as someone working alone.

答案1

得分: 0

这两者都是好的习惯,即使你是单独工作。

将事物设置为privateprotected意味着你将它们封闭在你的函数内部,这样你可以理解这个函数只能在类内部使用。当超出访问修饰符的边界时,它也不会出现在Intellisense中,以防意外滥用它们。

异常处理总体上是有用的,如果你的程序意外崩溃,捕获异常将帮助你找出问题所在,或者在崩溃后保持程序运行。崩溃也可能在发布后发生,在你的调试环境之外。因此,如果这些崩溃被通知或记录下来供其他用户或甚至你自己使用,那将会有所帮助。

话虽如此,只要你预期某个部分不会出现问题,就不需要把所有东西都放在try-catch()中,但考虑在可能不太稳定的部分使用它(比如与外部资源连接或创建和保存文件)。

总的来说,养成这些习惯将使你更适合未来在团队中工作。

英文:

Both are good habits to get into, even if you're working alone.

Setting things private or protected means you'll keep them enclosed within your function, so you can understand the function is only meant to be used within the class. It'll also not show up in Intellisense, when it's outside the boundary of the access modifier, to prevent accidently misusing them.

And Exception Handling overall is useful, if your program crashes unexpectedly, the catch exception will help you to figure out what goes wrong, or can help keeping the program running after the crash.
Crashes are also possible to happen after release, Outside your debugging enviroment. So it'll help if these crashes are notified or logged down for other users, or even for yourself.

That said, you don't need to put everything into a try-catch() as long as you expect that it'll run that section without problem, but consider using it on sections where it could be less stable (like connecting with external sources, or creating and saving files).

And overall, getting into these habits will make you more suitable to work in teams in future.

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

发表评论

匿名网友

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

确定