CASE: use variables instead of registers

While registers are great to store certain numeric values to it, it does have some disadvantages:

  • you can not tell by the register which value it contains, which makes it harder to debug eg
  • the amount of registers are limited (40)
  • if lots of summations are done, it gets complex pretty quickly with all the different registers around

But using registers isn’t something that has to be used, as there is another way that will be more clear to read.

Let’s say we have a table with all values, and at the end we need to show the sum of current and previous year:

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

{% comment %}section A{% endcomment %}
{% assign acc_range = "60" %}
{% assign cy_value = period.accounts | range:acc_range %}
{% assign py_value = period.minus_1y.accounts | range:acc_range %}

{% newline %}
| {% t "goods" %}
| {{ cy_value | currency }}
| {{ py_value | currency }}

{% comment %}section A{% endcomment %}
{% assign acc_range = "61" %}
{% assign cy_value = period.accounts | range:acc_range %}
{% assign py_value = period.minus_1y.accounts | range:acc_range %}

{% newline %}
| {% t "costs" %}
| {{ cy_value | currency }}
| {{ py_value | currency }}

{% endstripnewlines %} 

output:
Screen Shot 2021-02-17 at 14.03.17

now, instead of using registers like $0 and $1, we are going to use plain simple named variables.

Before the table, we’ll do this:

{% comment %}vars to assign totals to{% endcomment %}
{% assign total_cy = 0 %}
{% assign total_py = 0 %} 

And then for each section, we will assign the variable to itself and add the part that needs to be added to it:

{% newline %}
| {% t "goods" %}
| {{ cy_value | currency }}
  {% assign total_cy = total_cy+cy_value %}
| {{ py_value | currency }}
  {% assign total_py = total_py+py_value %} 

In fact, this is what a register does too, but the difference here, is that you immediately know which variable holds which value now, which is easier coding and better maintainable :muscle:

Complete snippet:

{% comment %}vars to assign totals to{% endcomment %}
{% assign total_cy = 0 %}
{% assign total_py = 0 %}

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

{% comment %}section A{% endcomment %}
{% assign acc_range = "60" %}
{% assign cy_value = period.accounts | range:acc_range %}
{% assign py_value = period.minus_1y.accounts | range:acc_range %}

{% newline %}
| {% t "goods" %}
| {{ cy_value | currency }}
  {% assign total_cy = total_cy+cy_value %}
| {{ py_value | currency }}
  {% assign total_py = total_py+py_value %}

{% comment %}section B{% endcomment %}
{% assign acc_range = "61" %}
{% assign cy_value = period.accounts | range:acc_range %}
{% assign py_value = period.minus_1y.accounts | range:acc_range %}

{% newline %}
| {% t "costs" %}
| {{ cy_value | currency }}
  {% assign total_cy = total_cy+cy_value %}
| {{ py_value | currency }}
  {% assign total_py = total_py+py_value %}
  
{% comment %}TOTAL{% endcomment %}  
{% newline %}
| TOTAL
|^  {{ total_cy | currency }}  ^
|^  {{ total_py | currency }}  ^
{% endstripnewlines %}
1 Like

Thanks Sven

1 Like

Is there actually any advantage to using a regiser instead of a variable? Performance or something? (what’s the reason Silverfin supports registers?)

Hi @Wsteppe,

Using one or the other does not affect the performance of the code/template.
With registers, you reduce the number of lines you need to code but as Sven explained above, the advantages of using variables outweigh the benefits of registers.
We still support them for historical reasons as they are used in some old templates.

Best,
Borja

1 Like

Makes sense, thanks!

1 Like