Skip to content

Commit

Permalink
Add a note on pure functions
Browse files Browse the repository at this point in the history
  • Loading branch information
sapegin committed Jul 5, 2024
1 parent 3c042e6 commit cb3a333
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 2 deletions.
2 changes: 1 addition & 1 deletion manuscript/030_Avoid_conditions.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ There are more cases when a condition is unnecessary:

The `Array.isArray()` method returns `false` for any _falsy_ value, no need to check for it separately.

I> _Falsy value_ is a value that is considered `false` during type conversion to a boolean, and includes `false`, `null`, `undefined`, `0`, `''`, and [a few others](https://developer.mozilla.org/en-US/docs/Glossary/Falsy).
I> A _falsy value_ is a value that is considered `false` during type conversion to a boolean, and includes `false`, `null`, `undefined`, `0`, `''`, and [a few others](https://developer.mozilla.org/en-US/docs/Glossary/Falsy).

And a more complex but great (and real!) example of unnecessary conditions:

Expand Down
2 changes: 2 additions & 0 deletions manuscript/110_Other_techniques.md
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,8 @@ I> We talk more about separation of logic and presentation in the [Divide and co

To summarize: it’s easier to test small pure functions. I don’t always follow these principles, because I write unit tests only when I need to test some complex logic. In such cases, these principles make testing simpler and test more resilient to further code changes.

I> A _pure function_ is a function that always returns the same value if we pass the same arguments (meaning it doesn’t depend on any non-deterministic values like dates or random numbers, on any non-constant value outside the function, or on the function’s internal state), and doesn’t have side effects (meaning it doesn’t change any external variables, update the database, send network requests, and so on).

Let’s look at an example:

<!--
Expand Down
2 changes: 1 addition & 1 deletion manuscript/120_Linting.md
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ This rule requires us to use type-safe equality operators `===` and `!==` instea

For example, `3 == '3'` is okay and expected but `[1] == true` is not (`[0] == false` though which is inconsistent with how conditions work where `[]`, `[0]`, and `[1]` are all truthy meaning `if ([0])` would take the `if` branch, not the `else` branch.).

I> _Truthy value_ is a value that is considered `true` during type conversion to a boolean, and includes `true`, non-zero numbers (including negative numbers), non-empty strings, arrays and objects (even empty ones), and [a few others](https://developer.mozilla.org/en-US/docs/Glossary/Truthy).
I> A _truthy value_ is a value that is considered `true` during type conversion to a boolean, and includes `true`, non-zero numbers (including negative numbers), non-empty strings, arrays and objects (even empty ones), and [a few others](https://developer.mozilla.org/en-US/docs/Glossary/Truthy).

One common use case for `==` is comparison to `null`:

Expand Down

0 comments on commit cb3a333

Please # to comment.