Now, let's get back to our pizza calculator. 1
Essentially, what we want is to be able to specify the pizza in inches that we want to buy because of some 2
special offer or because some pizzas only come in certain sizes, 3
whatever it may be, 4
and we also want to specify the number of people to feed. 5
So let's say that this was, I don't know, 6
we had six people coming over, 7
right? 8
And we'll also specify another constant which is called slicesPerPerson. 9
So according to Domino's, the world experts in pizza, the ideal slice of per person is apparently three. 10
Now, I like to be a little bit more generous than that, especially for myself. 11
So let's say that everybody gets five slices of pizza. 12
So, now I want to be able to set all of these variables, and then calculate how many pizzas I should 13
buy. 14
So I'm going to create anotherr computed property called numberOfPizza. 15
And this, of course, is going to be an integer because you can't buy half a pizza, at least not where I'm 16
from. 17
And we're going to create a getter that is going to compute how many pizzas we need to buy whenever we 18
try to access this property numberOfPizzas. 19
And this is pretty easy because we can say that numberOfPeopleFedPerPizza is equal to 20
the numberOfSlices divided by the slicesPerPerson. 21
And then in order to figure out the number of pizza we need to buy, we can simply return 22
the number of people to feed 23
divided by numberOfPeopleFedPerPizza. And now our computed property is a read-only property because 24
it only has a getter, 25
it doesn't yet have a setter. But it means that we can simply printout numberOfPizza for the current 26
settings. 27
So these are 12-inch pizzas that we're buying. 28
We've got six people in the house and everybody is going to eat 5 slices of pizza. 29
So down here we've got the result. We need to buy six pizzas. 30
Simple, right? 31
So if at a later date, we decide to change some of these stats, say. we're buying larger pizzas or buying 32
16-inch pizzas, and we've actually got a lot more people coming, 33
we've got 12 people in the house, and everybody only wants 4 slices, because I don't know why. 34
Now, in this case, when our code updates, we didn't have to call a method or update anything or, you know, 35
run some piece of code again, 36
is we've-- 37
without doing anything, 38
when we try to access numberOfPizza, the getter gets called and our code dynamically works out how many 39
pizzas we need to buy. 40
Now, what about the opposite situation? 41
Say, we have a bunch of leftover pizza in the fridge and we want to know how many friends we can invite 42
over to play Mario Kart. So we can use a setter 43
in this case to work out the number of people we can feed. 44
So instead of just having a getter, so a read-only property, we're going to add a setter as well. 45
And we're going to say let totalSlices = numberOfSlices multiplied by newValue. So, remember 46
that previously, we learn that newValue is simply the newValue that this property is going to be set to. 47
And this is always spelt in this way and you can't change how it's spelt or how it's written because 48
it's predefined in the setter. 49
So, now that we've got the total numberOfSlices, we can change the numberOfPeople to equal 50
the totalSlices / slicesPerPerson. 51
So, now we have to change the number of people to a var, so that it can be modified. And we can print 52
the numberOfPeople as we change the numberOfPizza. 53
So let's say that we've got four pizzas left at home. 54
Now, at this stage, I'm saying number of pizza equals four. 55
So I'm setting numberOfPizza and this block of code will trigger, and we're going to calculate the 56
numberOfPeople that we can feed with four pizzas that are 16 inches. And that is 12 people, 57
so I can invite 12 people over with all of my pizzas in my fridge which is pretty neat to know. 58
So through using computed properties and getters and setters, we've been able to write this pizza calculator 59
in a very simplified way. And you can see that our code updates dynamically whenever we change the values 60
of its dependence. 61
So, for example, if I change the numberOfPizza, I get a new numberOfPeople that can be fed, and this means that 62
all I have to change are these three values up here, and everything else, 63
I don't need to touch ever again. 64
And that minimizes errors, makes our code more dynamic and more maintainable, and easier to understand. 65
So that is the beauty of computed properties. 66
And so, essentially, all that we're doing is we've basically just implemented a spreadsheet where we change 67
any of the dependent variables, say, the pizza size, then everything else will get updated accordingly 68
depending on this change.