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

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

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

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

6

7

Grammarly Challenges 8

Text is difficult 9

Change is difficult ➡ 10

Our combo Object Oriented Design 11

Our combo Object Oriented Design 12

13

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

TypeScript is… 15

TypeScript is… 💚 16

TypeScript is… Confident refactoring 17

TypeScript is… Interfaces! 18

= ❤ Types + 19

20

21

22

TypeScript FP libs Funfix fp-ts 23

TypeScript FP libs Funfix fp-ts 24

Functional Programming: Real Life Cases 25

💼 Case 1/4 26

27

28

29

😊 30

🧪 How to experiment ? 31

32

33

34

35

Can we 🧪 better ? 36

37

38

39

40

41

42

43

44

45

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

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

48

⛳ What we achieved 49

⛳ Reliable almost nobody knows about 🧪 50

⛳ Extensible add ♾ number of 🧪 51

💼 Case 2/4 52

53

54

55

56

57

🙏 Will not compile 58

🤔 But how to deal with null? 59

60

🤔 Empty alert? Exception? … 61

Bad 62

  1. Null 2) Undefined

63

64

65

66

67

68

Option ≡ an array with 0…1 elements 69

map filter forEach contains … 70

map filter forEach contains … empty getOrElse fold … 71

72

73

74

75

⛳ What we achieved 77

⛳ Reliable empty values are safe 78

⛳ Clean no ifs overload, empty values handled separately 79

💼 Case 3/4 80

81

82

83

84

85

86

87

88

89

90

91

🤔 How to handle errors correctly? 94

95

🧐 96

97

98

99

100

⛳ What we achieved 101

⛳ Clean & Confident no defensive programming 102

⛳ Single Responsibility Business logics is separated from error handling 103

💼 Case 4/4 104

105

106

107 🏋

React lazy loading 👍 108

🌓 109

https://dev.to

112

113

114

115

116

117

🤔 How to build a good retry function?

120

121

122

😱 125

126

fp-ts 127

Promise in FP Task 128

Promise in FP Task 129

Promise in FP Task 130

Error + Result container Either<Error, Result> 131

Error + Result container Either<Error, Result> 132

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

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

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

136

✅ 137

☑ ✅ 138

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

140

141

142

You can’t just stop speaking about FP

144

146

147

148

149

✅ 150

☑ ✅ ☑ 151

152

153

154

✅ 155 ✅

☑ ✅ ☑ ☑ 156

157

158

159

160

161

✅ 162 ✅ ✅

☑ ☑ ☑ ☑ ✅ 163

164

Сode examples: done 168

⛳ What we achieved 169

⛳ Flexible retry Combine different retry policies 170

⛳ Predictable Know your errors in advance 171

⛳ Isolated side effects Combine complex tasks without execution 172

Problem - Solution Empty values ➡ 173 Option

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

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

Remember?

Option Try, Either Task, TaskEither 177

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

Conclusions 179

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