-Let's now see a related refactoring, which is the - -2
-decompose conditional refactoring. What happens here is that in some - -3
-cases, the complexity of the conditional logic in a program - -4
-can make a method hard to read and understand. Specifically - -5
-we might have one or more particularly complex conditional - -6
-statements. And similar to what we discussed for the previous - -7
-refactoring, the conditional, if it's too complex, might tell you - -8
-what happens, but obscure why it happens. To address this - -9
-issue, you can do a similar thing to what we did in - -10
-the previous refactoring. You can transform the condition into a method and then - -11
-replace the condition with a call to that method. And if you give - -12
-the right name to the method, as we saw in the last example, - -13
-that can make the code much clearer and much easier to understand. In - -14
-addition here you can also do something else. Let's assume that those are - -15
-the then and else part of the conditional are complex. We can do - -16
-the same thing with them. So what we can do, we can modify - -17
-the then and else part of the conditional by extracting the corresponding - -18
-code, making also that one into a method, suitably naming the method, - -19
-and then having the call to the method only in the then - -20
-and else part of the conditional statement. So let's see how that works - -21
-with an example. Here we have the matter that computes some charge. - -22
-And it computes the charge based on some corrective uses of the date - -23
-that is provided as input, or it's imagined or is just, you - -24
-know, one of the fields in the class. So as you can see, - -25
-there is a conditional here that checks that if the - -26
-dates is before the beginning of the summer, so before summer - -27
-start. Or it's after the summer end. Then it compute - -28
-the charge using some winterRate. Otherwise, if we are in the - -29
-summer, it will compute the quantity, the charge using a - -30
-summerRate. And this is just a small example, so it might - -31
-not look that complex. But, you know, just project this on - -32
-more realistic code, on larger code. You can end up with - -33
-the conditions that are hard to understand. And even in this case, even - -34
-such a small piece of code, you have to kind of look at - -35
-the conditionals, figure out what does it mean for the date to be - -36
-before the summer start and after the summer end. We can make this much - -37
-clearer. So, how can we do it? By applying this refactoring as we - -38
-described. Let's see what happens when - -39
-I apply the decompose conditionals refactoring to - -40
-this method. The first thing I will do is to take this condition, - -41
-create a method that perform exactly the same check, give it a meaningful name. - -42
-In this case I called it notSummer, which is pretty self-explanatory, and then - -43
-replacing the condition with a call to that matter. As you can see - -44
-here, there's a clear improvement in the code, because here I just need - -45
-to look at this statement and I see right away that the check. What - -46
-the check is doing is just checking whether the date is in the - -47
-summer or not. So, much easier than having to interpret this condition. And - -48
-the second thing I do is to take the code that computes the - -49
-charge and also in this case, creating - -50
-suitable methods that compute the winterCharge and - -51
-the summerCharge. And I called them exactly winterCharge and summerCharge which - -52
-again is self explanatory. And then I replace this computation with - -53
-a call to that method. So again, when I look at - -54
-this code, I can clearly see that the charge is computed - -55
-using some sort of winterCharge calculation and then using some sort - -56
-of summerCharge calculation. And if I don't want to know how - -57
-this is exactly computed, that's all I need to know to - -58
-understand what this method does. Easier and faster than looking at - -59
-this method and figuring out what it does. And if - -60
-I need to look at the details, exactly like in the - -61
-previous case, I can just go and look at the - -62
-implementation of winterCharge and summerCharge. But I will be looking at - -63
-that in its specific context. So, without having to understand - -64
-everything at once. So in this way, you make it clear - -65
-both why you're doing something, because it is notSummer, and - -66
-what exactly you're doing. You're computing a winterCharge, or a summerCharge. -- cgit 1.4.1