Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Now that we have a navigation that has a list of our subjects and a list of our pages, I'd like us to take a moment to talk about refactoring that navigation. Refactoring is a programming concept that I think it's important to introduce you to. The definition of code refactoring is, revising existing code to change its structure or its appearance but without changing its behavior, It's going to do the exact same thing. We're just going to revise it in place. It's essentially a fancy word for saying let's improve our code. We don't just revise our code for no reason. We need to have specific goals in mind when we refactor, and you can also think of these goals as the criteria for deciding if your revised code is an improvement over the old version or not. So, what are those goals? Well, we revise code for simplicity, for clarity, for maintainability, for efficiency, and for flexibility. And under flexibility, I would say that come in two categories. That's reusability, so that we're flexible in how we use it. And also extensibility, that it can be extended to do more things. So, these are our goals, this is what we want to do with our code to make it better.
And if we can make changes to our code so it gives the same results, but yet it's more simple or more maintainable, then that's better code. That's going to be better for us in the long run. So, let's look at our navigation and see how we can refactor it. So, let's open up manage_content, and I just want to note here, we did a touch of refactoring earlier when we did confirm_query. You'll remember that we took confirm_query, we moved it into our functions, so that we didn't have to repeat ourselves all the time. We could have that each and every time, and it makes it more maintainable because we can come back here and make changes to how we confirm queries.
And we can just do it in the function, and then it'll trickle out to the rest of our pages. Functions are often great ways to refactor code, because the code does the same tasks but in a different location that's out of our way. It becomes reusable and it also gets a function name that helps clarify the codes purpose. I don't know about you, but I find confirmed query to be a lot easier to read and understand what it does than to look at this code here. If not result set die. This makes it really clear what I'm trying to do, what my purpose is, so the code's more maintainable and it's more flexible.
So, let's look now at this list here we've got where we're finding all of these subjects. Let's take that and put that into a function. A function that we can call, and then it will return a result set to us. So, let's start a function over here and let's call this one function find all subjects. We go plural, and I'm just going to grab everything that was in here. I don't need those PHP tags. I'm just going to cut. I'm already inside PHP. I'll paste it right in there. So now, find_all_subjects, we'll assemble a query it will perform the query on a connection and then, confirm the query on the subject set.
So, we need to do a couple things different when we're working with functions. We need to return a value, let's return the subject set. And, notice that we have connection here. So, we need to either pass this value in as an argument to find all subjects, or we can just call global on it here. So, getting the global connection, bringing it in from the global scope so that we can use it. And this is a perfectly appropriate place to use global. To grab that connection because we're just going to use it in our query and then we're done with it. We're not going to try to change the values or anything like that.
So, now we have find all subjects, that's a nice little function. So, over here instead of having all that code before, we can not just say subject set equal find all subjects, see how that works? Now I think that's a good refactor. It took all of this SQL that we were working with here and it got it out of our way. We don't need to be worried about it. What we care about is the fact that we're finding all subjects. So let's do that. Let's just label it, find all subjects. And you want to know what this does? It finds all subjects and it assigns them to subject set. Then they're available to me. Let's do the same thing for the pages.
You've got all of this page query right here. Let's just take that same thing out, we just copy it real quick, actually I'll cut it and let's jump over here. And let's make ourselves a new function, function find all pages and then let's paste that in. Now, wait a minute. We're not actually trying to find all pages. That's not what we really want to do. What we really want to do is find pages for a subject. Find pages for subject, and we need to provide the subject that we're looking for. We have subject id that was passed in before.
Well, we don't want to use this, we don't want to look for the key inside of an array. Instead, let's actually just pass in subject id, so we're going to expect an integer to come in here. Then we'll need our global connection, just like we had before. Now instead of subject id, this is going to be subject id. We've got our connection. We confirm our page set. And then the very last thing of course is return that page set to us. Okay. Let's save it. Now let's come back over here. I'm just going to copy this. And let's paste it in here.
And we're going to set page set equal to it. And notice, this page set has nothing to do with the value that (INAUDIBLE) here. This is a local variable that only has scope inside the function. This is one that I'm defining in the global scope, so it's fine to reuse it. Now, I can't, though, call it subject id here. I need to actually put in the value for that. And we had that before, that was inside the associative array for the subject. Bring up its id. So there, find pages for this subject with this subject id. And guess what, it returns a page set to me, but then I'm ready to iterate through.
See how much slimmer this is now? Look at my page, it's much, much cleaner, it's much clearer what it's doing. I think that's a good refactor. Of course, for a refactor to work, it has to do the same thing, without breaking anything. It has to have the same functionality. So, let's try it, let's see. Let's go back here to manage content, let's reload the page. And sure enough, it does the exact same thing, there's no difference in what it does. And yet, now, I have some reusable functions over here. Whenever I want to find all subjects, I have a function that does that for me. Whenever I want to find the pages that belong to a subject, I have a function that does that for me. That's the basic idea behind refactoring.
We're going to be doing refactoring a lot throughout the rest of this tutorial. So, when you use that term just know that we're going to be changing our code to make it better. To improve it with the goals in mind of simplicity, clarity, maintainability, efficiency and flexibility.
Get unlimited access to all courses for just $25/month.Become a member