Presenter: So now we added guards.
We leave aside the fact that we have a bunch of
if statements here, we should see another problem
with this code though.
And that is the mixture of levels of abstraction.
What we learned about functions still applies.
This is inside of a functional after all.
And when it comes to simplifying controlled structures
and managing deeply nested controlled structures,
splitting up functions and extracting controlled structures
into functions is another important building block.
Now this starts with the checks here.
We can run the checks here in line
but we could also outsource this into a separate function
which then also would allow us to use positive wording.
For example, here we are interested in whether
we have transactions or not.
So we could grab this code, cut it
and add a new function
below this process transactions function.
And we could name this function isEmpty
and then simply return the result of this check
of course ensure though that we get transactions as a value.
So isEmpty will return true
if we have no transactions object
or if we have no elements in transactions.
And I named this is empty and not has no elements
because of this prefer positive checks rule here,
it's simply is easier to read if we use a positive statement
than a negative one.
So if I named this is empty,
I can simply check here in process transactions,
if isEmpty transactions,
passing transactions to isEmpty.
And this is super easy to read.
If it's empty transactions, then we do this.
Now of course, speaking of mixed levels of abstraction,
we do have more violations of that rule.
For example, also in this console log statement,
it's not directly related to the if statement
and to simplifying the if statements
but of course we might also wanna extract that
so we can grab this console log statement
and add a number of function here,
showErrorMessage could be the function name
where we get the message as a parameter
and where we then simply output the message like this
and then we could call, showErrorMessage here
and forward this message which we wanna show to again
have an equal level of abstraction here.
Now of course, extracting these two functions
didn't remove the if statement,
it also didn't shorten this code here
but it makes it a bit more readable
and overall that is our end goal still.
Nonetheless, when we talk about functions,
it's not just about extracting Boolean checks
and deriving booleans or showing messages,
it's also about extracting entire controlled structures
from our code.
And that's therefore what we're going to do next.