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

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

4

Grammarly Challenges 5

Text is difficult 6

Change is difficult ➡ 7

Our combo Object Oriented Design 8

Our combo Object Oriented Design 9

10

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

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

TypeScript is… 13

TypeScript is… 💚 14

TypeScript is… Confident refactoring 15

TypeScript is… Interfaces! 16

TypeScript is… Interfaces! 17

= ❤ Types + 18

19

20

TypeScript FP libs Funfix fp-ts 21

Functional Programming: Real-Life Cases 22

💼 Case 1/4 23

24

25

😊 26

🧪 How to experiment? 27

28

29

30

31

🤔 Can we 🧪 better ? 32

33

34

35

36

37

38

Immutability 39

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

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

Pure Functions 42

43

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

💼 Case 2/4 45

46

47

48

49

🙏 Will not compile 50

🤔 But how to deal with null? 51

52

🤔 Empty alert? Exception? … 53

  1. Null 2) Undefined Bad 54

55

56

57

58

59

Option ≡ an array with 0…1 elements 60

map filter forEach contains … empty getOrElse fold … 61

62

63

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

💼 Case 3/4 66

67

68

69

70

71

72

73

74

75

76

77

🤔 How to handle errors correctly? 79

80

81

82

83

84

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

💼 Case 4/4 86

87

88

🏋 89

React lazy loading 👍 90

🌓 91

https://dev.to

94

95

96

97

98

99

🤔 How to build a good retry function?

102

103

104

107

108

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

Promise in FP Task 110

Promise in FP Task 111

Promise in FP Task 112

Error + Result container Either<Error, Result> 113

Error + Result container Either<Error, Result> 114

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

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

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

✅ 118

☑ ✅ 119

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

121

122

123

You can’t just stop speaking about FP

125

127

128

129

✅ 130

☑ ✅ ☑ 131

132

133

134

✅ 135 ✅

☑ ✅ ☑ ☑ 136

137

138

139

140

☑ ☑ ☑ ☑ ✅ 141

Сode examples: done 146

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

Problem - Solution Empty values ➡ 148 Option

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

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

Remember?

Option Try, Either Task, TaskEither 152

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

Conclusions Twitter: @oopets 154

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