Calculate values in two different ways - A or B?

by user190707   Last Updated September 11, 2019 18:06 PM

My question is about code and database design. How should I design my code to calculate simple variables (stored in sqlite db) like the balance of a wallet?

Example: We have tables for customers, wallets, transactions. We have methods to fill these tables. When receiving money from a customer, we add the amount to the balance of a wallet and submit a transaction (date, amount, ...) into the transactions table.

I have 2 approaches to design my code and database - which one is more appropriate and bugfree?

A) When doing a transaction I add / subtract the amount to my the balance of a wallet and submit a transaction into a table transaction with information like wallet_id, amount, date...

B) I just submit the transaction to the transactions table and recalculate the balance of a specific wallet by checking for columns with the matching wallet_id and add up the wallet_balance from there.

Which is the right way? For me A) looks more prone to bugs and safe code requires more effort. I would go with B) which would be much easier then I would add more features like deleting transactions (+ recalculating balances) and so on.

What is your advice?

Tags : mysql

Related Questions

Updated November 15, 2016 08:02 AM

Updated April 09, 2015 22:02 PM

Updated August 24, 2018 15:06 PM

Updated December 27, 2018 07:06 AM

Updated June 11, 2015 18:02 PM