Story
Alice uses her todo app for a few days and it does its job. But when she opens the file again, reading it is hard work.
The switch block has grown long. To remember what done does, her eyes have to walk over add, list and everything
in between. The file tells her how every command works, but it never tells her what the commands are.
So she gives each command a name. add, list, done and remove each become a function. The switch block keeps only
the choice: this word means that function. The details move below, out of the way. Now the top of the file reads like a
short table of contents, and when she wants one command she reads one function.
She does not split the file, and she does not add any folders. It is still one file and it still works the same way. Only the shape changed: the decision at the top, the work at the bottom.
Alice's Notebook
- Give every command its own function instead of writing its body inside the switch block.
Choose a file from the tree.
;
;
;
;
;
if !dir dir;
;
switch command {
case "add":
rest;
break;
case "list":
;
break;
case "done":
rest;
break;
case "remove":
rest;
break;
default:
"usage: todo <add|list|done|remove>";
}