Cover....1
Half Title....2
Title Page....4
Copyright Page....5
Dedication....6
Contents....8
1. Introduction....12
1.1. Who is our audience?....12
1.2. What tools and ideas do we cover?....13
1.3. How are these lessons laid out?....14
1.4. How did we get here?....14
1.5. How can people use and contribute to this material?....15
1.6. Who helped us?....16
2. Systems Programming....18
2.1. How can we list a directory?....19
2.2. What is a callback function?....20
2.3. What are anonymous functions?....23
2.4. How can we select a set of files?....24
2.5. How can we copy a set of files?....27
2.6. Exercises....30
3. Asynchronous Programming....34
3.1. How can we manage asynchronous execution?....34
3.2. How do promises work?....37
3.3. How can we chain operations together?....38
3.4. How are real promises different?....40
3.5. How can we build tools with promises?....42
3.6. How can we make this more readable?....45
3.7. How can we handle errors with asynchronous code?....47
3.8. Exercises....48
4. Unit Testing....52
4.1. How should we structure unit testing?....52
4.2. How can we separate registration, execution, and reporting?....53
4.3. How should we structure test registration?....54
4.4. How can we build a command-line interface for testing?....57
4.5. Exercises....59
5. File Backup....64
5.1. How can we uniquely identify files?....64
5.2. How can we back up files?....67
5.3. How can we track which files have already been backed up?....70
5.4. How can we test code that modifies files?....72
5.5. Exercises....76
6. Data Tables....80
6.1. How can we implement data tables?....81
6.2. How can we test the performance of our implementations?....84
6.3. What is the most efficient way to save a table?....86
6.4. Does binary storage improve performance?....87
6.5. Exercises....89
7. Pattern Matching....94
7.1. How can we match query selectors?....94
7.2. How can we implement a simple regular expression matcher?....98
7.3. How can we implement an extensible matcher?....100
7.4. Exercises....108
8. Parsing Expressions....110
8.1. How can we break text into tokens?....111
8.2. How can we turn a list of tokens into a tree?....114
8.3. Exercises....119
9. Page Templates....122
9.1. What will our system look like?....123
9.2. How can we keep track of values?....124
9.3. How do we handle nodes?....125
9.4. How do we implement node handlers?....128
9.5. How can we implement control flow?....132
9.6. How did we know how to do all of this?....134
9.7. Exercises....135
10. Build Manager....138
10.1. What’s in a build manager?....139
10.2. Where should we start?....139
10.3. How can we specify that a file is out-of-date?....144
10.4. How can we update out-of-date files?....145
10.5. How can we add generic build rules?....147
10.6. What should we do next?....153
10.7. Exercises....153
11. Layout Engine....156
11.1. How can we size rows and columns?....157
11.2. How can we position rows and columns?....160
11.3. How can we render elements?....162
11.4. How can we wrap elements to fit?....165
11.5. What subset of CSS will we support?....167
11.6. Exercises....172
12. File Interpolator....176
12.1. How can we evaluate JavaScript dynamically?....176
12.2. How can we manage files?....180
12.3. How can we find files?....182
12.4. How can we interpolate pieces of code?....184
12.5. What did we do instead?....186
12.6. Exercises....186
13. Module Loader....190
13.1. How can we implement namespaces?....190
13.2. How can we load a module?....192
13.3. Do we need to handle circular dependencies?....193
13.4. How can a module load another module?....196
13.5. Exercises....199
14. Style Checker....202
14.1. How can we parse JavaScript to create an AST?....202
14.2. How can we find things in an AST?....204
14.3. How can we apply checks?....206
14.4. How does the AST walker work?....207
14.5. How else could the AST walker work?....209
14.6. What other kinds of analysis can we do?....212
14.7. Exercises....215
15. Code Generator....218
15.1. How can we replace a function with another function?....218
15.2. How can we generate JavaScript?....220
15.3. How can we count how often functions are executed?....222
15.4. How can we time function execution?....224
15.5. Exercises....226
16. Documentation Generator....230
16.1. How can we extract documentation comments?....230
16.2. What input will we try to handle?....233
16.3. How can we avoid duplicating names?....237
16.4. Code is Data....241
16.5. Exercises....242
17. Module Bundler....244
17.1. What will we use as test cases?....244
17.2. How can we find dependencies?....246
17.3. How can we safely combine several files into one?....250
17.4. How can files access each other?....252
17.5. Exercises....257
18. Package Manager....260
18.1. What is semantic versioning?....260
18.2. How can we find a consistent set of packages?....261
18.3. How can we satisfy constraints?....263
18.4. How can we do less work?....265
18.5. Exercises....269
19. Virtual Machine....272
19.1. What is the architecture of our virtual machine?....272
19.2. How can we execute these instructions?....274
19.3. What do assembly programs look like?....277
19.4. How can we store data?....281
19.5. Exercises....283
20. Debugger....286
20.1. What is our starting point?....286
20.2. How can we make a tracing debugger?....288
20.3. How can we make the debugger interactive?....292
20.4. How can we test an interactive application?....294
20.5. Exercises....299
21. Conclusion....302
A. License....304
B. Code of Conduct....308
C. Contributing....310
D. Bibliography....312
E. Glossary....314
Index....336
The best way to learn design in any field is to study examples, and some of the best examples of software design come from the tools programmers use in their own work. Software Design by Example: A Tool-Based Introduction with JavaScript therefore builds small versions of the things programmers use in order to demystify them and give some insights into how experienced programmers think. From a file backup system and a testing framework to a regular expression matcher, a browser layout engine, and a very small compiler, we explore common design patterns, show how making code easier to test also makes it easier to reuse, and help readers understand how debuggers, profilers, package managers, and version control systems work so that they can use them more effectively.
This material can be used for self-paced study, in an undergraduate course on software design, or as the core of an intensive weeklong workshop for working programmers. Each chapter has a set of exercises ranging in size and difficulty from half a dozen lines to a full day’s work. Readers should be familiar with the basics of modern JavaScript, but the more advanced features of the language are explained and illustrated as they are introduced.
All the written material in this project can be freely reused under the terms of the Creative Commons - Attribution license, while all of the software is made available under the terms of the Hippocratic License. All proceeds from sale of this book will go to support the Red Door Family Shelter in Toronto.