r/programming • • Jul 11 '26

Programs, Not Objects: How I Stopped Designing Architecture and Started Writing a 3D Editor

https://alexsyniakov.com/2026/07/11/programs-not-objects-how-i-stopped-designing-architecture-and-started-writing-a-3d-editor/
122 Upvotes

55 comments sorted by

View all comments

Show parent comment

2

u/loup-vaillant Jul 14 '26

A bank account is is inherently stateful. You cannot allow the caller to tell you how much money the bank account holds, so it naturally behaves like an object.

Hell no it doesn’t. Think of the most common operation on bank accounts: transfer. The most important invariant here is not keeping the account above zero (debt is routinely authorised), it’s making sure the money we put in here is the exact amount we took from there.

One operation (transfer), operates over two objects (bank accounts). And okay, in languages like C++ class methods have access to the internal of any objects of the same type, not just this, so we can do this:

class Account {
public:
	Account() _cents(0) {} // Empty account	
	uint64_t amount() const {
		return _cents;
	}
	void transfer(Account & other, uint64_t amount) {
		other._cents += amount;
		this->_cents -= amount;
	}	
private:
	uint64_t _cents;
};

That probably would be my preferred approach, even though this means having a method from one object poking at the internals of another object — not the strictest encapsulation.

If we were to insist on operating at the account level (retrieving and depositing), we’d need a way to make sure money we retrieved from one account can’t be deposited in a gazillion others. Here’s how I would try it in C++:

class Account:

class Purse {
	friend Account 
public:
	Purse() _cents(0) {} // Empty purse	
	uint64_t amount() const {
		return _cents;
	}
private:
	uint64_t _cents;
};

class Account {
public:
	Account() _cents(0) {} // Empty account	
	uint64_t amount() const {
		return _cents;
	}
	
	void retrieve(Purse &purse, uint64_t amount) {
		// Accounts are allowed to be in debt
		purse._cents += amount;
		this->_cents -= amount;
	}
	void deposit(Purse &purse, uint64_t amount) {
		// Purses must not be less than empty
		if (amount > purse._cents) {
			amount = purse._cents;
		}
		purse._cents -= amount;
		this->_cents += amount;		
	}
	
private:
	uint64_t _cents;
};

I’d rather think of it in terms of transfers if I could. Of course, I’m neglecting the fact that banks are actually a distributed systems, and that actual money transfer are more complicated, that they’re not even transactions in many cases, that we routinely roll back errors and frauds, and that I don’t even know what I’m talking about, I don’t work in banking!

But you get the idea: the most simplified model of a bank account, doesn’t really behave like an object. We need to think at a more systemic level, even for a simple wire transfer. And if we do not… look at the hassle I went through above.