So in this lesson, I want to cover a pretty important topic in terms of developing on the ICP, which 1
is this idea of orthogonal persistance. 2
The idea of persistance is being able to hold on to state over many different cycles and updates. 3
So, for example, if we had our canister and we created a variable, let's call it a and set it equal to 4
42, which is the meaning of life and everything. 5
Now if then I decide to increment a by 5, 6
so at this point, the state that's held inside the container a is equal to 47. 7
Now this is the state. 8
But what happens once I update the code and I rerun the canister or I redeploy it? 9
Well, in a normal situation, unless we explicitly tell it to add 5 to a again, the variable 10
a is going to be reset to its original state. 11
Now in a canister on ICP, that doesn't have to happen and instead, it actually can hold on to the state 12
of your variables, your collections, your objects, basically the entire state of your container. 13
And the reason why it's called orthogonal is because we as a developer don't even have to think about 14
it. 15
It can happen just behind the scenes as a function of the way that the Internet computer is created. 16
So I want to show you this in a normal situation. 17
So here I've got a code sandbox up and I'm just writing some JavaScript code. 18
So again, we're going to say var a equals, let's call it 15. 19
And now if I decide to console.log it, 20
you can see that the state of a is 15. Our console.log is basically a way for us to just expose the 21
state to be able to be printed out so that we can actually see it because we are not the computer, 22
we're not inside the box. 23
So now again, as I said, I'm going to plus equals, so I'm going to increment a by five. 24
So now the state of a at line 9 is no longer 15, 25
and if I go ahead and just copy this line of code here, you can see at this point it's now 20. 26
We've added 5 to it. 27
Now, here's the interesting thing. 28
If I go ahead and get rid of this add a 5 and reload our sandbox, then you can see that the state 29
of a has been completely restored to the beginning. 30
It is not held on to the fact that it turned 20 and instead goes back to being 15. 31
This is how normal programs work and this is something that we're used to. 32
So in order to hold on to state in between these cycles, we actually have to use data persistence, and 33
that will involve databases and putting things in databases, taking things out of databases, etc. 34
But let me show you what we can do using orthogonal persistence. 35
Firstly here you can see that we've got our currentValue set to 300 and I'm going to go ahead and comment 36
out this line of code, because this line of code says currentValue should be set to a new value of 37
100, which we don't want. 38
We actually just want to create a new variable that holds the number 300. 39
Now, if I go ahead and hit save and deploy and go over to my Candid UI and we can use our previous 40
checkBalance method to quickly query what is the value of the currentValue, which is 300. 41
Okay. 42
So nothing special there. 43
Now, if we go back and I go ahead and top up the value, so let's go ahead and add 100 to that, hit 44
call. 45
And then once that's done, we hit query. 46
You can see it is now increased by 100 to 400. 47
So 300 to 400. 48
Now, at this point, if I go ahead and just change some minor thing in my code, let's say we added 49
a new line, 50
Debug.print, doesn't really matter what you do as long as it's something that actually requires the 51
container to update and then we deploy our canister again. 52
If we come back and we hit query again, you can see that it gets reset back to 300. 53
So this is the normal behavior that we saw with other programming languages as well. 54
But we can actually add in persistance to our code with just a single keyword. 55
We can add the word stable in front of the var, and this turns as variable into a persisted variable. 56
So it's a stable variable 57
now. Previously without the stable keyword, this was what they call a flexible variable. 58
So because in normal programming, it's not very usual for state to be persisted across update cycles 59
etc., the default is still a normal variable that you would get on any other programming language. 60
But adding in the stable keyword now upgrades our variable to a orthogonally persisted variable. 61
So let's hit save, 62
let's go ahead and hit deploy again. 63
Let's go back to our Candid UI and let's see what happens this time. 64
So query starts out at 300. 65
Now let's go ahead and top it up by 100. 66
So now it's up to 400, right? 67
Remember that number? 68
That's the state of this variable. 69
Now, let's again go ahead and change some line of code and then go ahead and force our container to 70
deploy again to update the container. 71
So we hit save and we deploy again. 72
So this is basically the same as hitting refresh on our code sandbox or simply getting the process to 73
stop and restart. 74
So now that we've deployed our canister, if I go ahead and hit query, see what happens. 75
It's held on to the state of that variable. 76
It still knows that it should be 400, which means that this state can now be used across all update 77
cycles. 78
I don't have to worry about databases, 79
I don't have to think about how am I going to get this number in or how am I going to get it out? 80
I just have to keep programming and with my stable variable it will be persisted across any sort of 81
update, any sort of downtime. 82
If the electricity goes out, it's going to restore it to the previous state. 83
Now, if you're wondering why it is I've commented out this line of code, well, this is because this 84
operator is a replace operator. 85
So whenever this code runs here, it doesn't matter what is the state of currentValue. 86
It will actually go ahead and update it to whatever is on the right-hand side of the equals sign. 87
So our stable variable, when we declare it, we can declare with an initial state. 88
But the next time the code runs and it hits this, it's not going to do what other programming languages 89
do, which is to reset it to the initial value. 90
But if it does encounter this operator, it will set it to whatever this value is. 91
So that means we have to comment it out in order to see the stable variable in action. 92
And this is a really, really magical way of programming applications, which we're going to see more 93
and more in the coming lessons. 94
But this was just a little taster, and I really want you to try it out for yourself, mess around with 95
it and see if you can get it to work for yourself and really wrap your head around it by tweaking it, 96
changing it, and seeing it for yourself with your own eyes.