本文共 3283 字,大约阅读时间需要 10 分钟。
by Preethi Kasireddy
通过Preethi Kasireddy
Note: This is Part 3 of a 3-part series. You can check out and if you haven’t already!
注意:这是3部分系列的第3部分。 您还可以查看和 !
The first programming languages I learned were JavaScript and Python, both of which are dynamically-typed languages.
我学习的第一种编程语言是JavaScript和Python,它们都是动态类型化的语言。
But my foray into static types have added a whole new dimension to how I think about programming. For example, even though I found the Elm compiler errors overwhelming at first, defining types and “pleasing the compiler” became second nature, and actually improved my code thinking. Plus, there’s nothing more liberating than an intelligent robot telling me when I’m doing something wrong and how to fix it.
但是我对静态类型的尝试为我对编程的思考增加了一个全新的维度。 例如,尽管我一开始发现Elm编译器错误不堪重负,但定义类型和“取悦编译器”成为第二天性,实际上改善了我的代码思维。 另外,没有什么比解放智能的机器人告诉我什么时候做错了以及如何解决它要解放了。
Despite the tradeoffs that come with types like verbosity and the upfront investment to master them, the safety and correctness that types add to our programs make these “disadvantages” less of an issue for me personally.
尽管诸如冗长性和要掌握它们的前期投资之类的类型需要权衡取舍,但类型为我们的程序增加的安全性和正确性使这些“缺点”对我个人而言不再是一个问题。
Dynamic typing feels faster and easier, but it sometimes loses ground once you actually try to make a program run in the wild. At the same time, you can talk to any Java developer who’s had to work with more complicated generic type definitions and they’ll tell you all about how much they hate types.
动态类型输入感觉更快,更容易,但是一旦您实际尝试使程序在野外运行,它有时会失去作用。 同时,您可以与必须处理更复杂的泛型类型定义的任何Java开发人员进行交谈,他们会告诉您所有讨厌类型的信息。
Ultimately, there’s no silver bullet. My personal approach is to favor using types under these circumstances:
最终,没有灵丹妙药。 我个人的方法是在以下情况下使用类型:
On the flip side, I’d consider opting out of types in these types of situations:
另一方面,我会考虑在以下几种情况下选择退出类型:
The beauty of being a JavaScript developer today is that because of tools like Flow and TypeScript, we finally have a choice of whether to use static types or good ol’ vanilla JavaScript.
如今,成为JavaScript开发人员的好处在于,由于使用了Flow和TypeScript之类的工具,我们最终可以选择使用静态类型还是使用优秀的普通JavaScript。
I hope this post helped you get a feel for why types matter, how to use them, and most importantly *when* to use them.
我希望这篇文章可以帮助您了解为什么类型重要,如何使用它们,最重要的是*何时*使用它们。
Being able to toggle between dynamic and static types is a powerful tool for the JavaScript community — and exciting :)
能够在动态和静态类型之间切换是JavaScript社区的强大工具-令人兴奋的是:)
More questions? As always, ping me in the comments to keep the conversation going.
还有其他问题吗? 与往常一样,在评论中回复我以保持对话的进行。
翻译自:
转载地址:http://vhewd.baihongyu.com/