何时使用脚本语言?
什么时候应该使用脚本语言而不是更冗长的编译语言(如 C++)。 C# 还是 Java?
为了让问题变得更有趣,让我们回答这样的问题:
当... BLANK...时应该使用脚本语言
当您需要 A 使用脚本语言 X 时。
当你需要B时使用脚本语言Y。
当你需要C时使用脚本语言Z。
When should one use a scripting language over a more verbose, compiled language like C++. C# or Java?
And to make the question a little more interesting, let's answer the questions like this:
You should use a scripting language when... BLANK...
When you need A use scripting language X.
When you need B use scripting language Y.
When you need C use scripting language Z.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
当开发速度比执行速度更重要时,您应该使用脚本语言。
You should use a scripting language when speed of development is more important than speed of execution.
添加脚本语言作为大型编译系统的组件是一种常见模式:系统的编译部分针对执行速度进行了优化,但更难修改(因为必须编译并重新加载它) ),虽然系统的脚本部分针对灵活性进行了优化,但运行速度会较慢,但任何使用文本编辑器的人都可以快速轻松地进行修改。
如果您想让应用程序的部分功能易于自定义,则可以使用此模式。
Adding a scripting language as a component of a larger compiled system is a common pattern: the compiled part of the system is optimized for speed of execution, but is harder to modify (because it must be compiled and reloaded), whereas the scripted part of the system is optimized for flexibility, it will run slower but can be quickly and easily modified by anyone with a text editor.
You would use this pattern if you wanted to make part of the functionality of your application easy to customize.
这有点宗教问题。 我使用脚本语言来执行临时任务,例如:
所有这些也可以用编译语言来完成,但对我来说,用脚本语言来完成它们会更快更容易(我使用 ruby,但 PERL 和 python 就可以了)。
This is slightly a religious issue. I use a scripting language for ad-hoc tasks, such as:
All of these can also be done in a compiled language, but for me it is faster and easier to do them in a scripting language (I use ruby, but PERL and python are just fine).
我倾向于使用脚本语言进行快速原型设计、实验和数据整理。 例如,如果我有一堆文本文件,需要作为一次性操作进行预处理并构建到数据库装置中,我通常会编写脚本。
然而,如今,随着 groovy 和 ruby 等语言的出现,“脚本”和“编译”之间的界限变得越来越模糊。 对于上面提到的任务,我将使用 ruby,但随后我还将使用它来构建带有 Rails 的生产 Web 应用程序。 我将用 Java 编写桌面应用程序,但 groovy 也允许我混合脚本。 即使在使用 C/C++ 编写时,我发现一个有用的模式是嵌入特定于域的脚本语言(例如 tcl,尽管我不太喜欢该语言)。
我认为语言的实际选择是一种宗教选择,尽管有一些明显的权衡(例如可读性 - Perl 很有用,但太容易编写神秘的脚本。在某些方面,它是“只写”语言 :-)。 过去我使用过 bash+awk+sed、一些 perl、ruby 等等...对于一次性任务,这主要取决于您和团队的其他成员是否熟悉。 这些天我有意识地选择使用 ruby,即使我在 bash/awk/sed 中做同样的事情会更快一些,但这只是为了通过在其中执行尽可能多的任务来提高我的 ruby 技能。
I tend to use scripting languages for rapid prototyping, experimentation, and data wrangling. For example if I have a bunch of text files that I need to preprocess and build into database fixtures as a one-off operation, I'll generally script that.
However, these days the line between 'scripting' and 'compiled' is becoming blurred, with languages like groovy and ruby. For the task mentioned above, I'll use ruby but then I'll also use it to build a production webapp with rails. I'll write desktop apps in java but groovy allows me to blend in scripts as well. Even when writing in C/C++, I found a useful pattern was to embed a domain specific scripting language (e.g. tcl, though I don't much like that language).
The actual choice of language is I think a religious choice, though there are some tradeoffs that are obvious (readability for example - perl is useful, but is too easy to write cryptic scripts in. In some ways it is a 'write-only' language :-). In the past I've used bash+awk+sed, some perl, ruby, etc etc...For one off tasks it is mainly a matter of what you and the rest of your team are comfortable with. I make a conscious choice to use ruby these days even if I'd be somewhat quicker doing the same thing in bash/awk/sed, but this is just to improve my ruby skills by doing as many tasks as possible in it.
这是我的层次结构:
使用 grep 尝试一下。
如果无法使用 grep 完成此操作,请使用 sed。
如果 sed 不够强大,请使用 awk。
如果 awk 不够强大(或者开始变得非常难看),请使用 C、C++ 或您选择的其他成熟的通用编程语言。
请注意,awk 和 C 之间没有“脚本语言”。这是故意的。
Here's my hierarchy:
Try it using grep.
If you can't do it with grep, use sed.
If sed isn't powerful enough, use awk.
If awk isn't powerful enough (or starts to get really nasty-looking), use C, C++, or the other full-blown general purpose programming language of your choice.
Notice there's no "scripting language" in between awk and C. That's on purpose.
我发现脚本语言显然非常有用,当有一个特定的任务适合该语言的优势时(即使用 Perl 进行字符串处理、使用 ruby 进行 Web 开发等等)。 特别是在 Web 开发中,脚本语言具有更快地向您显示代码更改结果的特性。
在某些情况下,您将编译语言与脚本语言混合使用 - 就像在某些游戏中一样。 当您可以用脚本语言比用编译语言更快、更干净、更简单地表达少量行为时,这样做是很有用的。 例如,C++ 是一种非常具有表现力的语言,但它的开发成本比 lua 等语言要高。
I find scripting languages very useful obviously when there's a specific task that fits the language's strengths (i.e. string processing with perl, web development with ruby, or whatnot). In particular with web-development scripting languages have the property of showing you results of code changes faster.
There are some cases where you mix compiled languages with scripted languages - like in some games. It's useful to do that when you can express a smaller amount of behaviour faster, cleaner and simpler in a scripting language than in the compiled language. C++, for example, is a very expressive language - but it's development costs are higher than, say, lua.
我认为何时使用脚本语言没有一般规则。 脚本语言很容易开发,但有时维护起来很困难,特别是当脚本要在不同平台(操作系统)下运行时。
脚本语言的优点当然是:
缺点是:
I dont't think theres a general rule when to use a scripting language. Scripting languages are easy so develop but sometimes they are hard do maintenance, especially when the script should run under different platforms (operating systems).
The pros of scripting languages are of course:
Contras are:
我使用脚本语言来挖掘数据并将它们作为 cron 作业提供给数据库。 它更容易,因为我可以使用 Windows 调度程序或 UNIX 中的 crontab 轻松设置它们,并且由于 Perl 中的正则表达式等一些关键功能,我发现编写起来更容易。
再加上我可以从 CPAN 获得数以百万计的模块......
I use scripting language to mine data and feed them to database as a cron job. It's easier because i can easily set them using windows scheduler or crontab in unix and i find it easier to write due to some key features like regex in perl.
Plus the millions of modules that are made availble to me from CPAN...
当交互式操作很重要时,请使用脚本语言(例如命令行 shell),或者您要导出功能以供其他用户在简单操作中使用。 (它们不必编译;编写脚本就足够了)
Use a scripting language when interactive operation is important, e.g. a command-line shell, or you are exporting functionality for other users to use in simple operations. (They shouldn't have to compile; scripting is enough)
脚本语言,如果您打算编写一次或保持简单。 一旦您的项目变得更大,强类型语言会更好。 想想看,一个大型项目的初始开发与集成和维护花费了多少时间。 您是否愿意维护大量脚本或强类型语言来强制您的类型行为?
Scripting language if you plan on either writing once, or keeping it simple. A strongly typed language is much better once your project becomes larger. Think about it, how much time is spent on a large project with inititial development versus integration and maintenance. Would you rather have to maintain a large set of scripts or a strongly typed language enforcing that your types behave?