比较静态类型代码与动态类型代码 - 成本和收益
无论其优点如何,Adobe 的 Actionscript 3 都提供了一个独特的机会来探索类型化语言与非类型化语言的后果,因为它几乎是 javascript 的严格超集,具有严格类型声明、声明式转换、等等。
这使得可以比较两种方式编写的相同代码,同时提取基本的语言语法。
这让我想知道是否有任何定量证据表明强类型的真正好处,特别是在编译期间的错误检查方面,在错误率、编程效率和代码量方面; 或者我们的观点完全基于猜测和猜想? 是否有任何其他语言可以两种方式使用(不包括老式的 VB - 无论如何都不是一种备受推崇的语言。)
我在这两种语言上花费了大量时间,但还没有最终决定我更喜欢哪种方式,我不想添加轶事证据 - 我正在寻找客观信息。
Whatever its merits, Adobe's Actionscript 3 presents what may be a unique opportunity to explore the consequences of typed versus untyped languages, because it is pretty much a strict superset of javascript, with all the syntactic benefits/overhead of strict type declarations, declarative casting, etc.
What this makes possible is comparing the same code written both ways, while factoring out essential language syntax.
This causes me to wonder if there is any quantitative evidence about the real benefit of strong typing in particular for error checking during compilation, with respect to error rates, programming productivity, and code volume; or are our perspectives entirely based on speculation and conjecture? Are there any other languages that can be used both ways (not counting old-fashioned VB - not being a highly respected language either way.)
I've spent significant time with both languages, but haven't conclusively decided which way I prefer, and I'd rather not add to the anecdotal evidence - I'm looking for objective information.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是编程领域最伟大的宗教战争之一,也许只有真正的编辑器 EMACS 和邪恶的撒旦 vi 之间的冲突才能超越。
基本上,如果用动态语言编写的程序是正确的,那么它可以转换为静态类型语言并且仍然是正确的,反之亦然。 真正静态类型语言的优点在于,可以在静态类型语言的编译时识别动态语言中运行时出现的错误。
然而,在这些情况下经常被忽视的是,静态类型语言通常具有逃逸舱口(如类型转换),而显然动态语言可以使用类型推断来推断明显动态表达式的类型,并将其视为静态类型。
在幕后真正重要的是程序员。 如果程序员认为程序是正确的,那么无论是静态语言还是动态语言,它都是正确的。 我不知道有任何好的实验证据表明这两种方法在实践中效率更高或更容易出错。
This is one of the great religious wars in programming, exceeded only perhaps by the conflict between the one True Editor EMACS, and the evil spawn of Satan vi.
Basically, if a program written in a dynamic language is correct, then it can be converted to a statically-typed language and still be correct, and vice versa. The advantage of a truly statically-typed langfuage is that bugs which would show up at run time in a dynamic language can be identified at compile time in a statically-typed language.
What often gets neglected in these situations, though, is that statically typed languages generally have escape hatches (like typecasts), and apparently dynamic languages can use type inference to infer, and treat as statically typed, the types of apparently dynamic expression.
What's really important, under the covers, is the programmer. If the programmer thinks the program right, then it'll be right in either a static or a dynamic language. I'm not aware of any good experimental evidence that either one is more productive or more prone to errors in practice.
在我看来,像 C# 这样的强类型语言可以在编译时识别许多被忽视的错误,而松散类型语言不会捕获这些错误,因此会在以后导致运行时错误。
我不认为这只是一种猜测,因为严格的编译可以预见稍后会在运行时导致错误的问题。 这可能会消除您在实现过程中忽略的大多数编码错误,但编译器可以轻松找到并修复这些错误。
In my opinion, strongly typed languages like C# can identify during compile time many overlooked errors that would not be caught in a loose typed language and would, therefore, cause a runtime error later.
I don't thinks this is just an speculation, since strict compilation can anticipate issues that would later cause errors during runtime. This can potentially eliminate most of the kind of coding erros that you overlook during the implementation, but that can be easily found and fixed by the compiler.
如果你像我一样相信,除非你有测试证明它是正确的,否则静态类型的价值对我来说似乎相当有限。 如果您接受并实践这一点,那么 bug 是在编译时还是在测试时识别出来基本上就无关紧要了。
在这一点上,我宁愿使用两种类型的语言中更简洁的一种,根据我的经验,这是动态语言。
另一方面,静态类型只有在您不编写测试时才真正有帮助。 如果是这种情况,静态类型可能不足以确保软件的正常运行。
The value of static typing seems pretty limited to me if you believe, as I do, that you can't say something is true about a piece of software unless you have a test demonstrating it to be the case. If you accept and practice that, then it's largely irrelevant as to whether bugs are identified at compile time or at test time.
At this point, I'd rather have the more succinct of the two types of languages, which in my experience has been dynamic languages.
The flip side, is that static typing only really helps you if you aren't writing tests. If that's the case, static typing probably isn't enough to ensure the proper functioning of your software.
OP 和 felipecsl 对谈论打字规则时使用的术语感到困惑。
静态类型意味着在编译时检查类型(无论编译时在语言中意味着什么)。
动态类型意味着在执行表达式/语句时检查类型。
例如,强类型意味着您不能将指针颠覆(转换)为整数。
弱类型与强类型相反。
据我所知,不存在“严格打字”。
现在让其他人回答实际问题;-)
The OP and felipecsl are confused about the terminology used when talking about typing disciplines.
Static Typing means types are checked during compile-time (whatever compile time means in the language).
Dynamic Typing means types are checked as expressions/statements are being executed.
Strong Typing means that you cannot subvert (cast) a pointer into an integer, for instance.
Weak Typing is the opposite of Strong Typing.
There is no "strict typing", as far as I know.
Now for someone else to answer the actual question ;-)
弱类型需要更多的维护时间。 随着程序变得越来越复杂,由于没有编译器来发现明显的问题,就会出现更多的问题。 不推荐用于大型复杂项目。 我是根据经验说话的。
Weak typed requires more maintenance time. As programs get more complex, more problems arise due to there not being a compiler to pick up obvious problems. Not recommended for big complex projects. I speak from experience.
统计上为 typed lang。 将它们的变量和表达式作为固定类型,由程序员明确声明或推断。 编译器。使用这个。 信息,这里的类型检查将在编译时进行。 虽然动态类型 lang 的值是固定的,但变量和 xx 表达式不是固定的。由于计算操作数时,它很可能返回不同的值,因此在计算操作数后执行类型检查。即计算之后但在运行时执行操作之前
Statistically typd lang. Have their variables and xpressions as fixed types which are explicitly stated by the programmer or infered by. The compiler.using this. Info, type check here will be carried out at compile- time. While dynamically typed lang have their values fixed, but variables and xxpressions arent fixed..due to the facct that when ever an operand is computed,it is likely to return different values, type check is then perfomed after the operand has been computed.i.e after computation but before performing the operation at run-time