Now let's talk about the most important rule there is about clinical functions.
It's to keep them fought, to keep them little.
Smaller functions are the key for clinical, for mental ability, for scalability, for everything.
There is a little quote I want to say to you that the first rule of function is that they should be
small.
The signal of function is that there should be more than that.
So it really influences the fact that they need to be smaller and for them to be small.
There is several stuff that we can do.
The first one is that we have to ensure that they are doing one thing.
For example, this little code right there, it's the function log in as first it login in the database.
For example, we have a database like Superhighways or something else.
Any give us an access token and then we save this access token and then we redirect the user and we
notify.
It's notified that it's a success.
He's login.
So what is wrong with this thing is that this function don't do one thing.
It will tell me yes it does free.
It's no more or we cannot but just one line in the function.
That's not what I mean.
I mean, that's there need to be one thing in the in their name with the goal of their name here.
The goal is to log in.
So it's log indexes.
But getting saving ducks is looking to keep the decision of the user so it's normal and then it should
be direct and notifier.
This thing is not part of the log in goal.
The logging goal is not to redirect the user to look and go in just to log in.
The user eats an extra.
It should not be there.
It means that the function do log in and redirect a redirect.
And this is not the name of our function.
The name of the function is just a logging, not a logging and a redirect.
So this path here will not be in this function.
It will be in a purpose up in the function submit form, for example, but not in this scope.
So this thing, we did it because it's not it should not be there basically.
And here we have a function that does one thing and it's great because it's small shirt and it will
be more easily to change refactor and to use it because when we use it, we will not have to always
redirect the user.
So it's way better that way.
The second way to make a function smaller is by trying to extract another function from it.
So for example, here I will uncomment this little code right there.
Then we have a function submit log in.
Yeah, it's well done.
And this function is using the log in that we use and then is using the redirect to in the defined user
that we put in the purpose as I told you.
So this is where we put the thing, it was inside here we remove from log in and we put them in the
purpose group.
So it was the function submit logging.
So in this function we will try to extract another function and we will see if we can, how we can know
if we can extract or not.
So it is by taking a group of line, a group of instructions, and by trying to find a name for them,
a name for a function for them.
So for example here my first step is to submit log in is to a checking the validity of the field.
So this type of line here, can I put this group of in function and if I put them in the function,
what will be the name of this function I can find?
I can start to think I'm trying to validate some stuff.
So it will be validate log into fields for example.
And at this moment precisely you stop everything and you do it because you find a name that is proper,
validate logging fields and it will work basically.
So you just do it at function, validate, validate, log in fields up in barometer, username and password,
and you just copy the code that you have.
And now this function validate log in fields, you put it right there up and putting user and password
in parameters and know your function submit.
Logging A has become more little.
It's become more effort shorter so it's way better that way.
And we can see that we check the validity of the routine fields, we do the log in, we reject and we
notify user.
And if it don't work, then we notify the user that there was an error of course.
So here the function is already you can see really more easy to read.
It is really easy because it's basically text.
It's text that you read and it's way better that way.
And another way of saying this extraction as a function, it's bypassing the level of abstraction extraction
levels.
What is an extraction abstraction level?
It's kind of a hidden, hidden wall or stuff like this that we put on the code to not see the details
of an implementation.
So here, for example, in the function submit login, I need to know.
That doesn't mean looking to do the meditation.
Yes.
But do I need to know how this validation is done?
Not for sure.
When I'm in the I don't need to always to know how validation is done.
So it's okay if the radiation is hidden in another in another function.
It's okay to put this level of abstraction in the validation, because the setting login is not only
about this, it's about several steps.
So I need to know the steps, but I don't need for the implementation of those steps.
So this is the limit of abstraction.
And by doing that, by doing a lot of assumptions, you will be able to have shorter function.
So this is really an important concept to remember, too.
So to keep the function short, do only one thing and extract a function to create a level of abstraction.