Ledger: A DSL for accounting
Ledger is a Unix command line tool to do double-entry accounting using only text files. I personally use this tool to manage my personal finances, but since this blog is about Domain Specific Languages, in this post I focus on the domain-specific language used by this tool to model the general ledger used in accounting.
The goal of the language is to represent an accounting ledger, and the core construction is the transaction. So to represent a transaction, you just write the following in a text file and you have your first ledger file.
2022/01/10 Landlord
Expenses:House USD 500.00
Assets:Bank Account
This transaction basically says that on January 10th, I gave my Landlord 500,00 USD from my bank account. A ledger file is basically a long text file full of transactions. The ledger tool is smart enough to deduce all it needs from the transaction. It deduces that there are two accounts Expenses:House
and Assets:Bank Account
(I do not need to declare it before), that there is an asset (or currency) called USD, that there is a payee called Landlord
, and that the separator for the decimals is the dot. The DSL also allows to declare the accounts, payees, and assets separately and then check that you are using only the ones that you declared. Transactions can also involve several accounts and be posted or pending. Finally, on top of that, you can also declare a budget and automation rules to automatically create transactions.
My personal opinion on ledger
Before I continue, let me say that I actually enjoy using ledger and I think that it is a powerful tool aimed at people like me (hardcore geeks that like to work on the console). However…
I see DSLs as a way to let expert express themselves in a way that is both familiar for them while at the same time easy for the computer to parse and analyze. I personally do not think that this DSL is something that an accountant can see and immediately understand what each thing means, and even if she understand it, it is not obvious enough. Indeed, an accountant might be more comfortable by seeing the words she is already familiar with, like “debit” or “credit”, and some visual cues that help her to understand what is trying to be written. This DSL omits these words and uses another way of seeing accounting, a way that resonates much more with computer scientists than with accountants (you might want to read this article to better understand how computer scientists see accounting).
A DSL for accountants, in my opinion, should be something that they can immediately recognize. This should be the case even if it complicates the parsing of the language or even makes it more verbose by adding some extra keywords. Verbosity is something that can be improved with the right tooling to edit the files. On the other hand, if the language is not natural for the domain expert, it stops being a tool to improve his productivity and becomes another hurdle to overcome.
My feeling is that the ledger DSL is more something that helps computer scientists (or geeks) get closer to accounting, than a tool that allows accountants to perform better at their accounting tasks. I would really like to give my ledger file to my accountant and have my taxes done, but I do not see that happening in this part of the multiverse. On the other hand, as a computer scientist, I get to do the basic of accounting without leaving my comfort zone, I work with text files, which I can version control, and even VI comes with syntax highlight for ledger files.
How are tools like ledger useful
Tools like ledger can be used as a way to perform complex operation without investing too much in a user interface. The ledger command line tool is very powerful and takes text as input and outputs text. This means that other tools can be used to easily generate the text input that ledger needs (for example, ledger can import CSV files), and later other tools (like Excel) can be used to present and analyze ledger’s output. Coding and maintaining user interfaces is hard, tedious and time-consuming. Using tools like ledger helps with the data portability (data is just a text file) and keeps the core tool simple and maintainable.
Conclusion
To conclude, ledger is a nice example of what can be done with text files, maybe it does not shine for the easy of use for the actual domain experts, but it shines for the power it allows while staying simple.