A Second Face

Alice in Userland

Story

Bob is back.

Dear Alice,

The sorting is lovely, thank you. One more thing, if it is not too much trouble: I keep a browser open all day, and I would much rather look at my todos on a page than type a command every time.

So Alice adds one more command: todo serve. It starts a small web server on 127.0.0.1:8080, and the page it hands back does everything the terminal does: write a todo down, say what it waits for, tick one off, throw one away, and show them filtered or in alphabetical order. The page is finished before it is sent. There is no clever machinery: she drops the todos into a piece of HTML written inside a template string, spends a little while on the spacing so it is not painful to look at, and lets Express do the listening and the answering. All of it lives in a new file, web.ts, because a page is not a terminal, and by now she knows to keep those two apart.

What surprises her is the part she did not do. She never opened todo.ts. The browser asks the same questions the terminal asks, changes todos with the same three functions, and reads from the folder or the database according to the same setting. Two faces, one set of rules underneath, and the rules never noticed the second face arrive.

Alice's Notebook

  • Give every command its own function instead of writing its body inside the switch block.
  • Keep the code that reads the terminal apart from the code that knows what a todo is.
  • Handle different concerns in different parts of the program (Single Responsibility Principle).
  • Use a library for the work that is not your program’s real job.
  • Keep the rules in one place, so that you can put a new face on them without changing them.

Choose a file from the tree.