![]() |
|
|
| |
|
||||
In computer science, a side-effect is a property of a programming language function that it modifies some state other than its return value. For example, a function might modify a global or "static" variable, modify one of its arguments, write data to a display or file, or read some data from other side-effecting functions. Side-effects often make a program's behavior more difficult to understand. Imperative programming is known for employing side effects to make programs function. Functional programming in turn is known for its minimization of side effects. TerminologyA function that uses side effects is referred to as referentially opaque, and one that doesn't is called referentially transparent. For simplicity's sake, we say that a referentially transparent function is one that, given the same parameters, will always return the same result. Another term for a referentially transparent function is a deterministic function. ExampleAs an example, let's use two functions, one which is referentially opaque, and the other which is referentially transparent: globalValue = 0; integer function rq(integer x) begin globalValue = globalValue + 1; return x + globalValue; end integer function rt(integer x) begin return x + 1; end Now, So, how is this a bad thing? Well let's say we want to do some reasoning about the following chunk of code: integer p = rq(x) + rq(y) * (rq(x) - rq(x)); Now, right off-hand, one would be tempted to simplify this line of code to: integer p = rq(x) + rq(y) * (0) = integer p = rq(x) + 0 = integer p = rq(x); However, this will not work for This however will work for Therefore we can reason about our code which will lead to more robust programs, the possibility of finding bugs that we couldn't hope to find by testing, and even the possibility of seeing opportunities for optimization. |
|
|
|
|
|
|
|
Copyright 2008 WordIQ.com - Privacy Policy
::
Terms of Use
:: Contact Us
:: About Us This article is licensed under the GNU Free Documentation License. It uses material from the Wikipedia article "Side effect (computer science)". |