TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Null References: The Billion Dollar Mistake

93 点作者 wheresvic3超过 5 年前

12 条评论

Matthias247超过 5 年前
Out of all possible gotchas in programming languages I still find null pointers the easiest one to discover and fix. You directly see when and where it happens, and the fix is usally straightforward.<p>Compared to that invalid pointers (stale references) are a lot more painful, since programs might continue to work for a while. Managed languages do at least prevent those.<p>Multithreading issues are imho the biggest pain points, since they are introduced so easily and often go unnoticed for a long time. The amount of languages that prevent those is unfortunately not that big (Rust plus pure singlethreaded languages like JS plus pure functional languages).
评论 #22023454 未加载
评论 #22023231 未加载
评论 #22024253 未加载
评论 #22024398 未加载
LorenPechtel超过 5 年前
Count me amongst those who do not think they&#x27;re a mistake. You need to indicate no-data-here in some fashion. If you try to use that no-data in some fashion having your program blow up from a null reference is a feature to me--in the vast majority of cases it&#x27;s better go boom than silently continue doing something wrong. In the few where that&#x27;s not the case you can trap the exception and go on.<p>The real solution is what has been done with C# in recent years--have the compiler track whether a field can contain a null or not and squawk if you try to dereference something you haven&#x27;t checked. That causes it to blow up in the best place--compile time rather than runtime.
评论 #22024197 未加载
评论 #22025886 未加载
评论 #22024191 未加载
评论 #22028364 未加载
x3ro超过 5 年前
This comes up again and again in one form or the other, yet new languages still seem to be making the same mistake. Of all languages I&#x27;ve touched, Rust seems to be the only one that mostly circumvents this problem. Are there other good examples?
评论 #22019854 未加载
评论 #22020010 未加载
评论 #22019959 未加载
评论 #22020021 未加载
评论 #22019841 未加载
评论 #22019807 未加载
评论 #22022633 未加载
评论 #22019777 未加载
评论 #22025247 未加载
评论 #22022967 未加载
评论 #22019831 未加载
seventh-chord超过 5 年前
&quot;Making everything a reference: The Billion Dollar Mistake&quot; is the talk I want to see
评论 #22019776 未加载
评论 #22022986 未加载
评论 #22019783 未加载
评论 #22019880 未加载
decafbad超过 5 年前
C.A.R Hoare couldn&#x27;t foresee consequences 55 years ago. That&#x27;s a small mistake. We should blame language designers who didn&#x27;t bother to handle the problem after it&#x27;s been obvious.
评论 #22025894 未加载
rzwitserloot超过 5 年前
This old chestnut again.<p>There is an inherent problem in designing processes and writing code to capture them: The notion of not-a-value.<p>There are great many ways to solve them. The most common ones are &#x27;null&#x27; and &#x27;Optional[T]&#x27;. Neither just makes the problem magically go away. If a process is designed (or a programmer writes it) thinking that &#x27;ah, well, here, not-a-value cannot happen&#x27;, but it can, then.. you have a bug.<p>Some language features might make it possible to help reduce how often it occurs, but eliminate it? I don&#x27;t think so.<p>Imagine, for example, in an Optional based language, that you just map the optional to a lambda to execute on the optional, and the behaviour of the optional is to then simply silently do nothing if it&#x27;s optional.none. That&#x27;d be a much harder to find bug than a nullpointer error. (errors with stack traces pointing at the problem are obviously vastly superior to mysterious do-nothing behaviour with no logs or traces of any sort!).<p>Some other creative solutions:<p>* [Pony](<a href="https:&#x2F;&#x2F;www.ponylang.io&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.ponylang.io&#x2F;</a>) tries to be very careful about registering when an object is &#x27;valid&#x27; and when it isn&#x27;t, and when you write code, you have to say which state the objects you interact with can be in. This lets you avoid a lot of the issues... but pony is quite experimental.<p>* In java you can annotate any usage of a type with nullity info, and then compiler linter tools will simply tell you that you have failed to take into account a potential null value. You are then free to ignore these warnings if you&#x27;re just writing test code, or know better. Avoids clogging up the works with optional, but as the java ecosystem shows, you can&#x27;t just snap your fingers and make 30 years of massive community effort instantaneously instantly be festooned with &#x27;might-not-hold-a-value&#x27; style information. At least the annotation style gives the hope of being backwards compatible (to be clear, optional, for java? Really bad idea).<p>* in ObjC, if you send a message to a null pointer, it silently does nothing, in contrast to virtually all other languages with null types where attempting to message a null ref causes an error or even a core dump.<p>* Just write better APIs. Have objects that represent blank state (empty strings, empty collections, perhaps dummy streams which provide no bytes &#x2F; elements, etc). For example, in java: Java&#x27;s map (a dictionary implementation) has the `.get(key)` method which returns the value associated with that key, and returns `null` if there is no such value. About 6 years ago another method was added in a backwards compatible fashion (so, all java map implementations got this automatically): `getOrDefault(key, defaultValue)`. This one returns the provided default value if key isn&#x27;t in the map. You&#x27;d think optionals provide a general mechanism for this, but, in scala, you have both: There&#x27;s `someMap get(key)` which returns an optional, so to get the &#x27;give me a default value&#x27; behaviour, that&#x27;d be `someMap.get(key).getOrElse(defaultValue)`, but maps in scala also have the java shortcut: `someMap.getOrElse(key, defaultValue)`. Sufficient thought in your APIs mostly obviates the issues.<p>null is not a milion dollar mistake. It is a solution to an intrinsic problem with advantages and disadvantages over other solutions.
评论 #22019968 未加载
评论 #22019873 未加载
评论 #22019940 未加载
评论 #22019971 未加载
评论 #22020023 未加载
mlangenberg超过 5 年前
I was expecting someone to mention the Crystal programming language.<p>In Crystal, types are non-nilable and null references will be caught at compile time.<p><a href="https:&#x2F;&#x2F;crystal-lang.org&#x2F;2013&#x2F;07&#x2F;13&#x2F;null-pointer-exception.html" rel="nofollow">https:&#x2F;&#x2F;crystal-lang.org&#x2F;2013&#x2F;07&#x2F;13&#x2F;null-pointer-exception.h...</a><p>I certainly recognize that many bugs in Ruby programs announce themselves as `NoMethodError: undefined method &#x27;&#x27; for nil:NilClass`. So to be able to catch that before releasing code is a very welcoming addition in my opinion.
teh_klev超过 5 年前
InfoQ has some gems, but their video content presentation is terrible (tiny box, or full screen):<p><a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=YYkOWzrO3xg" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=YYkOWzrO3xg</a>
RickJWagner超过 5 年前
No comment on the Null References, but I will say I <i>love</i> the time-index provided for the video. I wish every video had these!
olliej超过 5 年前
Null termination is still easily much worse. At least the general case of null dereferences today (less so earlier) is a page fault.
agumonkey超过 5 年前
Should every domain have a Nil element instead ?
评论 #22021273 未加载
评论 #22020039 未加载
microcolonel超过 5 年前
Null references are not a mistake, they make perfect sense. Letting nullable types be dereferenced directly is the mistake.<p>Null references are at the core of a great number of sensible datastructures, and they&#x27;re a natural fit for conventional computers.
评论 #22023484 未加载