Side Effect (computer Science)

Side Effect (computer Science)

In computer science, a function or expression is said to have a side effect if, in addition to returning 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 static variable, modify one of its arguments, raise an exception, write data to a display or file, read data, or call other side-effecting functions. In the presence of side effects, a program's behavior depends on history; that is, the order of evaluation matters. Understanding a program with side effects requires knowledge about the context and its possible histories; and therefore can be hard to read, understand and debug.

Side effects are the most common way 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 its frequent utilization of side effects. In functional programming, side effects are rarely used. Functional languages such as Standard ML, Scheme and Scala do not restrict side effects, but it is customary for programmers to avoid them. The functional language Haskell restricts side effects with a static type system; it uses the concept of monads to do stateful and IO computations.

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 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 pipelining (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.

Read more about Side Effect (computer Science):  Referential Transparency, Temporal Side Effects, Idempotence, Example

Famous quotes containing the words side and/or effect:

    “Telephone poles were matchsticks, put there to be snapped off at a whim. Dogs trotting across the road were suddenly big trucks. Old ladies turned into moving—vans. Everything was too bright, but very funny and made for my delight. And about half a mile from my long liquid breakfast I turned carefully down a side street and parked, and sat beaming happily through the tannic fog for about an hour, remembering how witty we all had been, how handsome and talented ... [ellipsis in original]”
    —M.F.K. Fisher (1908–1992)

    “I have no faith in human perfectability. I think that human exertion will have no appreciable effect upon humanity. Man is now only more active—not more happy—nor more wise, than he was 6000 years ago.”
    —Edgar Allan Poe (1809–1845)