方法到底什么时候会产生副作用?

发布于 2024-07-19 03:18:56 字数 173 浏览 7 评论 0原文

正如我一直理解的那样,对程序状态的任何更改(或与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

葬﹪忆之殇 2024-07-26 03:18:56

你的导师错了。 向 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:

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.

Money Quote #2:

In the presence of side effects, a program's behavior depends on past history; that is, the order of evaluation matters.

Non-NOP Setters always satisfy that criteria.

盛夏已如深秋| 2024-07-26 03:18:56

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).

你与昨日 2024-07-26 03:18:56

首先:我知道与语言无关的标签!“runako”非常正确地回答了这个问题。 但通常你想将你的知识应用到现实世界,所以我想我也很乐意提供一个以更务实的方式解决这个问题的答案。

当处理 C++、C# 或 Java 等现实世界语言时,即使是 nop 函数也会产生实际的副作用,可能导致代码被执行!

想想静态构造函数。 尽管规范并不总是指定类的静态构造函数运行的时间,但大多数时候它将是首次访问类的方法或成员的时间点。

C# 中的示例:

class NotSoObvious
{
    static NotSoObvious()
    {
        CauseSomeSideEffects();
    }

    // calling this can cause the constructor to run first!
    public static void DoNothing()
    {
        return;
    }
}

更重要的是,即使根本没有被调用的方法也可能会导致副作用!
想想反射(程序查询有关其自身结构的信息的能力)。
当一个方法存在但未被调用时,仍然可以通过反射检测到它。

没有调用它的方法肯定会对输出内部方法数量的程序产生副作用!

这一切都可以归结为:如果您想了解某种方法的实际副作用,您首先必须确定您认为什么是“副作用”。

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#:

class NotSoObvious
{
    static NotSoObvious()
    {
        CauseSomeSideEffects();
    }

    // calling this can cause the constructor to run first!
    public static void DoNothing()
    {
        return;
    }
}

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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文