Why Personal Ledger Uses VIPER

blogs 8 min #Personal Ledger#Rust#AI

A black and white photo of a Gaboon viper's head, turned towards the letters V, I, P, E and R, listing View, Interactor, Presenter, Entity and Router.
Photo: Gaboon viper (Bitis gabonica) by TimVickers, Wikimedia Commons, public domain.
contents

Personal Ledger has been written with the assistance of AI. That reinforces, not changes, what I want from an architecture. I still want code that is easy to read, test and change, but I also want code that an assistant can pick up cold just like you would with a code base worked on by a big team, change in one place, and hand back to me in a shape I can review quickly.

Early on, the app was heading the usual way: a few big files that knew about everything. The screens drew the data, worked out the rules, saved to the database and decided where to go next. Every change touched one of those files, and every change got a little harder. So I moved Personal Ledger to an architecture called VIPER. This post covers what VIPER is, why I think it is a good fit for most apps, and why it is an even better fit when an AI is writing a lot of the code.

What VIPER is

VIPER comes from iOS development, where view controllers tended to grow huge because they did everything. It splits each area of an app, called a module, into five parts. Each part has one job, talks only to its neighbours, and data flows one way through them.

Diagram of the VIPER model. The View sends events to the Presenter and gets a view model back. The Presenter calls the Interactor and gets a result back. The Presenter asks the Router to navigate, and the Router builds and opens Views. The Interactor uses Entities.

The VIPER model: each part has one job and only talks to its neighbours.

Why VIPER is good for development

The main benefit is separation of concerns. Each part answers one question, so when something goes wrong, you know where to look. A wrong total is in an Interactor or an Entity. A total shown with the wrong number of decimal places is in a Presenter. A total drawn in the wrong spot is in a View.

It also makes testing easy. Interactors, Presenters and Entities have no UI toolkit and no database in them, so they can be tested with plain unit tests and in-memory fakes, no window and no SQLite file needed. The Views are thin enough that their tests only need to check wiring and drawing.

And it suits an app with more than one front end. Personal Ledger has two Clients, a desktop app and a terminal app. Both show the same Ledger, so both use the same Presenters and Interactors. Only the Views and the Router implementation are written twice, once for each UI toolkit. When I fix a rule, both Clients get the fix.

The cost is boilerplate. A small feature that could be twenty lines in one file becomes an Entity, an Interactor, a Presenter with its event and view model types, a port, a fake for the port, a View and a Router method. That is a lot of ceremony, and it’s why many people try VIPER once and walk away.

Why it fits AI-assisted development

When I am working with an AI assistant, that trade-off changes.

Boilerplate is cheap now

Boilerplate is what an AI is best at. It is repetitive and follows a pattern that is already in the codebase. When I ask for a new use case, the assistant reads an existing Interactor, its port and its test, and writes the new one in the same shape. The ceremony that used to cost me an afternoon of typing now costs a few seconds of generation and a few minutes of review.

The structure also helps the review. Because every module has the same shape, I know what to expect before I open a file. An Interactor that has started formatting text, or a View that is making a decision, stands out straight away. The pattern is a checklist the assistant and I both work from.

Small files mean less context

An AI assistant works inside a context window: everything it has read, everything it has written, and the conversation so far. That window is limited, and what fills it matters. The more irrelevant code an assistant has to read to make one change, the less room it has to think about the change, the more it costs, and the more likely it is to lose track of a detail.

VIPER keeps files small and focused. In Personal Ledger, each use case is its own file, so “change how deleting a payee works” means reading one Interactor of about 80 lines, its port and its tests, not a whole screen file that also draws the list, handles the keys and talks to the database. Across the shared libraries, there are over 500 source files, and the median is around 140 lines. The Presenters are the biggest part because they hold a screen’s state, but they are still one screen each.

Smaller files help in a few ways:

Boundaries the assistant can’t quietly cross

Separation of concerns is only useful if it holds. With an AI writing code, the easy shortcut is always there: reach into the database from a screen, or have one domain call another directly, because it works and the tests pass.

Personal Ledger uses tooling, not AI, so it doesn’t rely on the assistant remembering the rules. The shared libraries can’t depend on a UI toolkit, on the database crate, or on each other, and the build checks this. If a change breaks a boundary, the tooling fails it before I ever see it. That turns the architecture from a convention into something the tools enforce, which matters a lot more when you are not typing every line yourself.

How Personal Ledger uses VIPER

Personal Ledger is written in Rust and split into crates. VIPER maps onto them like this:

Diagram of Personal Ledger’s VIPER layout. The desktop and terminal Clients hold the Views and Router implementations, and both depend on the feature crates. Each feature crate holds Presenters, Interactors, Entities, ports, the Router trait, the keymap and Messages. A cross-domain workflow crate writes two domains through one atomic port. lib-database implements every port over SQLite. lib-core and lib-viper hold shared types used by all.

How Personal Ledger splits VIPER across its Rust crates. Dependencies point inward, toward the Entities.

Moving to this layout is still in progress. Each domain moves over in its own slice, and the architecture notes set out where it is heading. But the payoff has already shown up. New features land as new files rather than longer ones, and most changes the assistant makes stay inside one crate and one layer.

Wrapping up

VIPER asks you to write more code up front in exchange for code that is easier to understand, test and change. For a long time that was a hard sell. With an AI assistant writing much of the boilerplate, the cost has dropped a lot, and the benefits have grown: small files keep the assistant’s context focused, clear boundaries keep its changes contained, and a consistent shape makes its work quick to review.

If you are building something with an AI assistant and your files keep getting longer, it is worth a look. The code is on GitHub if you want to see how it hangs together.