 Hi everyone, in this video I'm going to be looking at question four from the PDPS workshop. So in this question we are doing some major refactoring and it's to do with creating functions. So if you are, I don't know, programming something and you have two chunks of code or multiple sets of code that are looking very similar, that's often a sign that you can refactor it and just create a function because there's no need to write this code twice when you can just write it once and have two function calls that will save you a lot of time. So if you're in an exam setting and you get two lots of code that look the same, often a sign that you're needing to create a function and there's a really easy process to do that and to do it with doing what the question asks to have as few parameters as possible, giving the parameters meaningful names which we do anyway and that will do the same as the seven lines. So if we have a look at what, I'll comment this out first, what the code does, this is what it looks like. So this is what we want to create in the end. We don't want to change that code, we just want to refactor it and we're going to refactor it by creating this method here called draw ellipse in rect which is what the program's doing and then instead of this code being written twice it will just be one function call. So I'm going to replace that there. So that's a function call for this and this will be the function call for this and then what we want to do is analyze each line of the code, see what's similar and what's different, what's similar will just be put into the function and what's different will be used as a parameter. So let's go line by line and work out what we need because if we do this process then we'll have the smaller amount of parameters that we need, there's no need having a parameter for each of these values because some of them are the same. So let's start with the first line of each. So first line of each we've got stroke weight two and stroke weight four. So that's our first difference, we can see that for each section of code it has a different stroke weight so that is an input that we need for our function. So that's an int so int and then we want a meaningful name, stroke w, stroke weight I guess that's fine and for this one this one will be inputting two and this one will be inputting four. Okay so now I can go stroke weight like that and that will work for both of these now. So we've done this line and we've done this line, sometimes I find commenting it out or deleting it is good because I can just say it's done, don't have to worry about it and then we have a look at the next line, we analyze that one. So this one has a stroke color of red 25500 and this one is the same. So if we want as few parameters as possible there's no point having this as a parameter because it's the same for both. So I can just put that here, no input is needed and I'm finished with that line. Okay next line this one has this RGB code for our fill and this one has a different RGB code here so that will be an input that we need. So we need an R, a G, a B. Some people have gotten around doing an R, a G and a B by creating a color variable but we can just use RGB. So the RGB for this one is those values there so I'll just copy that straight in. The RGB values for this one are these ones here so now that's an input and we can just go fill RGB and that will work for both codes. Great so we're done with that one. Next line, rectimode center, so it's the same mode so we can just put that straight into our function because it's the same. So that's done, that's done. Okay so this rectangle is positioned at 250 and its width and height is 70 and this rectangle's x and y position is 350, 350 with the width and height 70 again. So the width and height here are the same but our x and y positions aren't so that's going to be another input. So I'll go rect x, rect y and then we can put that code here so we've got rect x, rect y, 70, 70 and then we need to, this one needs 250 as its x and y and this one needs 350, 350 as its x and y and we are done with that one. Next we've got fill 255 so that's the same, copy that straight in and we're done. Great and now we've got, this one has ellipse 250 and its width and height is 50, 50, this one here the width and height is the same but its position is not so that will be like this and then 50, 50 and this one will need the inputs here and this one will need the inputs here. Great so we've finished with all of this but we're not finished with this function because we've used a lot of parameters here what we want to do is have a look and see if any of the values are the same and that way we can eliminate some of those parameters. So I can see in this function call here the number 350 is written four times which seems pretty unnecessary and on this function call here the combination of 250 is written twice. So when we have a look back at our code we can see that the x and y position of our rectangle in our ellipse is the same in both scenarios so instead of having different x and y variables for each shape we can just create the one x and y variable just to reduce the number of parameters we're using so let's change it to x pos and y pos let's remove those ones y pos and then we can remove some of these so that eliminates two there which looks good and great I don't think I can see other than this scenario but that's just a coincidence that the x and y are the same and so we're finished here so we can remove that code and making these decisions is tricky like making the decision to have the x and y position of the rectangles and the ellipse the same is sometimes something that people don't pick up because they're thinking okay but what if I want to do this function again and for some reason I want my ellipse to be somewhere else so we don't know how this code is going to be used again so in that situation what you want to do especially in your exam in real life it's probably a bit different that in the exam you just want to pretend that this function is only being called twice and only worry about what's similar and different in these two instances even though you know someone might decide why I don't want the stroke to be red I want it to be green just assume that that's not going to happen because then you can have as few parameters as possible same with this situation here someone might say well but what if later on I want to change the size of my rectangle my ellipse maybe that's a variable I need just assume in the context of this exam that that's not going to happen and that's the trick there um yeah so if you have a look here if you're comparing maybe I'll copy this over into a new hard on each herb a new ellipse window here when you're comparing these two bits of code this one is more I mean there's more information we know that we're drawing an ellipse in a rectangle with these variables as their positions so not only have we condensed the amount of code we're written we've written by removing repetitive code but we're actually more informed on what's happening because we have a function that tells us we have variables that have meaningful names so I'm just going to double check that we've done this correctly oops probably should move that over so this is the code from this one and this is the code from the left one so they are the same which means that we've refactored it correctly if you get something different then maybe you've refactored something incorrectly um but if you follow this process of going line by line comparing the similarities and differences you should pick up or you should have code that works the same way just more refactored yeah so that's what you want to do for question four