返回主题:契约式设计

此卡片不在当前主题的阅读序列中,可返回主题继续阅读。

知识卡片

普通读书笔记卡 · 1971.o.1.c.1.a.1.c

无副作用函数便于安全组合

理解等级:中级

这里的“副作用”指会影响后续操作的系统状态变化,并不只指意外后果。无副作用函数返回计算结果,却不产生可观察的状态修改;在输入及所读状态不变时,重复调用可预期,嵌套组合和测试也更容易。相反,调用链中的状态变化若藏在下层,使用者就得追查实现才能判断结果。函数可以与[[接口名称表达领域意图]]配合,让调用者主要依据接口推理;“同样输入同样结果”仍以所依赖状态稳定为条件。参见[[服务无状态不等于无副作用]]。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • 原书第10章|Side -Effect-Free Functions · 查看原书依据

      Operations that return results without producing side effects are called functions. A function can be called multiple times and return the same value each time. A function can call on other functions without worrying about the depth of nesting. Functions are much easier to test than operations that have side effects. For these reasons, functions lower risk.

    参考来源

    - 位置:《Domain-Driven Design: Tackling Complexity in the Heart of Software》第74章《Side-Effect-Free Functions》(源文件:_epub-src/0321125215_ch10lev1sec2.html;单元01) - 结论依据:原文把副作用限定为影响未来操作的状态变化,定义函数为返回结果而不产生副作用的操作,并将可组合、易测试和降低风险联系起来;关于依赖状态稳定的条件是避免误读“重复同值”的必要限定。 - 原始内容:Operations that return results without producing side effects are called functions. A function can be called multiple times and return the same value each time. A function can call on other functions without worrying about the depth of nesting. Functions are much easier to test than operations that have side effects. For these reasons, functions lower risk.