~ Office Supplies ~~ Buy Posters ~~ A-Z Products ~~ Website Advertising


Side-effect (computer science) - Wikipedia

<<Up     Contents

Side-effect (computer science)

Redirected from Side effect (computer science)

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 effect to make programs funciton. Functional programming in turn is known for it is without side effect.

A function that uses side-effects is referred to as a referentially opaque function, 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.

As 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, rt is referentially transparent, which means that rt(x) = rt(x) as long as x is the same value. For instance, rt(6) = rt(6) = 7, rt(4) = rt(3+1) = 5, and so on. However, we can't say any such thing for rq because it uses a global value which it modifies.

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

However, this will not work for rq because rq(x) <> rq(x)! Remember, that the return value of rq is based on a global value which isn't passed in and which gets modified all over the place. This goes against common sense since anything minus itself should be 0.

This however will work for rt, because it is a referentially transparent function. 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.

wikipedia.org dumped 2003-03-17 with terodump




 
 
35 ct Very pink red gemmy RHODOCHROSITE Gorgeous gemstone freeform Single gem piece Very nice PRETTY
 35 ct Very pink red my RHODOCHROSITE Gorgeous freeform Single piece Very nice PRETTY 
 
17 grams light green new jade Serpentine gem stone Tumble polished cab cabbing rough 89 carat Nice
 17 grams light green new jade Serpentine Tumble polished cab cabbing 89 carat Nice 
 
78 carats CHRYSOBERYL gems stones Facet uncut raw rough gemstones crystals lot 4 to 5 ct 15 grams gr
 78 carats CHRYSOBERYL uncut raw crystals lot 4 to 5 ct 15 grams gr 
 
11 carats pink Rhodonite gem Polished rectangle blocks Cabbing cab cabochon rough gemstone freeforms
 11 carats pink Rhodonite Polished rectangle blocks Cabbing cab cabochon freeforms 
 
10 gram pink KUNZITE crystal specimen gem stone Cab cabbing cabochon rough uncut gemstone 51 carat 4
 10 gram pink KUNZITE crystal specimen Cab cabbing cabochon uncut 51 carat 4