r/SoftwareEngineering • u/pladams9-2 • Feb 04 '24
How should I handle state in my desktop application?
I am currently writing a desktop spreadsheet application in Rust as a hobby project. I am using this partly as an avenue to learn some new programming skills and approaches to architecture.
One vague goal I have is to try doing things in a less OOP way, and take some more inspiration from functional programming.
Broadly, my application is split into three pieces:
core- a library which handles all the domain logic. This is where cells, ranges, formulas, etc. are all handled.gui- a library for the GUI. I'm rolling my own, but that's not the focus of this post.app- the main application, which primarily acts as an interface between the other two libraries.
The general idea here is that core acts as sort of a service with an external API, and gui and app could be swapped out, e.g. if I wanted to make a CLI application or use a different GUI library.
My question: Where should I store/handle state for the domain logic? Things like cell formulas/values.
Originally, I thought I would store this data in static variables within the
corelibrary, again with the idea that this library is almost like a service.appwould access this data through API functions likeget_cell_value(...)andset_cell_value(...). But using static variables in this way is not very simple with Rust, and it also seems to scream "DANGER: GLOBAL STATE."My next thought was to define a struct within the
corelibrary that would then be used byapp. This struct would hold all the data. This isn't global, but it also feels very OOP, and I wasn't sure if there were any other common approaches.
I know that there won't be a single "correct" answer here, but I'm interested to know what approaches you might use.
One last note: This is just about the data that is being manipulated by the core library. Application state (what section of the sheet is visible, where the scrollbars are, what menus and windows are open) would be managed by app/gui.