1
00:00:00,720 --> 00:00:06,090
Now let's talk about the most important rule there is about clinical functions.

2
00:00:06,390 --> 00:00:09,990
It's to keep them fought, to keep them little.

3
00:00:09,990 --> 00:00:16,770
Smaller functions are the key for clinical, for mental ability, for scalability, for everything.

4
00:00:17,100 --> 00:00:21,960
There is a little quote I want to say to you that the first rule of function is that they should be

5
00:00:21,960 --> 00:00:22,590
small.

6
00:00:22,950 --> 00:00:26,520
The signal of function is that there should be more than that.

7
00:00:26,700 --> 00:00:32,670
So it really influences the fact that they need to be smaller and for them to be small.

8
00:00:32,730 --> 00:00:34,170
There is several stuff that we can do.

9
00:00:34,350 --> 00:00:39,000
The first one is that we have to ensure that they are doing one thing.

10
00:00:39,240 --> 00:00:46,770
For example, this little code right there, it's the function log in as first it login in the database.

11
00:00:46,770 --> 00:00:49,860
For example, we have a database like Superhighways or something else.

12
00:00:50,340 --> 00:00:55,830
Any give us an access token and then we save this access token and then we redirect the user and we

13
00:00:55,830 --> 00:00:56,250
notify.

14
00:00:56,250 --> 00:00:59,010
It's notified that it's a success.

15
00:00:59,010 --> 00:01:00,200
He's login.

16
00:01:00,840 --> 00:01:06,150
So what is wrong with this thing is that this function don't do one thing.

17
00:01:06,420 --> 00:01:08,070
It will tell me yes it does free.

18
00:01:08,220 --> 00:01:10,640
It's no more or we cannot but just one line in the function.

19
00:01:10,860 --> 00:01:11,790
That's not what I mean.

20
00:01:12,000 --> 00:01:17,760
I mean, that's there need to be one thing in the in their name with the goal of their name here.

21
00:01:17,760 --> 00:01:18,960
The goal is to log in.

22
00:01:19,230 --> 00:01:20,410
So it's log indexes.

23
00:01:20,430 --> 00:01:26,460
But getting saving ducks is looking to keep the decision of the user so it's normal and then it should

24
00:01:26,460 --> 00:01:27,660
be direct and notifier.

25
00:01:28,200 --> 00:01:31,740
This thing is not part of the log in goal.

26
00:01:31,920 --> 00:01:35,130
The logging goal is not to redirect the user to look and go in just to log in.

27
00:01:35,140 --> 00:01:37,110
The user eats an extra.

28
00:01:37,110 --> 00:01:38,310
It should not be there.

29
00:01:38,520 --> 00:01:43,140
It means that the function do log in and redirect a redirect.

30
00:01:43,260 --> 00:01:44,790
And this is not the name of our function.

31
00:01:44,910 --> 00:01:49,020
The name of the function is just a logging, not a logging and a redirect.

32
00:01:49,230 --> 00:01:51,840
So this path here will not be in this function.

33
00:01:51,900 --> 00:01:57,360
It will be in a purpose up in the function submit form, for example, but not in this scope.

34
00:01:57,360 --> 00:02:01,500
So this thing, we did it because it's not it should not be there basically.

35
00:02:01,620 --> 00:02:08,100
And here we have a function that does one thing and it's great because it's small shirt and it will

36
00:02:08,100 --> 00:02:14,580
be more easily to change refactor and to use it because when we use it, we will not have to always

37
00:02:14,580 --> 00:02:15,480
redirect the user.

38
00:02:15,720 --> 00:02:17,370
So it's way better that way.

39
00:02:18,300 --> 00:02:25,620
The second way to make a function smaller is by trying to extract another function from it.

40
00:02:25,860 --> 00:02:30,600
So for example, here I will uncomment this little code right there.

41
00:02:30,810 --> 00:02:33,120
Then we have a function submit log in.

42
00:02:33,570 --> 00:02:34,770
Yeah, it's well done.

43
00:02:34,920 --> 00:02:40,770
And this function is using the log in that we use and then is using the redirect to in the defined user

44
00:02:40,890 --> 00:02:43,920
that we put in the purpose as I told you.

45
00:02:43,920 --> 00:02:48,870
So this is where we put the thing, it was inside here we remove from log in and we put them in the

46
00:02:48,870 --> 00:02:49,620
purpose group.

47
00:02:49,740 --> 00:02:52,260
So it was the function submit logging.

48
00:02:52,920 --> 00:03:00,210
So in this function we will try to extract another function and we will see if we can, how we can know

49
00:03:00,210 --> 00:03:01,620
if we can extract or not.

50
00:03:01,620 --> 00:03:07,680
So it is by taking a group of line, a group of instructions, and by trying to find a name for them,

51
00:03:07,680 --> 00:03:09,150
a name for a function for them.

52
00:03:09,510 --> 00:03:14,700
So for example here my first step is to submit log in is to a checking the validity of the field.

53
00:03:14,710 --> 00:03:20,430
So this type of line here, can I put this group of in function and if I put them in the function,

54
00:03:20,430 --> 00:03:23,160
what will be the name of this function I can find?

55
00:03:23,160 --> 00:03:26,310
I can start to think I'm trying to validate some stuff.

56
00:03:26,310 --> 00:03:29,610
So it will be validate log into fields for example.

57
00:03:30,000 --> 00:03:35,820
And at this moment precisely you stop everything and you do it because you find a name that is proper,

58
00:03:35,970 --> 00:03:39,360
validate logging fields and it will work basically.

59
00:03:39,510 --> 00:03:51,840
So you just do it at function, validate, validate, log in fields up in barometer, username and password,

60
00:03:52,410 --> 00:03:54,720
and you just copy the code that you have.

61
00:03:54,840 --> 00:04:01,920
And now this function validate log in fields, you put it right there up and putting user and password

62
00:04:02,460 --> 00:04:05,400
in parameters and know your function submit.

63
00:04:05,400 --> 00:04:08,370
Logging A has become more little.

64
00:04:08,430 --> 00:04:12,450
It's become more effort shorter so it's way better that way.

65
00:04:12,690 --> 00:04:18,840
And we can see that we check the validity of the routine fields, we do the log in, we reject and we

66
00:04:18,840 --> 00:04:19,590
notify user.

67
00:04:19,680 --> 00:04:24,030
And if it don't work, then we notify the user that there was an error of course.

68
00:04:24,270 --> 00:04:29,100
So here the function is already you can see really more easy to read.

69
00:04:29,100 --> 00:04:31,560
It is really easy because it's basically text.

70
00:04:31,560 --> 00:04:33,780
It's text that you read and it's way better that way.

71
00:04:34,140 --> 00:04:42,600
And another way of saying this extraction as a function, it's bypassing the level of abstraction extraction

72
00:04:42,600 --> 00:04:43,170
levels.

73
00:04:43,530 --> 00:04:45,900
What is an extraction abstraction level?

74
00:04:46,170 --> 00:04:54,750
It's kind of a hidden, hidden wall or stuff like this that we put on the code to not see the details

75
00:04:54,750 --> 00:04:56,250
of an implementation.

76
00:04:56,430 --> 00:05:00,020
So here, for example, in the function submit login, I need to know.

77
00:05:00,110 --> 00:05:02,000
That doesn't mean looking to do the meditation.

78
00:05:02,000 --> 00:05:02,300
Yes.

79
00:05:02,690 --> 00:05:05,440
But do I need to know how this validation is done?

80
00:05:05,930 --> 00:05:07,130
Not for sure.

81
00:05:07,550 --> 00:05:11,570
When I'm in the I don't need to always to know how validation is done.

82
00:05:11,750 --> 00:05:17,480
So it's okay if the radiation is hidden in another in another function.

83
00:05:17,600 --> 00:05:24,620
It's okay to put this level of abstraction in the validation, because the setting login is not only

84
00:05:24,620 --> 00:05:26,690
about this, it's about several steps.

85
00:05:27,050 --> 00:05:31,820
So I need to know the steps, but I don't need for the implementation of those steps.

86
00:05:32,000 --> 00:05:34,430
So this is the limit of abstraction.

87
00:05:34,550 --> 00:05:40,820
And by doing that, by doing a lot of assumptions, you will be able to have shorter function.

88
00:05:41,120 --> 00:05:44,030
So this is really an important concept to remember, too.

89
00:05:44,240 --> 00:05:51,350
So to keep the function short, do only one thing and extract a function to create a level of abstraction.

