3 ms·
If you write: int balance = 0; int get {...} void set(int balance) {...} void withdraw(int amt) { if amnt <= balance { balance -= amnt; }
by mrkeen 2y ago
If you write:
int balance = 0;
int get {...}
void set(int balance) {...}
void withdraw(int amt) {
if amnt <= balance {
balance -= amnt;
}
}
it will be flagged immediately in code review as a race condition and/or something that doesn't guarantee its implied balance>=0 invariant. Because threading.
But lift the logic up into a REST controller like pretty much all web app backends:
GET /balance/get
POST /balance/set
POST /balance/withdraw
And it will sail straight through review. (Because we pretend each caller isn't its own Thread.)
- baggy_trough 2y agoYou're right, we can't get away from concurrency control with database, cache, even the file system. I'm still happy to never have to think about in-process multithreading bugs.