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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Zero-Cost Exceptions and Error Interfaces

36 点作者 rekahrv超过 2 年前

6 条评论

peterkelly超过 2 年前
Exceptions are an anti-pattern. If a function can encounter an error, it should expose this fact through its interface (specifically its return type).<p>For example, Rust has a Result type [1], so this function would return Result&lt;Item, ItemNotFound&gt;. This forces the caller to think about what to do in case of an error and specify what should happen.<p>[1] <a href="https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;result&#x2F;index.html" rel="nofollow">https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;result&#x2F;index.html</a>
评论 #33652127 未加载
评论 #33656445 未加载
评论 #33651988 未加载
评论 #33653715 未加载
评论 #33652307 未加载
评论 #33652560 未加载
评论 #33652134 未加载
评论 #33652177 未加载
评论 #33652523 未加载
评论 #33658061 未加载
lawxls超过 2 年前
Just a small suggestion to consider: If the title was &quot;Zero-Cost Exceptions and Error Interfaces in Python&quot; I would have received it in my personal Hacker News feed in Telegram (<a href="https:&#x2F;&#x2F;github.com&#x2F;lawxls&#x2F;HackerNews-personalized" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;lawxls&#x2F;HackerNews-personalized</a>) as I do have &quot;Python&quot; keyword added. And there&#x27;s also email notification services people use which might have Python as a keyword.<p>Thanks for providing further resources btw, added Sebastian Witowski presentation to &quot;watch later&quot; list :)
评论 #33657178 未加载
Rygian超过 2 年前
How is &quot;dictionary[key] = None&quot; equivalent to dictionary[key] not existing?
评论 #33656160 未加载
评论 #33652138 未加载
评论 #33655030 未加载
randyrand超过 2 年前
As far as perf goes, a callee raising an exception is never the answer.<p>The perf arguments are a sham imo.<p>A callee cannot know how many times it is going to be called, only a caller, or a caller&#x27;s caller, etc, can know that.
BoorishBears超过 2 年前
I like how the closest thing to a &quot;correct&quot; answer gets dismissed almost immediately with fairly weak reasoning.<p>You&#x27;re adding type hints so they&#x27;re not going to be blind sided anyways, just return a union of Item and None. If None is a valid value then use a result type.<p>Exceptions are for exceptional cases isn&#x27;t just a performance thing, it&#x27;s a reasoning thing: You&#x27;re jumping out of the established control flow and there should be a very good reason for that.<p>If you&#x27;re struggling with handling None even when you know it&#x27;s a valid return value, living in a codebase that overuses exceptions is going to be 10x worse.
评论 #33653069 未加载
评论 #33652168 未加载
mypy_abuser超过 2 年前
<p><pre><code> def read(self, id_: str) -&gt; Item | None: ...</code></pre>