At this point, we have added a lot of new files, some of which have duplicate code. Let’s take some time to refactor them and make our code a little more manageable.
I don't know about you but I'm tired of writing out the response for our system. 0:00 I want to do a little refactoring in this video 0:05 to extract this out into its own function. 0:07 Refactoring improves the internal structure of your code 0:10 without changing the external behavior. 0:13 If you find yourself writing the same thing over and over, it's probably a good 0:16 indication that you have an area of code that could benefit from refactoring, 0:21 making it easier to maintain that code in the future in a lot of our files. 0:26 We have three lines that are repeated all over the place. 0:31 Why don't we extract this into a redirect you know functions file. 0:34 Let's copy the lines from our vote.php file and 0:39 move them into our functions file. 0:42 We'll create a new function and name it redirect. 0:46 Then we'll pass in the path. 0:52 Now we can change books.php to the path. 1:04 Now let's change the vote file to substitute our new redirect function that 1:09 we just created. 1:13 Redirect to books. 1:22 And then remove the last two lines. 1:25 Let's test out our voting once more just to make sure that everything still works. 1:28 Great. 1:38 Why don't you take the next steps on your own and 1:39 change all of the three redirects to our new redirect function. 1:42
You need to sign up for Treehouse in order to download course files.Sign up