Skip to content

Commit 9a04b8a

Browse files
author
Antoine RICHARD
committed
fix(purity): rework purity principle slide
1 parent 68969e4 commit 9a04b8a

File tree

1 file changed

+35
-14
lines changed

1 file changed

+35
-14
lines changed

docs/markdown/02-nodes-elements-components/22-purity.md

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ All React components **must** act like **pure functions** with respect to their
88

99
# Purity
1010

11-
## Pure function
11+
## Impure function
12+
13+
Do you know why this function is not pure ?
1214

1315
```typescript
14-
function sum(a, b) {
15-
return a + b;
16+
let count = 0;
17+
18+
function incrementCount(obj) {
19+
count++;
20+
obj.value = count;
21+
return count;
1622
}
1723
```
1824

19-
<!-- .element: class="big-code" -->
20-
21-
- always returns the same result for a given set of parameters
22-
- does not modify its parameters
23-
- does not have semantical side effects
24-
2525
##==##
2626

2727
<!-- .slide: class="with-code" -->
@@ -30,15 +30,36 @@ function sum(a, b) {
3030

3131
## Impure function
3232

33+
Do you know why this function is not pure ?
34+
3335
```typescript
34-
const fee = 0.5;
36+
let count = 0;
3537

36-
function withdraw(account, x) {
37-
account.total -= x + fee;
38+
function incrementCount(obj) {
39+
count++;
40+
obj.value = count;
41+
return count;
42+
}
43+
```
44+
45+
- depends on external variable context
46+
- creates side effect (updates external variable and argument)
47+
48+
##==##
49+
50+
<!-- .slide: class="with-code" -->
51+
52+
# Purity
53+
54+
## Pure function
55+
56+
```typescript
57+
function sum(a, b) {
58+
return a + b;
3859
}
3960
```
4061

4162
<!-- .element: class="big-code" -->
4263

43-
- modifies its parameters
44-
- and/or depends on external context
64+
- always returns the same result for a given set of parameters
65+
- does not create side effects

0 commit comments

Comments
 (0)