Composing Software (2019 ) PDF

Title Composing Software (2019 )
Author Derek Lim
Course Core course: European Art 1400–1900: Meaning and interpretation
Institution The Chancellor, Masters, and Scholars of the University of Cambridge
Pages 229
File Size 2.7 MB
File Type PDF
Total Downloads 79
Total Views 122

Summary

fasfdafadf...


Description

Composing Software An Exploration of Functional Programming and Object Composition in JavaScript Eric Elliott This book is for sale at http://leanpub.com/composingsoftware This version was published on 2019-02-24

This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do. © 2017 - 2019 Eric Elliott

Contents Thank You . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1

Composing Software: An Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 You Compose Software Every Day . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 The Dao of Immutability (The Way of the Functional Programmer) . . . . . . . . . . . . . . 11 Forward . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 The Rise and Fall and Rise of Functional Programming (Composable Software) . . . . . . . The Rise of Functional Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Fall of Functional Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Rise of Functional Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Functional Programming Has Always Been Alive and Well . . . . . . . . . . . . . . . . . . .

13 13 16 16 17

Why Learn Functional Programming in JavaScript? . . . . . . . . . . . . . . . . . . . . . . . . 18 Pure Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What is a Function? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Pure Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Trouble with Shared State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Same Input, Same Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . No Side Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

25 25 25 26 27 28 29 32

What is Functional Programming? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Pure Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Function Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Shared State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Immutability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Side Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Reusability Through Higher Order Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . Containers, Functors, Lists, and Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Declarative vs Imperative . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

33 34 34 34 37 38 38 39 40

CONTENTS

Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 A Functional Programmer’s Introduction to JavaScript . . . . . . . . . . . . . . . . . . . . . . Expressions and Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Destructuring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Comparisons and Ternaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Currying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Function Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Method Chaining . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

43 43 44 45 46 47 51 53 54 54 55

Higher Order Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Curry and Function Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What is a curried function? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What is a partial application? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What’s the Difference? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What is point-free style? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Why do we curry? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Trace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Curry and Function Composition, Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

59 59 60 60 60 61 63 64 69

Abstraction & Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Abstraction is simplification. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Abstraction in Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Abstraction through composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . How to Do More with Less Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Reduce . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Reduce is Versatile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

71 72 72 73 74 75 75 76 80

Functors & Categories . . . . Why Functors? . . . . . . Functor Laws . . . . . . . Category Theory . . . . . Build Your Own Functor Curried Map . . . . . . . . Conclusion . . . . . . . . .

81 82 86 87 89 90 91

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

CONTENTS

Monads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 You’re probably already using monads. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 What Monads are Made of . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Building a Kleisli Composition Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 The Monad Laws . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 The Forgotten History of OOP The Big Idea . . . . . . . . . . The Essence of OOP . . . . . What OOP Doesn’t Mean . . What is an object? . . . . . . We’ve lost the plot. . . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

110 111 113 114 116 117

Object Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 What is Object Composition? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 Three Different Forms of Object Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Notes on Code Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Aggregation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 Delegation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Factory Functions . . . . . . . . . . . . . . . . . Literals for One, Factories for Many . . . Returning Objects . . . . . . . . . . . . . . Destructuring . . . . . . . . . . . . . . . . . Computed Property Keys . . . . . . . . . . Default Parameters . . . . . . . . . . . . . . Type Inference . . . . . . . . . . . . . . . . Factory Functions for Mixin Composition Conclusion . . . . . . . . . . . . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138

Functional Mixins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 What are mixins? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 What is functional inheritance? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 What is a functional mixin? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 Composing Functional Mixins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 When to Use Functional Mixins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 Caveats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Why Composition is Harder with Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151

CONTENTS

The Delegate Prototype . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 The .constructor Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Class to Factory is a Breaking Change . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Composable Custom Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 You can do this with any data type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 Composable Currency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 Lenses . . . . . . . . . . Why Lenses? . . . Background . . . . Lens Laws . . . . . Composing Lenses

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . 167 . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 . . . . . . . . . . . . . . . . . . . . . . . . . . . 171

Transducers . . . . . . . . . . . . . . . . . Why Transducers? . . . . . . . . . . . Background and Etymology . . . . . A Musical Analogy for Transducers Transducers compose top-to-bottom. Transducer Rules . . . . . . . . . . . . Transducing . . . . . . . . . . . . . . . The Transducer Protocol . . . . . . . Conclusion . . . . . . . . . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190

Elements of JavaScript Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 1. Make the function the unit of composition. One job for each function. . . . . . . . . . . . 192 2. Omit needless code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 3. Use active voice. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 4. Avoid a succession of loose statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 5. Keep related code together. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 6. Put statements and expressions in positive form. . . . . . . . . . . . . . . . . . . . . . . . . 200 7. Use parallel code for parallel concepts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 Conclusion: Code should be simple, not simplistic. . . . . . . . . . . . . . . . . . . . . . . . . 203 Mocking is a Code Smell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 TDD should lead to better design. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 What is a code smell? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 What is a mock? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 What is a unit test? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 What is test coverage? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 What is tight coupling? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 What causes tight coupling? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 What does composition have to do with mocking? . . . . . . . . . . . . . . . . . . . . . . . . 209 How do we remove coupling? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212

CONTENTS

“Code smells” are warning signs, not laws. Mocks are not evil. . . . . . . . . . . . . . . . . . 220

Thank You To my editor, JS Cheerleader, who made this book much better in too many ways to list. If you find the text readable, it is because she carefully poured over every page and offered insightful feedback and encouragement every step of the way. Without her help, you would not be reading this book right now. To the blog readers, who’s support and enthusiasm helped us turn a little blog post series into a phenomenon that attracted millions of readers and provided the momentum we needed to turn it into a book. To the legends of computer science who paved the way. “If I have seen further it is by standing on the shoulders of Giants.” ∼ Sir Isaac Newton

Composing Software: An Introduction Composition: “The act of combining parts or elements to form a whole.” ∼ Dictionary.com In my first high school programming class, I was instructed that software development is “the act of breaking a complex problem down into smaller problems, and composing simple solutions to form a complete solution to the complex problem.” One of my biggest regrets in life is that I failed to understand the significance of that lesson early on. I learned the essence of software design far too late in life. I have interviewed hundreds of developers. What I’ve learned from those sessions is that I’m not alone. Very few working software developers have a good grasp on the essence of software development. They aren’t aware of the most important tools we have at our disposal, or how to put them to good use. 100% have struggled to answer one or both of the most important questions in the field of software development: • What is function composition? • What is object composition? The problem is that you can’t avoid composition just because you’re not aware of it. You still do it – but you do it badly. You write code with more bugs, and make it harder for other developers to understand. This is a big problem. The effects are very costly. We spend more time maintaining software than we do creating it from scratch, and our bugs impact billions of people all over the world. The entire world runs on software today. Every new car is a mini super-computer on wheels, and problems with software design cause real accidents and cost real human lives. In 2013, a jury found Toyota’s software development team guilty of “reckless disregard”¹ after an accident investigation revealed spaghetti code with 10,000 global variables. Hackers and governments stockpile bugs² in order to spy on people, steal credit cards, harness computing resources to launch Distributed Denial of Service (DDoS) attacks, crack passwords, and even manipulate elections³. We must do better. ¹http://www.safetyresearch.net/blog/articles/toyota-unintended-acceleration-and-big-bowl-%E2%80%9Cspaghetti%E2%80%9D-code ²https://www.technologyreview.com/s/607875/s...


Similar Free PDFs