静态/强类型输入和重构 [英] Static/strong typing and refactoring

查看:97
本文介绍了静态/强类型输入和重构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,静态/强类型编程语言最有价值的地方在于它有助于重构:如果/当您更改任何API时,编译器会告诉您该更改已中断.

It seems to me that the most invaluable thing about a static/strongly-typed programming language is that it helps refactoring: if/when you change any API, then the compiler will tell you what that change has broken.

我可以想象用运行时/弱类型语言编写代码...但是我不能想象没有编译器的帮助就可以进行重构,也不能想象不用重构就可以编写成千上万行代码.

I can imagine writing code in a runtime/weakly-typed language ... but I can't imagine refactoring without the compiler's help, and I can't imagine writing tens of thousands of lines of code without refactoring.

这是真的吗?

推荐答案

我认为您是在检查类型时将其与检查方式进行合并.运行时键入不一定很弱.

I think you're conflating when types are checked with how they're checked. Runtime typing isn't necessarily weak.

静态类型的主要优点正是您所说的:它们是详尽无遗的.您只需通过编译器就可以确定所有调用站点都符合该类型.

The main advantage of static types is exactly what you say: they're exhaustive. You can be confident all call sites conform to the type just by letting the compiler do it's thing.

静态类型的主要限制是它们在可表达的约束方面受到限制.这随语言而变化,大多数语言具有相对简单的类型系统(c,java),而其他语言具有极其强大的类型系统(haskell,cayenne).

The main limitation of static types is that they're limited in the constraints they can express. This varies by language, with most languages having relatively simple type systems (c, java), and others with extremely powerful type systems (haskell, cayenne).

仅凭此限制类型是不够的.例如,在Java中,类型或多或少地限于检查类型名称是否匹配.这意味着必须将要检查的任何约束的含义编码为某种命名方案,因此Java代码共有大量的间接调用和样板. C ++更好一点,因为模板允许更多的表达能力,但是与依赖类型所能做的事情并不相近.我不确定更强大的类型系统的缺点是什么,尽管显然必须有一些或更多的人在工业上使用它们.

Because of this limitation types on their own are not sufficient. For example, in java types are more or less restricted to checking type names match. This means the meaning of any constraint you want checked has to be encoded into a naming scheme of some sort, hence the plethora of indirections and boiler plate common to java code. C++ is a little better in that templates allow a bit more expressiveness, but don't come close to what you can do with dependent types. I'm not sure what the downsides to the more powerful type systems are, though clearly there must be some or more people would be using them in industry.

即使您使用的是静态类型,也可能无法表现出足够的能力来检查您关心的所有内容,因此您也需要编写测试.静态键入是否比模板节省了更多的精力,这是一个长期争论的话题,而且我认为并非所有情况下都有简单的答案.

Even if you're using static typing, chances are it's not expressive enough to check everything you care about, so you'll need to write tests too. Whether static typing saves you more effort than it requires in boilerplate is a debate that's raged for ages and that I don't think has a simple answer for all situations.

关于第二个问题:

如何使用运行时类型的语言安全地重构?

How can we re-factor safely in a runtime typed language?

答案是测试.您的测试必须涵盖所有重要的情况.工具可以帮助您评估测试的详尽程度.覆盖率检查工具可让您知道测试是否覆盖了代码行.测试变异工具(小丑,he变)可以让您知道测试在逻辑上是否不完整.验收测试可让您知道您编写的内容是否符合要求,最后进行回归和性能测试可确保产品的每个新版本都保持最新版本的质量.

The answer is tests. Your tests have to cover all the cases that matter. Tools can help you in gauging how exhaustive your tests are. Coverage checking tools let you know wether lines of code are covered by the tests or not. Test mutation tools (jester, heckle) can let you know if your tests are logically incomplete. Acceptance tests let you know what you've written matches requirements, and lastly regression and performance tests ensure that each new version of the product maintains the quality of the last.

与适当的类型间接调用相比,进行适当的测试的一大好处是调试变得更加简单.运行测试时,您会在测试中得到明确的失败断言,清楚地表达了它们在做什么,而不是混淆编译器错误语句(认为c ++模板错误).

One of the great things about having proper testing in place vs relying on elaborate type indirections is that debugging becomes much simpler. When running the tests you get specific failed assertions within tests that clearly express what they're doing, rather than obtuse compiler error statements (think c++ template errors).

无论使用哪种工具:编写有信心的代码都需要付出努力.最有可能需要编写大量测试.如果错误的罚金很高,例如航空航天或医疗控制软件,则可能需要使用形式化的数学方法来证明软件的行为,这会使开发工作极为昂贵.

No matter what tools you use: writing code you're confident in will require effort. It most likely will require writing a lot of tests. If the penalty for bugs is very high, such as aerospace or medical control software, you may need to use formal mathematical methods to prove the behavior of your software, which makes such development extremely expensive.

这篇关于静态/强类型输入和重构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆