知识卡片
短路求值避免不必要访问
布尔与先算左侧;若为假便不算右侧。布尔或在左侧为真时同理停止。
因此先检验对象非空,再在右侧访问其字段,可避免空值访问。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 第2章 算法入门 · 查看原书依据
Short-circuiting operators allow us to write boolean expressions such as “x ≠ NIL and x.f = y” without worrying about what happens upon evaluating x.f when x is NIL.
参考来源
- 位置:《算法导论(第4版)》第2章《算法入门》(OEBPS/chapter002.xhtml,文本字符[15000,20000))。
- 结论依据:原文给出短路求值定义以及空值字段访问的防护例子。
- 原始内容:Short-circuiting operators allow us to write boolean expressions such as “ x ≠ NIL and x.f = y ” without worrying about what happens upon evaluating x.f when x is NIL .