方法到底什么时候会产生副作用?
正如我一直理解的那样,对程序状态的任何更改(或与 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的导师错了。 向 SO 编辑们道歉,因为他们没有将整篇文章粘贴到这里,这就是维基百科所说的:
http://en.wikipedia.org/wiki/Side_effect_(computer_science)
金钱报价#1:
金钱报价#2:
非 NOP Setter 始终满足该标准。
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)
Money Quote #1:
Money Quote #2:
Non-NOP Setters always satisfy that criteria.
getter 和 setter 只是 get_ 和 set_ 方法的语法糖。 它们绝对会产生副作用(尽管当调用者想要增加计数器或其他东西时开始调整大量字段可能是一个坏主意)。
Getters and setters are just syntactic sugar for get_ and set_ methods. They can absolutely have side effects (though it's probably a bad idea to start tweaking lots of fields when all the caller wanted was to increment a counter or something).
首先:我知道与语言无关的标签!“runako”非常正确地回答了这个问题。 但通常你想将你的知识应用到现实世界,所以我想我也很乐意提供一个以更务实的方式解决这个问题的答案。
当处理 C++、C# 或 Java 等现实世界语言时,即使是 nop 函数也会产生实际的副作用,可能导致代码被执行!
想想静态构造函数。 尽管规范并不总是指定类的静态构造函数运行的时间,但大多数时候它将是首次访问类的方法或成员的时间点。
C# 中的示例:
更重要的是,即使根本没有被调用的方法也可能会导致副作用!
想想反射(程序查询有关其自身结构的信息的能力)。
当一个方法存在但未被调用时,仍然可以通过反射检测到它。
没有调用它的方法肯定会对输出内部方法数量的程序产生副作用!
这一切都可以归结为:如果您想了解某种方法的实际副作用,您首先必须确定您认为什么是“副作用”。
First of all: I am aware of the language agnostic tag! "runako" answered the question quite correctly. But often you want to apply your knowledge to the real world so I think I would be nice to also provide an answer that addresses this problem in a more pragmatic way.
When dealing with real world languages like c++,c# or java then even a nop function has actual side effects which can cause code to be executed!
Just think about static-constructors. Even though the specs don't always specify the time a static constructor is ran for a class, most of the time it will be the point in time where a method or member of the class is first accessed.
Example in C#:
What's more is that even a method that isn't being called at all can cause side effects!
Think of reflection (the ability of a program to query information about its own structure).
When a method is present but not being called it can still be detected by reflection.
A method with no calls to it surely has a side effect on a program that outputs the number of methods inside!
It all boils down to this: If you want to know about the actual side effects of a method, you first have to determine what you even consider to be a "side effect".