一种方法何时确切有副作用? [英] When exactly does a method have side effects?
问题描述
正如我一直了解的那样,对程序状态的任何更改(或与IO有关的任何更改)都是副作用.更改是否发生在调用该方法的对象的全局变量还是私有字段中都没有关系.随之而来的是,所有不返回任何东西的方法要么什么都不做,要么有副作用.
我的困惑来自我们大学的一位讲师(他仍然是学生,因此还不是万能的;))告诉我二传手没有副作用.
As I always understood it, any change to the programs state (or anything to do with IO) is a side effect. It does not matter, whether the change occurs in a global variable or in a private field of the object the method is called on. It follows that all methods which do not return anything either do nothing at all or have a side effect.
My confusion comes from one of our university's instructors (who is still a student and thus not omniscient yet;) ) telling me setters don't have side effects.
推荐答案
您的讲师有误.对于SO编辑者未在此处粘贴整篇文章表示歉意,这是Wikipedia不得不说的:
Your instructor is mistaken. With apologies to the SO editors for not pasting the entire article here, this is what Wikipedia has to say:
http://en.wikipedia.org/wiki/Side_effect_(computer_science)
货币报价1:
在计算机科学中,如果函数或表达式除了产生值之外还修改了某些状态或与调用函数或外界具有可观察的交互作用,则称其具有副作用.
In computer science, a function or expression is said to have a side effect if, in addition to producing a value, it also modifies some state or has an observable interaction with calling functions or the outside world.
货币报价2:
在存在副作用的情况下,程序的行为取决于过去的历史记录;也就是说,评估顺序很重要.
In the presence of side effects, a program's behavior depends on past history; that is, the order of evaluation matters.
非NOP设置者始终满足该条件.
Non-NOP Setters always satisfy that criteria.
这篇关于一种方法何时确切有副作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!