动态语言在什么情况下合适?

发布于 2024-07-14 19:38:56 字数 233 浏览 9 评论 0原文

我使用过 C#、Java、C 等静态语言,也使用过一些动态语言 Javascript。

与普遍的看法相反,我发现自己用 C# 编写代码比用 Javascript 更快(这可能是因为与 javascript 相比,我在 C# 方面有更多的经验)

所以,我想了解的是,动态语言在哪些地方可以使用是合适的并且比静态语言更受青睐。

动态语言是否可以用于需要维护多年的企业系统,或者主要用于使用和抛出代码?

I have used static languages such as C#, Java, C and some done some work in Javascript which is a dynamic language.

Contrary to the popular belief, I find myself writing code faster in C# than in Javascript (and that could be because I have more experience in C# compared to javascript)

So, what I want to understand is that what are the places where the dynamic language is appropriate and can be favored over the static languages.

Can a dynamic language be used for the Enterprise system which needs to be maintained for years to come or, its mostly used for use and throw codes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

何止钟意 2024-07-21 19:38:56

只要简单性和灵活性比性能和明确性更重要,我就会使用动态语言。 静态类型为编译器提供了大量信息,可用于编写真正快速的汇编代码。 它还使契约变得明确,可能使代码段更容易单独推理。 然而,由于这些合约非常明确,并且通过大量代码产生连锁反应,因此很难更改。 在需要高度灵活性的系统中,这可能会导致创建大量复杂性来解决静态类型的刚性问题。

就我个人而言,我不认为缺乏静态类型检查有什么大不了的。 当然,编译时失败是理想的快速失败。 然而,动态语言范例并不是很糟糕。 当动态语言在运行时发生类型错误时,事情会立即失败并带有明确的错误消息。 您不会只是在某个完全不同的地方遇到奇怪的未定义行为和失败。

另一方面,我发现一个好的模板系统(不是 C++)和静态类型推断绝对是天赐之物。 这使您可以两全其美,因为它基本上是编译时动态类型。 所有内容仍然在编译时进行静态检查。 及早发现错误并生成高效的汇编代码。 尽管如此,您仍保留了不必在设计时明确约定的灵活性。 我的理解是Haskell、OCaml等在这方面做得很好。 如果您对具有更主流外观和感觉的语言感兴趣,可以尝试 D。

I'd use a dynamic language anytime simplicity and flexibility count more than performance and explicitness. Static typing gives the compiler a lot of information that can be used to write really fast assembly code. It also makes contracts explicit, possibly making sections of code easier to reason about in isolation. However, because these contracts are so explicit and have ripple effects through so much code they're somewhat hard to change. In systems that need a high degree of flexibility, this can lead to a lot of complexity being created to get around the rigidity of static typing.

Personally, I don't see the lack of static type checking to be that big a deal. Of course, failing at compile time is the ideal fast failure. However, the dynamic language paradigm isn't really bad. When a type error occurs at runtime in a dynamic language, things fail immediately and with explicit error messages. You don't just get weird undefined behavior and a failure in some completely different place.

On the other hand, I find that a good template system (not C++) and static type inference can be an absolute godsend. This allows you to have the best of both worlds, as it's basically compile time dynamic typing. Everything still gets statically checked at compile time. Errors are caught early and efficient assembly code is generated. Nonetheless, you retain the flexibility of not having to make contracts explicit at design time. My understanding is that Haskell, OCaml, etc. do this quite well. If you're interested in a language with a more mainstream look and feel that does this, try D.

半岛未凉 2024-07-21 19:38:56

我认为你不能这样一概而论。 动态/非动态语言都可以在同一领域中使用,该领域几乎由所有内容组成。

I don't think you can generalize like this. Dynamic/non-dynamic languages can both be useful in the same domain, which is comprised of pretty much everything.

调妓 2024-07-21 19:38:56

一种情况显示了 C# 4.0 动态的有用性。 假设您有一个可以包含一拖二控件的控件。 它们都具有 DataSource 属性,但这个公共超类没有。 为了弄清楚这一点,您可以使用动态来检查该属性是否存在于运行时级别。

其他场景涉及运行时基于条件的类修改。 假设您希望函数 A() 位于类 B 中,前提是函数 C() 返回 true。 JavaScript 可以做到,但 C# 做不到。

One sitauation that is showing usefulness of C# 4.0 dynamics. Lets say you have a control that can contain one for two controls. Both od them have DataSource property, but this common superclass don't. In order to make this clear, you can use dynamics to check if the property exists at the runtime level.

Other scenario involves class modification durring runtime based on conditional. Let's say you want function A() to be in class B if only function C() would return true. It can be done in JavaScript, but C# can't do this.

很快妥协 2024-07-21 19:38:56

JavaScript 并不是最好的例子。 您应该看看 Python、Ruby 或 Groovy。

JavaScript is not the best example. You should take a look at Python, Ruby or Groovy.

随梦而飞# 2024-07-21 19:38:56

我想指出,动态语言可以由程序员隐式定义其类型,与静态语言相同。

动态语言的一大好处是您可以编写调用实际不存在的对象和方法的方法存根。 这对于首先设计代码并稍后填写详细信息非常有用。 当你在团队中工作时很好。 不过不用担心,如果您这样做,编译器通常会警告您该方法调用可能不起作用。 不过,编译器会让您编码并运行该代码。

动态语言很强大,尽管这种强大有时会使它们更难调试。

I would point out that dynamic languages can have their types implicitly defined by the programmer, the same as a static language.

One nice thing about dynamic languages is that you can write method stubs that call objects and methods that do not actually exist. This can be nice for designing the code first and filling in the details later. Good when you are working in teams. Don't worry though, if you do this the compiler will usually give you a warning that the method call probably won't work. The compiler will let you code and run that code though.

Dynamic languages are powerful, although the power can make them harder to debug at times.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文