简体   繁体   English

当我们有代码契约/静态分析时,为什么我们需要Option类型?

[英]Why do we need the Option type when we have code contracts/static analysis?

When designing a null safe piece of code, what's the better approach? 在设计零安全代码时,更好的方法是什么?

F# and Scala has Options type that encapsulates null check, but we also have static code analysis tools like code contracts, findbugs. F#和Scala具有封装null检查的Options类型,但我们也有代码契约,findbugs等静态代码分析工具。

To me static analysis seems a little cleaner, so what is the reason for Option/Maybe? 对我来说,静态分析看起来有点清晰,那么Option / Maybe的原因是什么? In particular, what makes it better in preventing NullPointerExceptions/NullReferenceExceptions? 特别是,什么使它更好地防止NullPointerExceptions / NullReferenceExceptions?

Option is used to model the fact that computation maybe return a value. Option用于模拟计算可能返回值的事实。 It doesn't exist merely to encapsulate null check; 仅仅为了封装空检查不存在; many functional programming languages such as SML, Haskell don't have null but Option/Maybe are present as useful tools for modeling problems. 许多函数式编程语言(如SML,Haskell)都没有nullOption/Maybe作为建模问题的有用工具。

To me static analysis seems a little cleaner, so what is the reason for Option/Maybe? 对我来说,静态分析看起来有点清晰,那么Option / Maybe的原因是什么?

In the context of functional programming, using static analysis to check the absence of values is overkill . 在函数式编程的上下文中,使用静态分析来检查缺少值是否过度 Static type checking can do it just fine (with Option ). 静态类型检查可以做得很好(使用Option )。 And the type systems can guarantee absolute correctness while static analysis tools may have false positives. 类型系统可以保证绝对正确,而静态分析工具可能有误报。

Another problem with static analysis tools is high cost . 静态分析工具的另一个问题是成本高 It costs a lot to build them (I don't know any good static analysis tools for F# and Scala) and to use them (software purchase, developer training). 构建它们需要花费很多(我不知道任何适用于F#和Scala的静态分析工具)并使用它们(软件购买,开发人员培训)。 Admittedly, they are powerful and should be used to catch more subtle errors (which can't be caught by static type checkers) such as index out of bounds, integer overflows, etc. 不可否认,它们很强大,应该用于捕获更微妙的错误(静态类型检查器无法捕获),例如索引越界,整数溢出等。

Option is monadic. Option是monadic。 A primary benefit is transparent integration into monadic chains of computation, typically using the for comprehension syntax. 主要好处是透明地集成到monadic计算链中,通常使用for comprehension语法。

Furthermore, I doubt static analysis could even in principle obviate tests for the presence or absence of a value (the Some / None distinction). 此外,我怀疑静态分析甚至可以原则上避免测试是否存在值( Some / None区别)。 Offhand my intuition is that it would be the equivalent of the halting problem. 反正我的直觉是它等同于停止问题。

For one thing, static analysis can only work if the API is annotated or full source/bytecode is available. 首先,静态分析只有在API被注释或完整源/字节码可用时才能工作。

If you have an API but the actual library implementing it will be decided at runtime, static analysis is helpless. 如果你有一个API,但实现它的实际库将在运行时决定,静态分析是无能为力的。

For another thing, static analysis is intrinsically limited. 另一方面,静态分析本质上是有限的。 The limitations of turing completeness apply, which means it can't decide whether something maybe be null or not in all cases. 图灵完整性的局限性适用,这意味着它无法在所有情况下决定某些东西是否为空。

So, these are all limitations of static analysis, not shared by option types, but option types have an additional advantage: they are monads. 因此,这些都是静态分析的所有限制,不是由选项类型共享,但选项类型还有一个额外的优点:它们是monad。 That means you can compose computation with them, while you'd have to resort to repeating yourself if limited to if-checks for nullability. 这意味着您可以使用它们进行计算,而如果仅限于检查可空性,则必须重复自己。

The last statement is probably unclear, but it's the nature of the thing: if you understand how monads are used, you don't need further explanation; 最后的陈述可能不清楚,但这是事情的本质:如果你了解如何使用monad,你不需要进一步的解释; if you do not, then explanations won't help you much. 如果你不这样做,那么解释对你没有多大帮助。 The best way to learn the usage of monads is to use it -- same as everything else in programming, really. 学习monad用法的最好方法是使用它 - 就像编程中的其他所有东西一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM