We're now going to talk about
the consolidate conditional expression refactoring.
A common situation in code is that you have a set
of conditionals with the same result. What that means that
sometimes the code contains a series of conditional checks in which
each check is different, yet the resulting action is the
same. In these cases, the code could be improved by combining
the conditionals using, for example, and, and or, as connectors. So
as to have a single conditional check, with a single result.
At that point you can also extract those conditional into a
method. And replace the conditional with a call, to debt matter consolidating
the conditional code in this way can make the checks clearer
by showing that you're really making a single check rather than multiple
checks, and extracted that condition and having that matter instead of
a condition can clarify your code by explaining why you're doing a
given check, rather than how you're doing it. You can see an
example of that situation in this code, which is the disabilityAmount method.
As the name of the method says, the purpose of this code
is to compute the disability amount for a given, for example, employee.
And there is a set of initial checks in the methods whose
goal is to decide whether there's this disabilityAmount should be instead zero.
And as you can see, there's multiple conditions. For example, there's a
check about the seniority level, and about the number of months that
the employee's been disabled. So far, whether the employee is part time
and the outcome of all these check is always the same. If they're
true, if the check is satisfied then there is no disability
amount. So the disabilityAmount is zero. So what I will do
if I apply the consolidate conditional expression to this matter, is
that I will take these three conditionals. I will put them together
by saying basically that if seniority is less than 2 or
monthsDisabled is greater than 12 or isPartTime is true then the
return should be zero. And once I have this combined conditional,
as I see here, I will just extract that into a method.