Structuring large Clojure codebases with Biff
9 months ago
- #Web Development
- #Clojure
- #Database Optimization
- Jacob O'Bryant is rewriting Yakread, a reading app, from scratch and open-sourcing it.
- He is experimenting with new features for Biff, a Clojure web framework, to manage larger codebases.
- Old Yakread had slow queries, like taking over 10 seconds to load subscription data.
- Traditional denormalization was avoided by implementing materialized views for XTDB, stored in RocksDB.
- Materialized views use pure 'denormalizer' functions to update data incrementally.
- Testing is simplified with property-based testing and oracle functions to verify materialized views.
- All application code in 'New Yakread' is pure, with no mocks needed in unit tests.
- Functions are structured as state machines, separating pure logic from effect handlers.
- Effect handlers are centralized and kept simple, improving testability and observability.
- Unit tests compare function outputs against saved EDN files, making testing more convenient.
- The codebase uses Pathom resolvers to separate model and view code, similar to OOP but with pure data.
- Pathom resolvers handle database queries, derived data, and even UI fragments, keeping code modular.
- GET request handlers define Pathom queries, simplifying backend logic and improving clarity.
- The approach is similar to Fulcro but tailored for server-side rendering with htmx.
- Yakread's rewrite is ongoing, with plans to integrate successful experiments into Biff.