Functional JavaScript Tutorial - #3 Side Effects & Pure Functions

preview_player
Показать описание
A side effect is a change of system state or observable interaction with the outside world that occurs during the calculation of a result. A common side effect is to generate output beyond the return value. IO (input/output) operations such as file reading, database access or HTTP requests are another example of side effects.

Side Effects are the primary source of complexity in software. They are hidden inside the function's body and not captured by its signature. On top of that, side Effects may break compositional nature of functions.

Pure functions produce the same output for the same inputs *and* there are no side-effects. It's a computation that is only influenced by its input and that only influences its output.

Pure Functions are easier to reason about. They don't have side effects. This way, pure functions can only influence the behavior of a program through their output. As the output is deterministic (it can reliably be calculated using only the input values), pure functions will always preserve referential transparency. Additionally, using pure functions may increase program's performance.

Functional programming is a programming paradigm in which you build programs by
composing functions. The computation is then the evaluation of those functions.
Functional programming focuses on the usage of pure functions and on avoiding
shared, mutable state. This programming paradigm is declarative. Functional code
is usually more concise and easier to test.

In this mini series, we will embark on an exciting journey to learn a bit about
functional programming by using JavaScript. Presented concepts will be mostly
universal and applicable to other programming languages. This series is created with beginners and non-programmers in mind; don't worry, we will take it slowly!

#functional #javascript #tutorial
Рекомендации по теме
Комментарии
Автор

That's very good explanation of side effects and pure function. Greetings from India!

subratopatnaik
Автор

Will you be talking about how to handle side effects in a future video? E.g., use Effect functors or with dependency injection? Or if you've already covered the topic, please could you share the relevant link(s)? Thanks!

voidnoire
Автор

But date takes input the elapsed time since 1970 and the same input produces the same output, and doesnt have side effects so its a pure function.

igors
Автор

= new Date() does not produce side effects and is a built-in function. Output changes when input does not, because it's the time, and the computer is otherwise calibrating for it. If that makes it impure for Functional Programming usage, does that reveal a dysfunctional rigidity about morbid adherence to vs organizational modularity of a paradigm concept? Might we consider, for the instance, it is deterministic outside the control of the programmer, because that is time?

LanguageSkillz