3

What I was taught in the university was we don't save the results in the database, but only the raw data which need for the calculation. For a very basic example,

if:

a + b + c = z

we only save a, b and c in the database. whenever we need z, we retrieve a, b and c from the database and calculate z at the software layer.

Imagine if the calculation is changed at some point.

2(a + b) + c = z

We still have the raw data and only need to change the calculation at the software.

But what if we need to keep track of which calculation was originally used to calculate z? Can we do that without saving the result (z in this case) in the DB?

3

You are confused because for every theoretical programming concept or principle (like "avoiding redundancy in a database") one can artificially construct a situation where the concept does not fit any more.

What they missed to teach you at the university is introducing redundancy into a database is not bad "in general". It is often (opposed always) just not necessary and bears a certain risk of making a system prone to errors. However, sometimes that risk is justified and sometimes it is the only way of getting certain requirements implemented.

For knowing if and how redundancy in a system is required, one needs to know the real use case and understand the real world situation. And that is the problem with a question like

But what if we need to keep track of which calculation was originally used to calculate z

this is not a real use case. It is an inventend situation which can mean different things in different real world situations. Sure one can "keep track of which calculation was originally used": it could mean

  • storing the whole formula, or

  • storing an index into a list of formulas, or

  • putting those formula and the result outside the database into a log file, or

  • just storing z redundantly, or

  • storing z with a timestamp, or with a version number, or

  • it could mean providing different versions of the same calculation program in parallel for production, without storing anything else, or

  • something else, give enough time and space for additional ideas.

To decide about the right design one needs context to validate the requirements, not just some vague idea. Real use cases provide such context, so ask your question when having such a real situation at hand, then you can probably answer that by yourself (or describe that use case here and get a more specific answer).

1
  • Thank you very much for such a descriptive answer. Actually, it is not a real use case. Where I'm currently working, good practices and design principles are not compulsory. (It is not a software development company, we develop bespoke software to support the main production.) In some legacy systems, developers used to store the calculation results in the DB. Since they are very simple calculations, the only reason I could imagine to do so was the situation I described in the question. So as a beginner I just wanted some clarification. Thanks again for your valuable effort. – Sherantha Apr 10 '18 at 4:32
2

Best practice dictates that you focus on persisting only what you need, calculating everything else. Not only does this reduce space on the disk, but it avoids errors relative to allowing inconsistent data (for example, saving x=5, y=10, x+y=15, then later changing x to 3, making the previously calculated result 15 no longer valid). However as @DocBrown so pointedly mentioned, best practice is only a rule of thumb, not a rule.

Suppose for a second that you're doing bitcoin calculation and you want to save your current progress. The program has been running for 5 hours straight.. clearly you don't follow best practice here, since you'd have to repeat 5 hours of calculation to get to the same point.

If the formula itself could change, then it stands to reason that this also becomes data, and more importantly, it is data that you need in order to calculate the result. Therefore there is absolutely nothing wrong with saving the formula in addition to the data in that case.

Ultimately it's up to you, the programmer, to determine whether saving the calculation is worth it to you, and in this case, it is not really even a question here as you must have the formula to perform the calculation. Though in general, less is more.

1
  • +1 Thank you very much for your answer. I've got a clear idea now. – Sherantha Apr 10 '18 at 4:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.