What are side effects?

7

I was reading this answer and it has a table that says that functional programming has no side effects and that OOP has, this gave me two doubts, they are:

  • What are these side effects?

  • How can you avoid them in OOP ?

Table:

    
asked by anonymous 18.09.2018 / 14:40

2 answers

6

The side effect is what interferes in a situation beyond what is expected, something happens as a consequence of the main event. In computing this is also, and we use to say that a code affects something that is beyond it. This occurs when something is not self-contained, it lacks encapsulation (in the broad sense of the word, not in what is normally improperly used in OOP). In many cases we speak of them as contractive of "hidden side effect" and generally it has a negative connotation, since it is something that affects the external environment without its intention.

In this context shown in the question it is clear that this means, but with a greater specificity. There it indicates that nonfunctional a code does not change state external to it. Then it can have local states and even change them without problem, it can receive external states, but can not change these received states.

Changing an external state to a function is generating the side effect. And no matter how this sternal state comes, it can be by normal parameter, it can be by implicit parameter (usually this ), or it may be because this state is globally visible, including since it comes from outside the application. OOP pretty much forces you to do this.

But receiving an external data without changing it is normal and does not cause side effects. Changing a local copy of a data is smooth and " pure ". Of course, there is always a local difficulty when changing this state, but this is not considered a side effect because it is self-contained.

Changing status is one of the things that most creates problems in computing, ideally avoiding it as much as possible, or at least limiting it to a very specific scope. Functions that do not change the external state are simpler to write, understand and debug, but of course this is not always possible, and because everything is trade-off, trying to escape from the side effect exaggeratedly complicates even more many cases, so languages that try to be pure (where it gives) are very complicated for most people. On the other hand it strengthens the idea of changing external state, one of the reasons that the paradigm is most criticized. What could often be only external communication without change of state becomes collateral always.

Just remembering that zero side effect is impossible on any non-trivial code. And functional programming in almost every language avoids this purity at any cost, just encourages it to be so.

It is possible to avoid them in object orientation, but in fact it stops being OO at least at that point :) Of course, there are methods that do not change the state of the object it belongs to or external object, so this method has no side effect , but it is not normal in OOP and in general this occurs only by coincidence.

    
18.09.2018 / 15:10
1

The side effect in object orientation

In object orientation, side effect is an unwanted effect that occurs on objects when their state is modified. It can occur in different situations, usually the cause is a confused maintenance of the state of the object, where calling one method ends up influencing the behavior of another.

    
18.09.2018 / 17:15