How FP made Grammarly codebase error-Free & Predictable

A presentation at Grammarly Meetup in October 2019 in Kyiv, Ukraine, 02000 by Anton Pets

Slide 1

Slide 1

How FP made Grammarly codebase error-Free & Predictable Anton Pets 1 October 2019

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

Grammarly - a writing assistant that helps make your communication clear and effective, wherever you type. 20M Daily Active Users 4

Slide 5

Slide 5

! Kyiv hiring! ” San Francisco, New York # Vancouver new! 5

Slide 6

Slide 6

6

Slide 7

Slide 7

7

Slide 8

Slide 8

Grammarly Challenges 8

Slide 9

Slide 9

Text is difficult 9

Slide 10

Slide 10

Change is difficult ➡ 10

Slide 11

Slide 11

Our combo Object Oriented Design 11

Slide 12

Slide 12

Our combo Object Oriented Design 12

Slide 13

Slide 13

13

Slide 14

Slide 14

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

Slide 15

Slide 15

TypeScript is… 15

Slide 16

Slide 16

TypeScript is… 💚 16

Slide 17

Slide 17

TypeScript is… Confident refactoring 17

Slide 18

Slide 18

TypeScript is… Interfaces! 18

Slide 19

Slide 19

= ❤ Types + 19

Slide 20

Slide 20

20

Slide 21

Slide 21

21

Slide 22

Slide 22

22

Slide 23

Slide 23

TypeScript FP libs Funfix fp-ts 23

Slide 24

Slide 24

TypeScript FP libs Funfix fp-ts 24

Slide 25

Slide 25

Functional Programming: Real Life Cases 25

Slide 26

Slide 26

💼 Case 1/4 26

Slide 27

Slide 27

27

Slide 28

Slide 28

28

Slide 29

Slide 29

29

Slide 30

Slide 30

😊 30

Slide 31

Slide 31

🧪 How to experiment ? 31

Slide 32

Slide 32

32

Slide 33

Slide 33

33

Slide 34

Slide 34

34

Slide 35

Slide 35

35

Slide 36

Slide 36

Can we 🧪 better ? 36

Slide 37

Slide 37

37

Slide 38

Slide 38

38

Slide 39

Slide 39

39

Slide 40

Slide 40

40

Slide 41

Slide 41

41

Slide 42

Slide 42

42

Slide 43

Slide 43

43

Slide 44

Slide 44

44

Slide 45

Slide 45

45

Slide 46

Slide 46

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

Slide 47

Slide 47

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

Slide 48

Slide 48

48

Slide 49

Slide 49

⛳ What we achieved 49

Slide 50

Slide 50

⛳ Reliable almost nobody knows about 🧪 50

Slide 51

Slide 51

⛳ Extensible add ♾ number of 🧪 51

Slide 52

Slide 52

💼 Case 2/4 52

Slide 53

Slide 53

53

Slide 54

Slide 54

54

Slide 55

Slide 55

55

Slide 56

Slide 56

56

Slide 57

Slide 57

57

Slide 58

Slide 58

🙏 Will not compile 58

Slide 59

Slide 59

🤔 But how to deal with null? 59

Slide 60

Slide 60

60

Slide 61

Slide 61

🤔 Empty alert? Exception? … 61

Slide 62

Slide 62

Bad 62

  1. Null 2) Undefined

Slide 63

Slide 63

63

Slide 64

Slide 64

64

Slide 65

Slide 65

65

Slide 66

Slide 66

66

Slide 67

Slide 67

67

Slide 68

Slide 68

68

Slide 69

Slide 69

Option ≡ an array with 0…1 elements 69

Slide 70

Slide 70

map filter forEach contains … 70

Slide 71

Slide 71

map filter forEach contains … empty getOrElse fold … 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

Slide 77

Slide 77

⛳ What we achieved 77

Slide 78

Slide 78

⛳ Reliable empty values are safe 78

Slide 79

Slide 79

⛳ Clean no ifs overload, empty values handled separately 79

Slide 80

Slide 80

💼 Case 3/4 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

85

Slide 86

Slide 86

86

Slide 87

Slide 87

87

Slide 88

Slide 88

88

Slide 89

Slide 89

89

Slide 90

Slide 90

90

Slide 91

Slide 91

91

Slide 92

Slide 92

Slide 93

Slide 93

Slide 94

Slide 94

🤔 How to handle errors correctly? 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

100

Slide 101

Slide 101

⛳ What we achieved 101

Slide 102

Slide 102

⛳ Clean & Confident no defensive programming 102

Slide 103

Slide 103

⛳ Single Responsibility Business logics is separated from error handling 103

Slide 104

Slide 104

💼 Case 4/4 104

Slide 105

Slide 105

105

Slide 106

Slide 106

106

Slide 107

Slide 107

107 🏋

Slide 108

Slide 108

React lazy loading 👍 108

Slide 109

Slide 109

🌓 109

Slide 110

Slide 110

https://dev.to

Slide 111

Slide 111

Slide 112

Slide 112

112

Slide 113

Slide 113

113

Slide 114

Slide 114

114

Slide 115

Slide 115

115

Slide 116

Slide 116

116

Slide 117

Slide 117

117

Slide 118

Slide 118

🤔 How to build a good retry function?

Slide 119

Slide 119

Slide 120

Slide 120

120

Slide 121

Slide 121

121

Slide 122

Slide 122

122

Slide 123

Slide 123

Slide 124

Slide 124

Slide 125

Slide 125

😱 125

Slide 126

Slide 126

126

Slide 127

Slide 127

fp-ts 127

Slide 128

Slide 128

Promise in FP Task 128

Slide 129

Slide 129

Promise in FP Task 129

Slide 130

Slide 130

Promise in FP Task 130

Slide 131

Slide 131

Error + Result container Either<Error, Result> 131

Slide 132

Slide 132

Error + Result container Either<Error, Result> 132

Slide 133

Slide 133

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

Slide 134

Slide 134

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

Slide 135

Slide 135

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

Slide 136

Slide 136

136

Slide 137

Slide 137

✅ 137

Slide 138

Slide 138

☑ ✅ 138

Slide 139

Slide 139

https://github.com/gcanti/retry-ts by the author of fp-ts 139

Slide 140

Slide 140

140

Slide 141

Slide 141

141

Slide 142

Slide 142

142

Slide 143

Slide 143

You can’t just stop speaking about FP

Slide 144

Slide 144

144

Slide 145

Slide 145

Slide 146

Slide 146

146

Slide 147

Slide 147

147

Slide 148

Slide 148

148

Slide 149

Slide 149

149

Slide 150

Slide 150

✅ 150

Slide 151

Slide 151

☑ ✅ ☑ 151

Slide 152

Slide 152

152

Slide 153

Slide 153

153

Slide 154

Slide 154

154

Slide 155

Slide 155

✅ 155 ✅

Slide 156

Slide 156

☑ ✅ ☑ ☑ 156

Slide 157

Slide 157

157

Slide 158

Slide 158

158

Slide 159

Slide 159

159

Slide 160

Slide 160

160

Slide 161

Slide 161

161

Slide 162

Slide 162

✅ 162 ✅ ✅

Slide 163

Slide 163

☑ ☑ ☑ ☑ ✅ 163

Slide 164

Slide 164

164

Slide 165

Slide 165

Slide 166

Slide 166

Slide 167

Slide 167

Slide 168

Slide 168

Сode examples: done 168

Slide 169

Slide 169

⛳ What we achieved 169

Slide 170

Slide 170

⛳ Flexible retry Combine different retry policies 170

Slide 171

Slide 171

⛳ Predictable Know your errors in advance 171

Slide 172

Slide 172

⛳ Isolated side effects Combine complex tasks without execution 172

Slide 173

Slide 173

Problem - Solution Empty values ➡ 173 Option

Slide 174

Slide 174

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

Slide 175

Slide 175

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

Slide 176

Slide 176

Remember?

Slide 177

Slide 177

Option Try, Either Task, TaskEither 177

Slide 178

Slide 178

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

Slide 179

Slide 179

Conclusions 179

Slide 180

Slide 180

One last last thing Composing Software: The Book E. Elliot Functional Programming in Scala P. Chiusano, R. Bjarnason grammarly.com/jobs 180