One Job Each

Alice in Userland

Story

The list works, but it is ugly. The id, the title and the waiting ones all run together on one line, and Alice has to read every line carefully to see anything. She wants columns: id, status, title, and what it is still waiting for. So she opens todo.ts, where all the printing lives, and starts writing the table there.

She stops before she finishes it. todo.ts is already the big file and main.ts is nearly empty, and this new code would only make that worse. Then she remembers something she heard from older developers: the single responsibility principle. She reads her file again and sees it. todo.ts is supposed to know about todos, but it also reads the words she typed, prints every line and holds the help text. That is not one job, that is two.

So she moves the second job out. Now todo.ts only answers questions and changes todos: it hands back a todo or a list of them, and when something is wrong it raises an error instead of printing one. main.ts does the rest. It reads the arguments, catches the errors, and draws the table. The two files are about the same size now, and each one is about one thing. She writes the rule in her notebook, so that next time she sees it sooner.

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).

Choose a file from the tree.