How FP made Grammarly codebase error-Free & Predictable

A presentation at OdessaJS in July 2019 in by Anton Pets

Slide 1

Slide 1

How FP made Grammarly codebase error-Free & Predictable Anton Pets 20 July 2019

Slide 2

Slide 2

Slide 3

Slide 3

!” Grammarly - a writing assistant that helps make your communication clear and effective, wherever you type. 20M Daily Active Users !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

!9

Slide 10

Slide 10

We believe that static types help a lot when building a complex system !10

Slide 11

Slide 11

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

Slide 12

Slide 12

TypeScript is… !12

Slide 13

Slide 13

TypeScript is… 💚 !13

Slide 14

Slide 14

TypeScript is… Confident refactoring !14

Slide 15

Slide 15

TypeScript is… Interfaces! !15

Slide 16

Slide 16

= ❤ Types + !16

Slide 17

Slide 17

!17

Slide 18

Slide 18

!18

Slide 19

Slide 19

!19

Slide 20

Slide 20

TypeScript FP libs Funfix fp-ts !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

😊 !27

Slide 28

Slide 28

🧪 How to experiment ? !28

Slide 29

Slide 29

!29

Slide 30

Slide 30

!30

Slide 31

Slide 31

!31

Slide 32

Slide 32

!32

Slide 33

Slide 33

Can we 🧪 better ? !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

!39

Slide 40

Slide 40

!40

Slide 41

Slide 41

!41

Slide 42

Slide 42

!42

Slide 43

Slide 43

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

Slide 44

Slide 44

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

Slide 45

Slide 45

!45

Slide 46

Slide 46

⛳ What we achieved !46

Slide 47

Slide 47

⛳ Reliable almost nobody knows about 🧪 !47

Slide 48

Slide 48

⛳ Extensible add ♾ number of 🧪 !48

Slide 49

Slide 49

💼 Case 2/4 !49

Slide 50

Slide 50

!50

Slide 51

Slide 51

!51

Slide 52

Slide 52

!52

Slide 53

Slide 53

!53

Slide 54

Slide 54

!54

Slide 55

Slide 55

🙏 Will not compile !55

Slide 56

Slide 56

🤔 But how to deal with null? !56

Slide 57

Slide 57

!57

Slide 58

Slide 58

🤔 Empty alert? Exception? … !58

Slide 59

Slide 59

Bad !59

  1. Null 2) Undefined

Slide 60

Slide 60

!60

Slide 61

Slide 61

!61

Slide 62

Slide 62

!62

Slide 63

Slide 63

!63

Slide 64

Slide 64

!64

Slide 65

Slide 65

!65

Slide 66

Slide 66

Option ≡ an array with 0…1 elements !66

Slide 67

Slide 67

map filter forEach contains … !67

Slide 68

Slide 68

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

Slide 74

Slide 74

⛳ What we achieved !74

Slide 75

Slide 75

⛳ Reliable empty values are safe !75

Slide 76

Slide 76

⛳ Clean no ifs overload, empty values handled separately !76

Slide 77

Slide 77

💼 Case 3/4 !77

Slide 78

Slide 78

!78

Slide 79

Slide 79

!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

!85

Slide 86

Slide 86

!86

Slide 87

Slide 87

!87

Slide 88

Slide 88

!88

Slide 89

Slide 89

Slide 90

Slide 90

Slide 91

Slide 91

🤔 ” How to handle errors correctly? !91

Slide 92

Slide 92

!92

Slide 93

Slide 93

🧐 !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

⛳ What we achieved !98

Slide 99

Slide 99

⛳ Clean & Confident no defensive programming !99

Slide 100

Slide 100

⛳ Single Responsibility Business logics is separated from error handling !100

Slide 101

Slide 101

💼 Case 4/4 !101

Slide 102

Slide 102

!102

Slide 103

Slide 103

!103

Slide 104

Slide 104

!104 🏋

Slide 105

Slide 105

React lazy loading 👍 !105

Slide 106

Slide 106

🌓 !106

Slide 107

Slide 107

https://dev.to

Slide 108

Slide 108

Slide 109

Slide 109

!109

Slide 110

Slide 110

!110

Slide 111

Slide 111

!111

Slide 112

Slide 112

!112

Slide 113

Slide 113

!113

Slide 114

Slide 114

!114

Slide 115

Slide 115

What we want? When we want it? Good retry function! Now!!!

Slide 116

Slide 116

!116

Slide 117

Slide 117

!117

Slide 118

Slide 118

Slide 119

Slide 119

Slide 120

Slide 120

😱 !120

Slide 121

Slide 121

!121

Slide 122

Slide 122

fp-ts !122

Slide 123

Slide 123

Promise in FP Task !123

Slide 124

Slide 124

Promise in FP Task !124

Slide 125

Slide 125

Promise in FP Task !125

Slide 126

Slide 126

Error + Result container Either<Error, Result> !126

Slide 127

Slide 127

Error + Result container Either<Error, Result> !127

Slide 128

Slide 128

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

Slide 129

Slide 129

Task returning Either Task<Either<Error, Result>> alias: TaskEither<Error, Result> !129

Slide 130

Slide 130

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

Slide 131

Slide 131

!131

Slide 132

Slide 132

✅ !132

Slide 133

Slide 133

☑ ✅ !133

Slide 134

Slide 134

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

Slide 135

Slide 135

!135

Slide 136

Slide 136

!136

Slide 137

Slide 137

!137

Slide 138

Slide 138

You can’t just stop speaking about FP

Slide 139

Slide 139

!139

Slide 140

Slide 140

Slide 141

Slide 141

!141

Slide 142

Slide 142

!142

Slide 143

Slide 143

!143

Slide 144

Slide 144

!144

Slide 145

Slide 145

✅ !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

Slide 161

Slide 161

Slide 162

Slide 162

Slide 163

Slide 163

Сode examples: done !163

Slide 164

Slide 164

⛳ What we achieved !164

Slide 165

Slide 165

⛳ Flexible retry Combine different retry policies !165

Slide 166

Slide 166

⛳ Predictable Know your errors in advance !166

Slide 167

Slide 167

⛳ Isolated side effects Combine complex tasks without execution !167

Slide 168

Slide 168

Problem - Solution Empty values ➡ !168 Option

Slide 169

Slide 169

Problem - Solution Empty values ➡ Option Exceptions ➡ Try !169

Slide 170

Slide 170

Problem - Solution Empty values ➡ Option Exceptions ➡ Try Async side effects ➡ Task / TaskEither !170

Slide 171

Slide 171

Remember?

Slide 172

Slide 172

Option Try Task / TaskEither !172

Slide 173

Slide 173

One last thing FP is about composition: Function composition Monadic composition !173

Slide 174

Slide 174

Conclusions !174

Slide 175

Slide 175

One last last thing Composing Software: The Book E. Elliot Functional Programming in Scala P. Chiusano, R. Bjarnason www.grammarly.com/jobs You know what to do !175