side effect

dreamhunter_lan 2009-05-16 11:39:27
网上有一段是这样的:
A side effect is a result of an operator, expression, statement, or function that persists even after the operator, expression, statement, or function has finished being evaluated.
读得不是很懂,到底side effect是什么东西?大家可以说说吗?
...全文
191 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ForestDB 2009-05-18
  • 打赏
  • 举报
回复
副作用,一句话已经说完了,但是它的作用还在。
比如:
int i = 1;
int a = i++; // 这里可以看成是有副作用的,a已经被赋值为1了,但是i也由于作用还在,被改成了2
// 这个例子并不典型,因为这里的副作用是可以利用的(你的逻辑就是这个目的)
比较常见的还有逻辑短路和?:操作符导致部分语句没有被计算或者执行,也算。
Yuchi1989 2009-05-17
  • 打赏
  • 举报
回复
函数的副作用,表达式的副作用,运算符的副作用
应该是在运行语句时达到的额外效果吧。
nyhenry1 2009-05-17
  • 打赏
  • 举报
回复
Side effect (computer science)
From Wikipedia, the free encyclopedia
Jump to: navigation, search

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. For example, a function might modify a global or a static variable, modify one of its arguments, raise an exception, write data to a display or file, read data, call other side-effecting functions, or launch missiles. In the presence of side effects, a program's behavior depends on past history; that is, the order of evaluation matters. Because understanding an effectful program requires thinking about all possible histories, side effects often make a program harder to understand.

Side effects are essential to enable a program to interact with the outside world (people, filesystems, other computers on networks). But the degree to which side effects are used depends on the programming paradigm. Imperative programming is known for uncontrolled, promiscuous use of side effects. In functional programming, side effects are rarely used. Functional languages such as Standard ML and Scheme do not restrict side effects, but it is customary for programmers to avoid them.[1] The functional language Haskell restricts side effects with a static type system; only a function that produces a result of IO type can have side effects. [2][3]

Assembly-language programmers must be aware of hidden side effects—instructions that modify parts of the processor state which are not mentioned in the instruction's mnemonic. A classic example of a hidden side effect is an arithmetic instruction which explicitly modifies a register (an overt effect) and implicitly modifies condition codes (a hidden side effect). One defect of an instruction set with many hidden side effects is that if many instructions all have side effects on a single piece of state, like condition codes, then the logic required to update that state sequentially may become a performance bottleneck. The problem is particularly acute on processors designed with instruction pipeline (since 1990) or with out-of-order execution. Such a processor may require additional control circuitry to detect hidden side effects and stall the pipeline if the next instruction depends on the results of those effects.

[edit] Referential transparency
Main article: Referential transparency (computer science)

Absence of side effects is necessary but not sufficient for referential transparency. Referential transparency means that an expression (such as a function call) can be replaced with its value; this requires that the expression has no side effects and is pure (always returns the same results on the same input).

[edit] Temporal side effects

Side effects due to the time taken for an operation to execute are usually ignored when discussing side effects and referential transparency. In most programs it is desirable to replace a long operation with an equivalent shorter one e.g. replacing (60 / 3 * 2) with 40. There are some cases, such as with hardware timing or testing, where operations are inserted specifically for their temporal side effects e.g. Sleep(5000) or for(i=0; i < 10000; i++){}. These instructions do not change state other than taking an amount of time to complete.
mu_yang 2009-05-17
  • 打赏
  • 举报
回复
A side effect is a result of an operator, expression, statement, or function that persists even after the operator, expression, statement, or function has finished being evaluated.

我理解的字面意思是
“side effect”是指某个运算符、表达式、语句或函数在求值之后遗存的效果。

比如
int f(int);
……
int i;
……
f(i++);
……
这个函数调用在求值(目的)之后遗存的副效应就是i的改变

f(i+1) 就没有这种副效应
magipan 2009-05-17
  • 打赏
  • 举报
回复
C语言中把很多东西看做表达式,C语句就是形如“表达式;”这样子,每个表达式都有自己的值
像一个赋值语句:a=5;本意是(a=5)这个表达式的值:5,使a的值为5则是这个表达式的副作用
记得哪本书说过来着
光宇广贞 2009-05-16
  • 打赏
  • 举报
回复
通俗一点来说,side effect 就是“吃饱了撑的”。理解一下吧。
lylm 2009-05-16
  • 打赏
  • 举报
回复
边界效应
mu_yang 2009-05-16
  • 打赏
  • 举报
回复
觉得翻成“副效应”更好些
qq675927952 2009-05-16
  • 打赏
  • 举报
回复
应该是副作用吧

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧