1 00:00:00,890 --> 00:00:05,030 We have a few more features to add to our pig Latin converter tool. 2 00:00:05,030 --> 00:00:08,500 But first, I want to do some refactoring. 3 00:00:08,500 --> 00:00:13,430 Refactoring restructures the code without changing its behavior. 4 00:00:13,430 --> 00:00:19,310 I don't like having this long list of else if statements, so I want to change that. 5 00:00:19,310 --> 00:00:24,380 I could use a switch statement, but I like having early returns in my methods. 6 00:00:24,380 --> 00:00:28,770 As soon as a condition is met, I want to return the results. 7 00:00:28,770 --> 00:00:34,494 At the end of our first conditional block, we can return $newWord. 8 00:00:36,110 --> 00:00:42,637 Now instead of an else if, I can make this second conditional a separate conditional. 9 00:00:45,163 --> 00:00:49,400 And return $newWord. 10 00:00:49,400 --> 00:00:52,562 Now I can remove this else entirely. 11 00:00:58,221 --> 00:01:02,705 If I make it past the first two checks, just perform these actions and 12 00:01:02,705 --> 00:01:04,290 return the $newWord. 13 00:01:04,290 --> 00:01:07,820 This should not change the behavior of our code. 14 00:01:07,820 --> 00:01:11,210 So our test should still pass, let's give it a try. 15 00:01:13,430 --> 00:01:15,990 Sure enough, our test still passed. 16 00:01:15,990 --> 00:01:19,730 Refactoring is a very handy way to keep your code updated 17 00:01:19,730 --> 00:01:22,080 as we learn better ways to do things. 18 00:01:22,080 --> 00:01:27,180 By having test for functionality, we can be confident that a refactor of one 19 00:01:27,180 --> 00:01:32,310 section of code will not break the rest of the application that's using that code. 20 00:01:32,310 --> 00:01:36,887 I could refactor this further and change these three lines into a single line. 21 00:01:39,720 --> 00:01:47,560 We could return the substring plus the first three letters, and ay. 22 00:01:51,790 --> 00:01:59,594 Return the substring, The first two letters and 23 00:01:59,594 --> 00:02:05,540 ay, And return substring. 24 00:02:06,790 --> 00:02:09,217 The first letter and ay. 25 00:02:14,108 --> 00:02:15,040 And run our test again. 26 00:02:16,540 --> 00:02:18,450 Great, everything still passes.