How FP Made Grammarly’s Codebase Error-Free & Predictable

A presentation at Projector Frontend Lectorium in March 2020 in Kyiv, Ukraine, 02000 by Anton Pets

Slide 1

Slide 1

How FP Made Grammarly’s Codebase Error-Free and Predictable Anton Pets @oopets 7 March 2020

Slide 2

Slide 2

Slide 3

Slide 3

Grammarly - a writing assistant that helps make your communication clear and effective, wherever you type. 3

Slide 4

Slide 4

4

Slide 5

Slide 5

Grammarly Challenges 5

Slide 6

Slide 6

Text is difficult 6

Slide 7

Slide 7

Change is difficult ➡ 7

Slide 8

Slide 8

Our combo Object Oriented Design 8

Slide 9

Slide 9

Our combo Object Oriented Design 9

Slide 10

Slide 10

10

Slide 11

Slide 11

TypeScript is… Args validation - free & out of the box 11

Slide 12

Slide 12

TypeScript is… Args validation - free & out of the box 12

Slide 13

Slide 13

TypeScript is… 13

Slide 14

Slide 14

TypeScript is… 💚 14

Slide 15

Slide 15

TypeScript is… Confident refactoring 15

Slide 16

Slide 16

TypeScript is… Interfaces! 16

Slide 17

Slide 17

TypeScript is… Interfaces! 17

Slide 18

Slide 18

= ❤ Types + 18

Slide 19

Slide 19

19

Slide 20

Slide 20

20

Slide 21

Slide 21

TypeScript FP libs Funfix fp-ts 21

Slide 22

Slide 22

Functional Programming: Real-Life Cases 22

Slide 23

Slide 23

💼 Case 1/4 23

Slide 24

Slide 24

24

Slide 25

Slide 25

25

Slide 26

Slide 26

😊 26

Slide 27

Slide 27

🧪 How to experiment? 27

Slide 28

Slide 28

28

Slide 29

Slide 29

29

Slide 30

Slide 30

30

Slide 31

Slide 31

31

Slide 32

Slide 32

🤔 Can we 🧪 better ? 32

Slide 33

Slide 33

33

Slide 34

Slide 34

34

Slide 35

Slide 35

35

Slide 36

Slide 36

36

Slide 37

Slide 37

37

Slide 38

Slide 38

38

Slide 39

Slide 39

Immutability 39

Slide 40

Slide 40

Pure Functions Deterministic: Same Input ➡ Same output No uncontrolled side effects 40

Slide 41

Slide 41

Pure Functions Deterministic: Same Input ➡ Same output No uncontrolled side effects 41

Slide 42

Slide 42

Pure Functions 42

Slide 43

Slide 43

43

Slide 44

Slide 44

What we achieved ⛳ Reliable Almost nobody knows about 🧪 ⛳ Extensible Add ♾ number of 🧪 44

Slide 45

Slide 45

💼 Case 2/4 45

Slide 46

Slide 46

46

Slide 47

Slide 47

47

Slide 48

Slide 48

48

Slide 49

Slide 49

49

Slide 50

Slide 50

🙏 Will not compile 50

Slide 51

Slide 51

🤔 But how to deal with null? 51

Slide 52

Slide 52

52

Slide 53

Slide 53

🤔 Empty alert? Exception? … 53

Slide 54

Slide 54

  1. Null 2) Undefined Bad 54

Slide 55

Slide 55

55

Slide 56

Slide 56

56

Slide 57

Slide 57

57

Slide 58

Slide 58

58

Slide 59

Slide 59

59

Slide 60

Slide 60

Option ≡ an array with 0…1 elements 60

Slide 61

Slide 61

map filter forEach contains … empty getOrElse fold … 61

Slide 62

Slide 62

62

Slide 63

Slide 63

63

Slide 64

Slide 64

Slide 65

Slide 65

What we achieved ⛳ Reliable Empty values are safe ⛳ Clean No ifs overload, empty values handled separately 65

Slide 66

Slide 66

💼 Case 3/4 66

Slide 67

Slide 67

67

Slide 68

Slide 68

68

Slide 69

Slide 69

69

Slide 70

Slide 70

70

Slide 71

Slide 71

71

Slide 72

Slide 72

72

Slide 73

Slide 73

73

Slide 74

Slide 74

74

Slide 75

Slide 75

75

Slide 76

Slide 76

76

Slide 77

Slide 77

77

Slide 78

Slide 78

Slide 79

Slide 79

🤔 How to handle errors correctly? 79

Slide 80

Slide 80

80

Slide 81

Slide 81

81

Slide 82

Slide 82

82

Slide 83

Slide 83

83

Slide 84

Slide 84

84

Slide 85

Slide 85

What we achieved ⛳ Clean & Confident No defensive programming ⛳ Singe Responsibility Business logics is separated from error handling 85

Slide 86

Slide 86

💼 Case 4/4 86

Slide 87

Slide 87

87

Slide 88

Slide 88

88

Slide 89

Slide 89

🏋 89

Slide 90

Slide 90

React lazy loading 👍 90

Slide 91

Slide 91

🌓 91

Slide 92

Slide 92

https://dev.to

Slide 93

Slide 93

Slide 94

Slide 94

94

Slide 95

Slide 95

95

Slide 96

Slide 96

96

Slide 97

Slide 97

97

Slide 98

Slide 98

98

Slide 99

Slide 99

99

Slide 100

Slide 100

🤔 How to build a good retry function?

Slide 101

Slide 101

Slide 102

Slide 102

102

Slide 103

Slide 103

103

Slide 104

Slide 104

104

Slide 105

Slide 105

Slide 106

Slide 106

Slide 107

Slide 107

107

Slide 108

Slide 108

108

Slide 109

Slide 109

Lets check github.com/gcanti/fp-ts 109

Slide 110

Slide 110

Promise in FP Task 110

Slide 111

Slide 111

Promise in FP Task 111

Slide 112

Slide 112

Promise in FP Task 112

Slide 113

Slide 113

Error + Result container Either<Error, Result> 113

Slide 114

Slide 114

Error + Result container Either<Error, Result> 114

Slide 115

Slide 115

Error + Result container Either<Error, React.Component> where type Error = TimeoutError | 404Error 115

Slide 116

Slide 116

Task returning Either Task<Either<Error, Result>> alias: TaskEither<Error, Result> 116

Slide 117

Slide 117

Task returning Either TaskEither<Error, Result> () => Promise< Either<Error, Result> > 117

Slide 118

Slide 118

✅ 118

Slide 119

Slide 119

☑ ✅ 119

Slide 120

Slide 120

github.com/gcanti/retry-ts by the author of fp-ts 120

Slide 121

Slide 121

121

Slide 122

Slide 122

122

Slide 123

Slide 123

123

Slide 124

Slide 124

You can’t just stop speaking about FP

Slide 125

Slide 125

125

Slide 126

Slide 126

Slide 127

Slide 127

127

Slide 128

Slide 128

128

Slide 129

Slide 129

129

Slide 130

Slide 130

✅ 130

Slide 131

Slide 131

☑ ✅ ☑ 131

Slide 132

Slide 132

132

Slide 133

Slide 133

133

Slide 134

Slide 134

134

Slide 135

Slide 135

✅ 135 ✅

Slide 136

Slide 136

☑ ✅ ☑ ☑ 136

Slide 137

Slide 137

137

Slide 138

Slide 138

138

Slide 139

Slide 139

139

Slide 140

Slide 140

140

Slide 141

Slide 141

☑ ☑ ☑ ☑ ✅ 141

Slide 142

Slide 142

Slide 143

Slide 143

Slide 144

Slide 144

Slide 145

Slide 145

Slide 146

Slide 146

Сode examples: done 146

Slide 147

Slide 147

What we achieved ⛳ Flexible Combine different retry policies ⛳ Predictable Know your errors in advance ⛳ Isolated side effects Combine complex tasks without execution 147

Slide 148

Slide 148

Problem - Solution Empty values ➡ 148 Option

Slide 149

Slide 149

Problem - Solution Empty values ➡ Option Exceptions ➡ Try (or Either) 149

Slide 150

Slide 150

Problem - Solution Empty values ➡ Option Exceptions ➡ Try (or Either) Async side effects ➡ Task / TaskEither 150

Slide 151

Slide 151

Remember?

Slide 152

Slide 152

Option Try, Either Task, TaskEither 152

Slide 153

Slide 153

One last thing FP is about composition: Function composition Monadic composition 153

Slide 154

Slide 154

Conclusions Twitter: @oopets 154

Slide 155

Slide 155

The last thing Composing Software: The Book E. Elliot Functional Programming in Scala P. Chiusano, R. Bjarnason Blog: https://dev.to/gcanti (fp-ts) 155