如果没有 NULL 我们会做什么?

发布于 2024-09-12 05:16:59 字数 285 浏览 7 评论 0原文

我曾经读到过,拥有可空类型绝对是一种邪恶。我相信它是由创建它们的人写的一篇文章(在 Ada 中?)我相信 这是这篇文章

无论如何,如果像 C# 这样的语言默认使用不可空类型怎么办?如何替换 C# 或 Ruby 或任何其他通用语言中 null 为可接受值的常见习惯用法?

I once read that having nullable types is an absolute evil. I believe it was in an article written by the very person who created them(in Ada?) I believe this is the article

Anyway, so what if by default a language like C# used non-nullable types? How would you replace some of the common idioms in C# or Ruby or any other common language where null is an acceptable value?

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

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

发布评论

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

评论(11

反话 2024-09-19 05:16:59

我不会直接声明可空类型是邪恶的,而是假设:大多数语言将可空性移植到整个类型上,而这两个概念实际上应该是正交的

例如,所有非原始 Java 类型(以及所有 C# 引用类型)都是可以为 null 的。为什么?我们可以回去&继续,但最终我敢打赌答案归结为“这很容易”。 Java 语言本身并不需要广泛的可为空性。 C++ 参考文献提供了如何在编译器级别消除空值的一个很好的示例。当然,C++ 有很多丑陋的语法,Java 明确试图减少这些语法,因此一些好的功能最终与坏的功能一起被淘汰。

C# 2.0 中的可空值类型朝着正确的方向迈出了一步——将可空性与不相关的类型语义或更糟糕的 CLR 实现细节解耦——但它仍然缺少一种对引用类型执行相反操作的方法。 (代码契约很棒,但它们并没有像我们在这里讨论的那样嵌入到类型系统中。)

大量函数式语言或其他晦涩的语言从一开始就“直接”获得了这些概念......但是如果它们如果被广泛使用,我们就不会进行这样的讨论......

回答你的问题:在现代语言中全面禁止 null 与所谓的“十亿美元错误”一样愚蠢。有一些有效的编程结构,其中空值是很好的:可选参数、任何类型的默认/后备计算(其中合并运算符导致简洁的代码)、与关系数据库的交互等。强迫自己使用哨兵值、NaN 等将是比疾病本身更糟糕的“治愈方法”。

也就是说,我暂时同意引文中表达的观点,只要我可以根据我自己的经验进行详细阐述:

  1. 需要空值的情况数量比大多数人想象的要
  2. 少如果您将 null 引入到库或代码路径中,那么删除它们比添加它们要困难得多。 (所以不要让初级程序员随心所欲地这样做!)
  3. 可为 null 的 bug 会随着变量生命周期的
  4. 变化而扩展,与 #3 相关:早期崩溃

Instead of outright declaring that nullable types are evil, I would posit: most languages graft nullability onto entire kinds of types, when the two concepts should really be orthogonal.

For example, all non-primitive Java types (and all C# reference types) are nullable. Why? We can go back & forth, but ultimately I'll bet the answer comes down to "it was easy". There's nothing intrinsic to the Java language that demands widespread nullability. C++ references offered a fine example of how to exorcise nulls at the compiler level. Of course, C++ has a lot more ugly syntax that Java was explicitly trying to curtail, so some good features ended up on the cutting floor alongside the bad.

Nullable value types in C# 2.0 offered a step in the right direction -- decoupling nullability from unrelated type semantics, or worse, CLR implementation details -- but it's still missing a way to do the opposite with reference types. (Code contracts are great & all, but they're not embedded in the type system the way we're discussing here.)

Plenty of functional or otherwise obscure languages got these concepts "straight" from the beginning...but if they were in widespread use, we wouldn't be having this discussion...

To answer your question: banning nulls from a modern language, wholesale, would be just as foolish as the so-called "billion dollar mistake." There are valid programming constructs where nulls are nice to have: optional parameters, any sort of default/fallback calculation where the coalesce operator leads to concise code, interaction with relational databases, etc. Forcing yourself to use sentinel values, NaN, etc would be a "cure" far worse than the disease.

That said, I'll tentatively agree with the sentiment expressed in the quote, so long as I may elaborate to fit my own experience:

  1. the # of situations where nulls are desirable is smaller than most people think
  2. once you introduce nulls into a library or codepath, it's much harder to get rid of them than it was to add them. (so don't let junior programmers do it on a whim!)
  3. nullable bugs scale with variable lifetime
  4. correlary to #3: crash early
剩余の解释 2024-09-19 05:16:59

我们会在(非常)少数允许 null 的地方使用 选项类型 value 实际上是可取的,并且我们会减少很多晦涩的错误,因为任何对象引用都将保证指向适当类型的有效实例。

We'd use option types for the (very) few places where allowing a null value is actually desirable, and we'd have a lot less obscure bugs since any object reference would be guaranteed to point to a valid instance of the appropriate type.

酒几许 2024-09-19 05:16:59

Haskell 是一种强大的语言,没有 null 的概念。基本上,每个变量都必须初始化为非空值。如果要表示“可选”变量(该变量可能有值,但也可能没有),可以使用特殊的“Maybe”类型。

在 Haskell 中实现这个系统比在 C# 中更容易,因为数据在 Haskell 中是不可变的,因此稍后填充的空引用实际上没有意义。然而,在 C# 中,链表中的最后一个链接可能有一个指向下一个链接的空指针,该空指针会在列表扩展时填充。我不知道没有空类型的过程语言会是什么样子。

另请注意,上面的许多人似乎建议用特定于类型的逻辑“无”值(999-999-9999、“NULL”等)替换空值。这些值并不能真正解决任何问题,因为人们对空值的问题是它们是一种特殊情况,但人们忘记了为特殊情况编写代码。使用特定于类型的逻辑无值,人们仍然忘记为特殊情况编写代码,但他们避免了捕获此错误的错误,这是一件坏事。

Haskell is a powerful language that doesn't have the concept of nullity. Basically, every variable must be initialized to a non-null value. If you want to represent an "optional" variable (the variable may have a value but it may not), you can use a special "Maybe" type.

It's easier to implement this system in Haskell than C# because data is immutable in Haskell so it doesn't really make sense to have a null reference that you later populate. However, in C#, the last link in a linked list may have a null pointer to the next link, which is populated when the list expands. I don't know what a procedural language without null types would look like.

Also, note that many people above seem to be suggesting replacing nulls with type-specific logical "nothing" values (999-999-9999, "NULL", etc.). These values don't really solve anything because the problem people have with nulls is that they are a special case but people forget to code for the special case. With the type-specific logical nothing values, people STILL forget to code for the special case, yet they avoid errors that catch this mistake, which is a bad thing.

淡看悲欢离合 2024-09-19 05:16:59

我认为您指的是这个演讲:“空引用:价值数十亿美元的错误

I think you are referring to this talk: "Null References: The billion dollar mistake"

彼岸花似海 2024-09-19 05:16:59

您可以采用一个简单的规则:所有变量都初始化(默认情况下,可以覆盖)为由变量的类定义的不可变值。对于标量,这通常是某种形式的零。对于引用,每个类将定义其“空”值是什么,并且将使用指向该值的指针来初始化引用。

这实际上是 NullObject 模式的语言范围实现: http://en.wikipedia.org/维基/Null_Object_pattern
因此,它并没有真正消除空对象,它只是防止它们成为必须如此处理的特殊情况。

You can adopt a simple rule: All variables are initialized (as a default, this can be overridden) to a immutable value, defined by the variable's class. For scalars, this would usually be some form of zero. For references, each class would define what its "null" value is, and references would be initialized with a pointer to this value.

This would be effectively a language-wide implementation of the NullObject pattern: http://en.wikipedia.org/wiki/Null_Object_pattern
So it doesn't really get rid of null objects, it just keeps them from being special cases that must be handled as such.

轻拂→两袖风尘 2024-09-19 05:16:59

Null 不是问题,问题在于这种语言允许您编写访问可能为 null 的值的代码。

如果该语言仅要求首先检查任何指针访问或将其转换为不可为空类型,则 99% 的与 null 相关的错误将会消失。例如在 C++ 中

void fun(foo *f)
{
    f->x;                  // error: possibly null
    if (f)              
    {
        f->x;              // ok
        foo &r = *f;       // ok, convert to non-nullable type
        if (...) f = bar;  // possibly null again
        f->x;              // error
        r.x;               // ok
    }
}

遗憾的是,这不能对大多数语言进行改造,因为它会破坏大量代码,但对于新语言来说是相当合理的。

Null is not the problem, it is the language allowing you to write code that accesses values that can possibly be null.

If the language would simply require any pointer access to be checked or converted to a non-nullable type first, 99% of null related bugs would go away. E.g. in C++

void fun(foo *f)
{
    f->x;                  // error: possibly null
    if (f)              
    {
        f->x;              // ok
        foo &r = *f;       // ok, convert to non-nullable type
        if (...) f = bar;  // possibly null again
        f->x;              // error
        r.x;               // ok
    }
}

Sadly, this can't be retrofitted to most languages, as it would break a lot of code, but would be quite reasonable for a new language.

﹂绝世的画 2024-09-19 05:16:59

Tcl 是一种不仅没有 null 概念的语言,而且 null 概念本身与该语言的核心相矛盾。在 tcl 中我们说:“一切都是字符串”。它真正的意思是 tcl 有严格的值语义(恰好默认为字符串)。

那么tcl程序员用什么来表示“无数据”呢?大多数情况下是空字符串。在某些情况下,空字符串可以表示数据,那么它通常是以下之一:

  1. 无论如何都使用空字符串 - 大多数情况下它对最终用户没有影响。

  2. 使用您知道数据流中不会存在的值 - 例如字符串 "_NULL_" 或数字 9999999 或我最喜欢的 NUL 字节 "\0"

  3. 使用围绕值的数据结构 - 最简单的是列表(其他语言称之为数组)。包含一个元素的列表表示该值存在,零个元素表示 null。

  4. 测试变量是否存在 - [info contains variable_name]

有趣的是,Tcl 并不是唯一具有严格值语义的语言。 C 也有严格的值语义,但值的默认语义恰好是整数而不是字符串。

哦,差点忘了另一个:

一些库使用数字 2 的变体,允许用户指定“无数据”的占位符是什么。基本上它允许您指定默认值(如果不指定默认值通常默认为空字符串)。

Tcl is one language that not only does not have the concept of null but where the concept of null itself is at odds with the core of the language. In tcl we say: 'everything is a string'. What it really means is tcl has a strict value semantics (which just happens to default to strings).

So what do tcl programmers use to represent "no-data"? Mostly it's the empty string. In some cases where the empty string can represent data then its typically one of:

  1. Use empty string anyway - the majority of the time it makes no difference to the end user.

  2. Use a value you know won't exist in the data stream - for example the string "_NULL_" or the number 9999999 or my favourite the NUL byte "\0".

  3. Use a data structure wrapped around the value - the simplest is a list (what other languages call arrays). A list of one element means the value exist, zero element means null.

  4. Test for the existence of the variable - [info exists variable_name].

It is interesting to note that Tcl is not the only language with strict value semantics. C also has strict value semantics but the default semantics of values just happen to be integers rather than strings.

Oh, almost forgot another one:

Some libraries use a variation of number 2 that allows the user to specify what the placeholder for "no data" is. Basically it's allowing you to specify a default value (and if you don't the default value usually defaults to an empty string).

找回味觉 2024-09-19 05:16:59

我们会创建各种奇怪的结构来传达对象“无效”或“不存在”的消息,如其他答案中所示。 null 可以很好地传达一条消息。

就我个人而言,我会编写一些允许我使用 null 的 C# 预处理器。然后,这将映射到某个动态对象,每当对其调用方法时,该对象都会抛出NullReferenceException

早在 1965 年,空引用可能看起来像是一个错误。但如今,有了各种代码分析工具对空引用发出警告,我们就不必太担心了。从编程角度来看,null 是一个非常有价值的关键字。

We'd create all kinds of strange constructs to convey the message of an object 'being invalid' or 'not being there', as seen in the other answers. A message that null can convey very well.

  • The Null Object pattern has its disadvantages, as I explained here.
  • Domain-specific nulls. This forces you to check for magic numbers, which is bad.
  • Collection wrappers, where an empty collection means 'no value'. Nullable wrappers would be better, but that doesn't differ much from checking for null or using the Null Object pattern.

Personally, I would write some C# preprocessor that allows me to use null. This would then map to some dynamic object, which throws a NullReferenceException whenever a method is invoked on it.

Back in 1965, null references may have looked like a mistake. But nowadays, with all kinds of code analysis tools that warn us about null references, we don't have to worry that much. From a programming perspective null is a very valuable keyword.

長街聽風 2024-09-19 05:16:59

实际上,在任何首先允许指针或对象引用的强大编程语言中,都会存在代码能够访问尚未在其上运行任何初始化代码的指针的情况。或许可以保证此类指针将被初始化为某个静态值,但这似乎并不是很有用。如果机器有一种捕获对未初始化变量(无论是指针还是其他变量)的访问的通用方法,那么这比特殊情况的空指针更好,但除此之外,我看到的最大的与空相关的错误发生在允许使用空指针进行算术的实现中。将 5 添加到 (char*)0 不应产生指向地址 5 的字符指针;它应该触发错误(如果适合创建指向绝对地址的指针,则应该有其他一些方法来执行此操作)。

Realistically speaking, in any powerful programming language that allows pointers or object references in the first place, there are going to be situations where code will be able to access pointers which have not had any initialization code run upon them. It may be possible to guarantee that such pointers will be initialized to some static value, but that doesn't seem terribly useful. If a machine has a general means of trapping accesses to uninitialized variables (be they pointers or something else), that's better than special-casing null pointers, but otherwise the biggest null-related mistakes I see occur in implementations that allow arithmetic with null pointers. Adding 5 to a (char*)0 shouldn't yield a character pointer to address 5; it should trigger an error (if it's appropriate to create pointers to absolute addresses, there should be some other means of doing it).

遮云壑 2024-09-19 05:16:59

如果没有 NULL 我们会做什么? 发明它!:-) 如果您正在寻找一个带内指针值来表示实际上不是指针,那么您不必是火箭科学家也可以使用 0。

What would we do without NULL? Invent it! :-) You don't have to be a rocket scientist to use 0 if you are looking for an inband pointer value to express actually not a pointer.

帅冕 2024-09-19 05:16:59

我们使用任一

  1. 鉴别器。表示某个值为“null”并且必须被忽略的额外属性、标志或指示符。

  2. 特定于域的空值。允许域内的特定值,被解释为“忽略此值”。例如,社会安全号码 999-99-9999 可能是域特定的空值,表示 SSN 未知或不适用。

We use either

  1. Discriminators. An extra attribute or flag or indicator that says that a value is "null" and must be ignored.

  2. Domain-Specific Nulls. A specific value -- within the allowed domain -- that is interpreted as "ignore this value". For example, a social security number of 999-99-9999 could be a domain-specific null value that says the SSN is either unknown or not applicable.

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