Instructor: Now in the last lecture,
I talked about these levels of abstraction.
And maybe it's already clear, at least to a certain extent,
how exactly that's all meant to work and how that helps you.
But it can also be intimidating.
You might not be sure whether you can always determine
if there are mixed levels of abstraction or not.
Now that's no problem though.
Of course, for one, that is also something
that comes with experience,
and it already is a good first step
if you think about your code
and you at least consider splitting it,
even if you then make the wrong decision.
I mean, what is the wrong decision anyways?
It's your code and you are trying your best
to make it as readable as possible.
But in addition, I also got a rule of thumb for you,
or actually two rules of thumb,
which help you decide whether to split your code or not.
You wanna split a function,
or at least consider breaking up a function
into multiple functions, if you can extract code,
multiple lines of code, that work on the same functionality.
For example, if you have a function which calls
both setAge and setName, you could consider merging this
into one function, a new function,
or a new method in this case,
which you outsource from that function
in which it was originally.
So that in your original function,
you now only have to call one other function,
which then does the actual work
of setting an age and a name.
So whenever you have an opportunity like this
to remove a block of code from one function
and put it into another function,
you could consider doing that.
The other rule of thumb which I have for you
is that you want to extract code
that requires more interpretation than the surrounding code.
And that again is directly related
to these levels of abstraction.
If we have a look at this good old example here,
then we can easily see that this email.includes check
requires more interpretation from our side
than having a look at saveNewUser.
There it's directly obvious what this does
and why we're doing this.
For email.includes, we have to add the extra meaning
of email validation being done here.
So changing it to this code would bring it all back
onto the same level of abstraction and on the same level
of required interpretation, you could say.
Because now we can easily read this code
and the interpretation is all baked into the names.
So you can also just have a look at these rules
when it comes to deciding whether you wanna split or not.