1
00:00:02,250 --> 00:00:04,950
<v Instructor>Now, here are a couple of examples.</v>

2
00:00:04,950 --> 00:00:07,270
And let's start with this example

3
00:00:07,270 --> 00:00:10,120
where I have a save user function,

4
00:00:10,120 --> 00:00:14,440
which takes an email and a password, so two parameters.

5
00:00:14,440 --> 00:00:18,110
And where we then create a user object on the fly.

6
00:00:18,110 --> 00:00:21,040
This is all written in Java script, by the way.

7
00:00:21,040 --> 00:00:24,070
And in this object, I save the email and the password,

8
00:00:24,070 --> 00:00:27,480
I also add a randomly generated new ID,

9
00:00:27,480 --> 00:00:29,870
which is not a perfect unique ID by the way,

10
00:00:29,870 --> 00:00:31,940
but good enough for this example.

11
00:00:31,940 --> 00:00:34,040
And then we reach out to some database,

12
00:00:34,040 --> 00:00:37,070
as it seems we could have also named this database,

13
00:00:37,070 --> 00:00:39,250
of course, and insert this user

14
00:00:39,250 --> 00:00:42,530
into a user's table or collection.

15
00:00:42,530 --> 00:00:43,780
Now don't get me wrong.

16
00:00:43,780 --> 00:00:46,830
This function is absolutely not horrible

17
00:00:46,830 --> 00:00:48,390
and if you have code like this,

18
00:00:48,390 --> 00:00:50,500
it's not the end of the world.

19
00:00:50,500 --> 00:00:52,660
Still, if you call this function,

20
00:00:52,660 --> 00:00:54,400
it would look something like this.

21
00:00:54,400 --> 00:00:58,090
And you probably would have to take a closer look

22
00:00:58,090 --> 00:01:01,570
at the function to understand that it wants an email address

23
00:01:01,570 --> 00:01:04,180
and a password, and that the email should be the first

24
00:01:04,180 --> 00:01:06,520
and the password should be the second argument.

25
00:01:06,520 --> 00:01:08,140
Again, not the end of the world

26
00:01:08,140 --> 00:01:11,580
and definitely also not totally unexpected.

27
00:01:11,580 --> 00:01:13,740
Still, when we write code like this,

28
00:01:13,740 --> 00:01:17,080
we might be able to come up with a better solution.

29
00:01:17,080 --> 00:01:20,410
For example, we could come up with a function like this,

30
00:01:20,410 --> 00:01:22,320
which only takes one argument,

31
00:01:22,320 --> 00:01:25,580
which is the already finished user.

32
00:01:25,580 --> 00:01:29,610
So we would outsource this task of creating the user

33
00:01:29,610 --> 00:01:32,870
out of this function into another place and code.

34
00:01:32,870 --> 00:01:35,860
So obviously this only works if we construct a user

35
00:01:35,860 --> 00:01:38,560
ahead of time, but then we have a simpler

36
00:01:38,560 --> 00:01:41,520
save user function, which ultimately of course,

37
00:01:41,520 --> 00:01:43,550
also is very easy to call.

38
00:01:43,550 --> 00:01:46,420
Still it takes one argument but now I would say

39
00:01:46,420 --> 00:01:48,930
it's easier to understand and call

40
00:01:48,930 --> 00:01:51,160
and it doesn't leave us guessing

41
00:01:51,160 --> 00:01:53,730
whether email or password should be first

42
00:01:53,730 --> 00:01:57,000
and which kind of values this function might want.

43
00:01:57,000 --> 00:02:00,250
Now an even better solution could be this one here.

44
00:02:00,250 --> 00:02:04,470
Here, we have a class user which acts as a blueprint

45
00:02:04,470 --> 00:02:08,240
for a new user objects, which then has a safe method,

46
00:02:08,240 --> 00:02:11,090
which can be called without any arguments.

47
00:02:11,090 --> 00:02:14,170
Therefore, user.safe is very easy to read

48
00:02:14,170 --> 00:02:16,600
and very clear and understandable.

49
00:02:16,600 --> 00:02:20,050
And instead, we now outsourced the problem

50
00:02:20,050 --> 00:02:23,550
of passing in values for the email address and the password

51
00:02:23,550 --> 00:02:25,890
to the user constructor function.

52
00:02:25,890 --> 00:02:28,950
And there you could argue it's a bit more expected

53
00:02:28,950 --> 00:02:33,020
and obvious than on a safe user function.

54
00:02:33,020 --> 00:02:35,550
As I mentioned, that also wasn't horrible,

55
00:02:35,550 --> 00:02:38,200
but this code might make even more sense

56
00:02:38,200 --> 00:02:40,960
and might be even easier to understand.

57
00:02:40,960 --> 00:02:43,330
And therefore, this acts as an example

58
00:02:43,330 --> 00:02:47,960
of how you could transform code to be easier to understand

59
00:02:47,960 --> 00:02:52,300
and to not use any arguments on certain parts of your code,

60
00:02:52,300 --> 00:02:54,780
where arguments might not be obvious

61
00:02:54,780 --> 00:02:57,620
and the order could be unclear.

62
00:02:57,620 --> 00:03:00,570
I also got one other example of a function

63
00:03:00,570 --> 00:03:04,080
that takes no arguments, and that would be this one here.

64
00:03:04,080 --> 00:03:07,080
Let's say we have a is logged in state,

65
00:03:07,080 --> 00:03:08,680
which we managed somewhere

66
00:03:08,680 --> 00:03:12,540
and then we have a function which just changes that state.

67
00:03:12,540 --> 00:03:14,750
Therefore this function does something,

68
00:03:14,750 --> 00:03:17,660
but it simply needs no arguments for that.

69
00:03:17,660 --> 00:03:22,120
It just inverts this is logged in state all the time.

70
00:03:22,120 --> 00:03:24,490
So that would be another example of a function

71
00:03:24,490 --> 00:03:27,030
without any arguments, because in this case

72
00:03:27,030 --> 00:03:28,950
it doesn't need any.

73
00:03:28,950 --> 00:03:30,360
So these are some examples.

74
00:03:30,360 --> 00:03:33,900
And again, I wanna emphasize that this original solution

75
00:03:33,900 --> 00:03:35,630
still isn't horrible,

76
00:03:35,630 --> 00:03:39,500
but maybe the other solutions we derived make more sense

77
00:03:39,500 --> 00:03:41,763
or improve readability even more.

