1
00:00:02,420 --> 00:00:04,720
So now that we know what pure functions are,

2
00:00:04,730 --> 00:00:10,910
let's dive into factory functions. Pretty unrelated to pure functions but still an important concept.

3
00:00:10,970 --> 00:00:17,620
The idea behind factory functions is that you have a function that produces another function,

4
00:00:17,630 --> 00:00:22,540
now that might sound strange but it makes more sense once we see an example.

5
00:00:22,700 --> 00:00:29,450
Let's say we're building a web application, some accounting software where as part of that software, we

6
00:00:29,450 --> 00:00:32,860
need to show various tax amounts

7
00:00:33,140 --> 00:00:40,190
and of course as you know, we got different taxes based on which amount we're working with, for our

8
00:00:40,190 --> 00:00:47,420
revenue we might need the value added tax, for our profit before tax we might need the income tax or the

9
00:00:47,420 --> 00:00:52,520
corporate tax. Now the tax calculation typically always is the same.

10
00:00:52,520 --> 00:01:01,070
If we had a function, calculate tax, then we probably would have a function that receives a tax rate and

11
00:01:01,160 --> 00:01:06,710
an amount and then we just return amount times tax,

12
00:01:06,740 --> 00:01:12,980
something like that, that could be a function that calculates a tax, of course it's a trivial function

13
00:01:13,220 --> 00:01:15,090
but you get the idea.

14
00:01:15,140 --> 00:01:24,860
Now the problem is if we have our value added tax amount, we might need to call calculate tax with our

15
00:01:24,860 --> 00:01:26,150
amount, 100

16
00:01:26,420 --> 00:01:35,810
and the tax rate of let's say 90%, so 0.9. For our income tax, we might need to call calculate tax

17
00:01:35,810 --> 00:01:41,060
with an amount of 100 and 0.25 or whatever.

18
00:01:41,060 --> 00:01:46,640
Now of course it's fine to do it like that and because it's a trivial function, it definitely seems doable

19
00:01:46,880 --> 00:01:48,560
to call it like that

20
00:01:48,560 --> 00:01:54,230
but what if you needed that amount in different parts of your software? Then you need to call the functions

21
00:01:54,230 --> 00:01:57,740
with these tax rates in different parts of the software.

22
00:01:57,740 --> 00:02:00,480
Now that's where a factory function can be helpful.

23
00:02:00,560 --> 00:02:08,420
You would create another function which you could call create tax calculator, so I try to make it clear

24
00:02:08,420 --> 00:02:13,710
by the function name that this function will return something which then does something for us

25
00:02:13,850 --> 00:02:17,440
and in this function, we create another function, as you learned

26
00:02:17,480 --> 00:02:23,540
you can have functions in functions, so we can move that calculates tax function into the create tax

27
00:02:23,540 --> 00:02:27,520
calculator function. So now we have that function in there

28
00:02:27,540 --> 00:02:34,680
and now the interesting thing is the create tax calculator function could take our tax parameter and

29
00:02:34,680 --> 00:02:38,100
we can therefore actually remove it from that internal function

30
00:02:38,100 --> 00:02:43,110
because of that scope thing too which I'll come back in the next lectures as well, a function in a

31
00:02:43,110 --> 00:02:47,400
function can access everything which is available outside of that inner function,

32
00:02:47,400 --> 00:02:52,940
so that tax parameter which we have in the outer function can be used in the inner function as well

33
00:02:53,070 --> 00:02:58,740
and therefore now here, we have our tax calculator function as part of that outer function.

34
00:02:58,740 --> 00:03:03,550
Now here in the outer function, we can return calculate tax

35
00:03:03,600 --> 00:03:05,900
and I don't return the result of it,

36
00:03:05,940 --> 00:03:06,500
so I don't

37
00:03:06,510 --> 00:03:07,310
execute it,

38
00:03:07,380 --> 00:03:10,770
instead I just return a pointer at this function.

39
00:03:10,770 --> 00:03:12,930
Now what's the benefit of that?

40
00:03:12,930 --> 00:03:20,010
Well now we return a function which expects an amount as an input but which doesn't need the tax percentage

41
00:03:20,130 --> 00:03:21,420
as an input.

42
00:03:21,420 --> 00:03:31,590
So now what we can do is we can create a vat calculator or name it calculate vat amount and that

43
00:03:31,590 --> 00:03:39,090
simply is a call to create tax calculator where we pass in the vat percentage and then we might have

44
00:03:39,090 --> 00:03:49,660
our income tax amount which we create with create tax calculator, .25 as an argument.

45
00:03:49,660 --> 00:03:56,260
So now what I in the end store here and I should name this calculate income tax amount,

46
00:03:56,290 --> 00:03:58,500
so what I store here are in the end

47
00:03:58,510 --> 00:04:05,460
two functions, this constant holds a function, it holds this inner function configured for this percentage

48
00:04:05,650 --> 00:04:11,830
and this here also holds the inner function but configured with a different percentage because I call

49
00:04:11,830 --> 00:04:13,500
the outer function twice,

50
00:04:13,510 --> 00:04:18,140
so it runs separately two times, with different arguments for the tax,

51
00:04:18,160 --> 00:04:23,980
so different values for the tax argument and therefore this inner function is recreated two times,

52
00:04:24,190 --> 00:04:26,780
once for every execution of the outer function

53
00:04:26,920 --> 00:04:33,850
and every time it logs in the tax percentage of that outer function which differs for the two executions

54
00:04:33,850 --> 00:04:39,880
and therefore the function which we return is this preconfigured inner function with the logged in

55
00:04:39,970 --> 00:04:42,450
tax amount and hence

56
00:04:42,460 --> 00:04:50,260
now if I console log calculate vat amount for 100 and then again I repeat this for let's say

57
00:04:50,290 --> 00:04:56,270
200, I just need to call this calculate vat amount function like this without passing in the

58
00:04:56,290 --> 00:05:02,770
value added tax percentage over and over again because I configured this once here with the help of

59
00:05:02,770 --> 00:05:06,100
this factory function and we're good to go thereafter.

60
00:05:06,130 --> 00:05:10,890
So if we save that and we reload, I get my different value added tax amounts.

61
00:05:11,500 --> 00:05:14,170
So that's the idea behind factory functions,

62
00:05:14,170 --> 00:05:20,620
if you have a certain function which you call multiple times in different parts of your app and it can

63
00:05:20,620 --> 00:05:23,170
be preconfigured in a certain way

64
00:05:23,260 --> 00:05:29,200
so that calling it is easier and you don't need to repeat yourself as often because our second argument

65
00:05:29,230 --> 00:05:35,500
would always be the same here if we would expect two arguments as we did earlier, in such a case, a factory

66
00:05:35,500 --> 00:05:39,370
function can be convenient and can save you time and code.
