Code that asks "what are you?"
After this: Read a function that picks its work with an if per kind, and count the edits each new kind costs.
On this page · 5 sections · 3 checks
You are writing the checkout of a small online shop. Today customers pay by card or by UPI (India's system for paying straight from a bank account with a phone). Next month the owner wants a wallet, and after that net banking. How do you write the checkout so that each new way to pay costs as little work as possible?
This tutorial answers that question. It starts with the way most people write it first.
A function that asks "what are you?"
The first version is one function. It takes the amount, in whole rupees, and the kind of payment as a word. It checks the word, and each branch does the work for that kind:
- a card adds a fee of 2% of the amount;
- UPI has no fee;
- any other word is an error.
def place_order(amount, kind): if kind == "card": return f"card charged {amount + amount * 2 // 100}" elif kind == "upi": return f"UPI paid {amount}" else: raise ValueError("unknown payment: " + kind)
print(place_order(1250, "card"))try: print(place_order(1250, "wallet"))except ValueError as e: print("error:", e)card charged 1275 error: unknown payment: wallet
public class Main { static String placeOrder(int amount, String kind) { if (kind.equals("card")) { return "card charged " + (amount + amount * 2 / 100); } else if (kind.equals("upi")) { return "UPI paid " + amount; } else { throw new IllegalArgumentException("unknown payment: " + kind); } }
public static void main(String[] args) { System.out.println(placeOrder(1250, "card")); try { System.out.println(placeOrder(1250, "wallet")); } catch (IllegalArgumentException e) { System.out.println("error: " + e.getMessage()); } }}card charged 1275 error: unknown payment: wallet
Walk through the two calls:
- place_order(1250, "card")placeOrder(1250, "card"): the word is
"card", so the first branch runs. 2% of 1250 is 25, and the line sayscard charged 1275. - place_order(1250, "wallet")placeOrder(1250, "wallet"):
"wallet"matches neither branch. The last branch raises a ValueErrorthrows an IllegalArgumentException, and the program printserror: unknown payment: wallet.
The fee is worked out with ///, which divides two whole numbers and drops the part after the point. For positive amounts that rounds down, in both languages.
Run it
The code blocks in this tutorial show Python and Java side by side, but only Python runs in the browser. Java does not run here: read the Java version, or paste it into a Java tool on your computer.
Add a branch for "wallet" that returns wallet paid and the amount, and run it again. Notice where you had to type: inside the function.
What each new kind costs
The function works. The trouble starts when the shop grows. Every new kind of payment means opening this function and adding a branch. And a real shop rarely has only one such function. Suppose it also has a refund function and a receipt function, and each asks the same question with its own if:
| Functions that ask the kind | New kinds | Branches to write | Functions edited |
|---|---|---|---|
| 1 (pay) | 1 (wallet) | 1 | 1 |
| 3 (pay, refund, receipt) | 1 (wallet) | 3 | 3 |
| 3 (pay, refund, receipt) | 2 (wallet, net banking) | 6 | 3 |
Each edit is a chance to break a kind that already worked. Forget one branch in the receipt function and the wallet pays but prints no receipt line.
The root of the problem is the question the code asks. It asks each payment "what are you?" and then decides what to do. The rest of this tutorial turns the question around: the checkout asks "can you pay?", and each kind of payment knows how to do its own work. A new kind is then a new class, and the checkout is not edited at all.
Check yourself
The same function, asked for 800 by "card" and then by "cash". What does this print?
def place_order(amount, kind): if kind == "card": return f"card charged {amount + amount * 2 // 100}" elif kind == "upi": return f"UPI paid {amount}" else: raise ValueError("unknown payment: " + kind)
print(place_order(800, "card"))try: print(place_order(800, "cash"))except ValueError as e: print("error:", e)public class Main { static String placeOrder(int amount, String kind) { if (kind.equals("card")) { return "card charged " + (amount + amount * 2 / 100); } else if (kind.equals("upi")) { return "UPI paid " + amount; } else { throw new IllegalArgumentException("unknown payment: " + kind); } }
public static void main(String[] args) { System.out.println(placeOrder(800, "card")); try { System.out.println(placeOrder(800, "cash")); } catch (IllegalArgumentException e) { System.out.println("error: " + e.getMessage()); } }}The shop wants a third way to pay, a wallet. With the code above, what has to change so that place_order(1250, "wallet")placeOrder(1250, "wallet") works?
A shop has three functions that each ask for the kind of payment with an if: one to pay, one to refund and one to print a receipt line. It adds two new kinds. How many functions are edited, and how many new branches are written?
Honesty notes
- Read a function that picks its work with one branch per kind of payment, and say what it prints for a kind it does not know.
- Say why every new kind means opening that function and adding a branch.
- Count the edits: each function that asks for the kind needs one new branch per new kind.
- Say the question this tutorial turns around: from "what are you?" to "can you pay?".