CASE: code a sum correctly

There’s special attention to be made when you want to summarise certain specific ranges.

Take below example, and take a good look at the sum:
Screen Shot 2021-02-17 at 13.23.55

This is a specific example created to show you how to code a table like this, and how not to do it. As you can see, something isn’t right with the sum :thinking:

Most developers, might think the sum (code 10) can be linked to a range, while the sub-parts (101 and 102) also have to be linked.
It would go something like this:

{% stripnewlines %}
|----20%----
|----15%----:

{% comment %}
================
    Code 10
================
{% endcomment %}
{% assign acc_range = "10" %}
{% assign acc_value = period.accounts | range:acc_range %}
{% newline %}
|--> **10 Cap.**
| **{{ -acc_value | currency }}**

{% comment %}
================
    Code 101
================
{% endcomment %}
{% assign acc_range = "101" %}
{% assign acc_value = period.accounts | range:acc_range %}
{% newline %}
|----> 101
| {{ -acc_value | currency }}

{% comment %}
================
    Code 102
================
{% endcomment %}
{% assign acc_range = "102" %}
{% assign acc_value = period.accounts | range:acc_range %}
{% newline %}
|----> 102
| {{ -acc_value | currency }}
{% endstripnewlines %}

Notice we first create the sum, and link it to the account range of “10”? And after that, we create the sub-parts?

This is actually not good coding, because:

  • The sum shouldn’t be linked to the account range of “10” at all (it actually should just be the sum of those sub-parts :point_up: )
    Think of this example: code 10 can only have the values of ranges 101 & 102, so what happens if an accountant booked a value on range 108? granted, perhaps an extreme example, but the value of range 108 would’ve been added to the sum, while actually not being part of the sum visually
  • Even if the account range of the sum would’ve been coded correctly, it still holds a certain risk if one of the sub-parts has an update (or a new sub-part needs to be added)
    A clear example: a new sub-part is added, linked to the range of 105. You then need to be sure to also update the range of the sum (code 10); if forgotten, the sum also won’t be correct

Then how can we code this correct and efficient?

First, the sub-parts should be created, while not being shown in the table. This can be done with the capture-tag, which let’s you render code (execute it in the background), while not show it.
Only when you print the capture, will it be made visible as well.

In our example, it would look like this:

{% assign total_value = 0 %}

{% stripnewlines %}
|----20%----
|----15%----:

{% capture code_10_parts %}
{% assign acc_range = "101" %}
{% assign acc_value = period.accounts | range:acc_range %}
{% newline %}
|----> 101
| {{ -acc_value | currency }}
  {% assign total_value = total_value+acc_value %}

{% assign acc_range = "102" %}
{% assign acc_value = period.accounts | range:acc_range %}
{% newline %}
|----> 102
| {{ -acc_value | currency }}
  {% assign total_value = total_value+acc_value %}
{% endcapture %}

{% newline %}
|--> **10 Cap.**
| **{{ -total_value | currency }}**

{{ code_10_parts }}

{% endstripnewlines %} 

So we first capture the sub-parts, so the sub-values are assigned to a variable:

{% capture code_10_parts %}
{% assign acc_range = "101" %}
{% assign acc_value = period.accounts | range:acc_range %}
{% newline %}
|----> 101
| {{ -acc_value | currency }}
  {% assign total_value = total_value+acc_value %}

{% assign acc_range = "102" %}
{% assign acc_value = period.accounts | range:acc_range %}
{% newline %}
|----> 102
| {{ -acc_value | currency }}
  {% assign total_value = total_value+acc_value %}
{% endcapture %} 

meaning above code gets executed before we print the total (code 10):

{% newline %}
|--> **10 Cap.**
| **{{ -total_value | currency }}**

After we print the total (code 10), will we also print our capture (to show the sub-parts):

{{ code_10_parts }}

In above example, if a change is to happen to the sub-parts, you only need to change it once, and the sum (code 10) will always also be exactly that: a sum