Code Shaman — Clean Code

Cleanse dirty code: 12 levels of renaming, extracting, removing duplication and SRP. The most hands-on clean code refactoring game.

What you learn in this game

A code smell is not a bug: the code still runs. It is a sign that the code is hard to read or to change. At Warung Pantai, the old owner's code still cooks correctly, but only Mak Ijah can read it. Tap the smelly lines, then pick the cure (a refactoring). The right cure makes the smell go away and keeps every regular's order the same.

A magic number is a number that turns up in the middle of code with no name. 4000 could be a rate, a weight, anything. The cure: name it with a constant. Its value stays, so the fee stays, but now the rule can be read.

DRY = Don't Repeat Yourself. When two places write the same rule, one change must be remembered in two places. The cure: one named function, called wherever it is needed.

Guard clause: check the failing condition first, then leave the function. Instead of if inside if inside if, each condition becomes one flat line.

A good comment explains why: a reason, a rule of the warung, a trap the code does not show. A bad comment repeats what the code already says. If code needs a what comment, it is usually the code that needs fixing: its names, or its functions.

A function with many parameters is hard to call: their order must be memorised. A parameter object gathers them all in one object, so the caller writes registerRegular({ name: "Bu Yanti", spice: 2 }) and each value comes with its name.

What the levels ask

  1. Bagas cannot read harga.js: it is all a, b, c and x. Tap every line whose names do not say what they hold, then pick the cure. The regulars' prices must not change.
  2. The delivery fee in ongkir.js is full of numbers with no name. Tap every line that uses a magic number, then pick the cure. The regulars' delivery fees must not change.
  3. label.js keeps something that is never used and a line that never runs. Tap every line of dead code, then pick the cure. The regulars' parcel labels must not change.
  4. tagihan.js adds up food and drinks with two identical lines. Tap both twin lines, then pick the cure. The regulars' bills must not change.
  5. printReceipt adds up the total and writes the receipt lines at once. Tap the function that is too long, then pick the cure. The regulars' receipts must not change.
  6. canOrder checks four conditions with if inside if. Tap every line nested too deep, then pick the cure. The answers for the regulars must not change.
  7. takeOrder checks the order, adds up the total and writes it in the book, all by itself. Tap the function that is too long, then pick the cure. The order book must not change.
  8. gelas.js writes names on cups, but its own names are process, data, info and temp. Tap every line whose names say nothing, then pick the cure. What goes on the cups must not change.
  9. langganan.js is full of comments. Tap every comment that only repeats the code below it, then pick the cure. Looking up a regular must not change.
  10. deliveryLabel belongs to Order, but it builds the address only from customer data. Tap the line that envies another object's data, then pick the cure. The delivery addresses must not change.
  11. registerRegular takes six parameters that must be filled in the right order. Tap the line with too many parameters, then pick the cure. The regulars' cards must not change.
  12. bungkus.js has many smells at once. Tap every smelly line, then pick the cure. The takeaway totals for the boat crews must not change by a single rupiah.