02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Condensing Code

Not exactly a mandatory phase in our game development but it just looks to easy not to do it. In this video we will take our getLine function and refactor it and condense it.

Video Transcript

00:00All right, so already we're doing better, right? So we found our up and we found our down
00:05All you left for us is to wrap this up and actually save this information
00:09So we'll actually know the initial dot will know the final dot so we'll be able to start combining
00:17These elements together and figuring out the next step. All right, so to do that
00:21We're gonna take advantage of that line variable that I've created
00:25Now what I want to do is I want to create here some sort of like package or
00:29Little packager that's just gonna instead of all these traces. We're gonna create a little package here. It's gonna be a line
00:35object
00:37Which is going to have a few properties. It's gonna have two properties. Well, it's gonna have two properties
00:42the first property is gonna be the
00:44the current direction so the direction and
00:49You know what?
00:50One thing that we could also look into in this thing
00:53Because all of our application here and this is gonna make our code look a little bit harder, but that's kind of exciting
00:59So let's let's before we even go into these changes of the lines. Let's try to make our code
01:05Smarter, but the much harder to read what will make it smarter? First of all if we could
01:11Subtract this if statement because basically what's happening here and what's happening here is the exact same thing
01:18The only difference between the two is that we're focused on the Y here while we're focused on the X here
01:25That's really the only difference
01:27Now to be able to really leverage this all I really need because I always know that if I could get rid of this if
01:34Statement I could cut my for my for statement to just have this now
01:39Instead of sending my X Boolean value here
01:42We could send into a directly X as a string or Y as a string. So that could be our direction
01:49So I could say here if X equals zero
01:53If X equals zero we already learned that if X is not changing that we want to test our Y
01:58so if our X is not changing we could already just send a string of a Y if
02:03Our Y is not if it's not that then we're gonna send our X so this is our two different scenarios
02:07So now instead of having a Boolean it could be here our direction which is going to be a string
02:14All right. So now we that we have a direction this is gonna look so bad, but we're gonna do it anyways
02:19So first of all, I'm gonna copy this just in case cuz I'm sure you're gonna want to play around with these values
02:24Cuz it does look it's gonna look really really hard, but I like things that are not tightly condensed
02:31It just makes things easier because then we just subtracted it if statement which I'm always happy to subtract an if statement if possible
02:37So I'm just gonna
02:39Toss that code right here. So you guys could access it later on
02:43and now let's just take this if statement that we have here and
02:48We could just ask ourselves
02:50We could just change this code here to just reflect this change that we've just did
02:55So first of all, we could take this direction and we could use a very very cool way of coding instead of using here
03:01y dot X and
03:04We could see that we have here a little bit of
03:07Switcher rules. So first of all based on the direction we have your two different directions. We have here the Y direction
03:12We have heard the X direction. So we need to give them a couple of names
03:17So first of all, we have our direction. So we know that our direction is our Y
03:23We're testing to that Y
03:25But at the other hand, we want to also change things in the X
03:28So we have to create another variable that will probably be the opposite so we could call it just the or var
03:34Opposite which is going to be a string as well. And that will equal if the direction equals Y
03:42It will equal X
03:45If not, it's gonna equal Y and if you're wondering if you're wondering so what make why would this be more efficient than in the loop is
03:53Because this happens only once when we call the get line
03:57Well anything we do inside of the loop happens over and over each time we do a loop
04:01All right, so we know the direction we know our opposite of the direction
04:06So all we have left to do is we could just any place that we have your a dot Y
04:10We could change it to be the direction
04:12Doing this just simply instead of having a dot notation just opening and closing a bracket and just putting inside our direction
04:19This is going to make it look really un-elegible not legible. But at this point because you're already finished with our logic
04:26Are worrying about we're not as worried about about the visuals even though obviously it's not recommended to do this
04:32I'm mainly doing this because it's a really really great way to practice
04:36All the different tools that we've learned so far and if it could cut some code, I'm always happy to cut some code
04:42Alright now opposite direction is gonna be just that just exactly where the X's are we're gonna put the opposite instead
04:50Now hopefully we won't break anything from the process of what we've just done
04:55All we have to do is just we're just tracing one by one going through all the different elements and deleting
05:01Basically changing it now not very legible
05:04Probably very very hard to understand what's going on in here
05:07So obviously you don't really want to do this unless you're very comfortable unless you already know that you're not gonna mess around with it anymore
05:14In our scenario, we're kind of like doing a preventive in advance. So but let's see if first of all
05:21Automatically, we're having already some issues. So first of all, let's
05:25solve them
05:26All right. So get line. First of all, where it's not it's not a Boolean anymore
05:32It's actually expecting for a value that is not a Boolean
05:37now by the way, if you you'll probably notice that
05:43We could have continued and set that Boolean and just change the line here. That would be the same
05:47They'll be fine. So this is really just for the practice because we're really what we're doing here is a double if statement
05:52Which is not necessarily needed
05:55All right
05:57Oopsie because really, you know, let's instead of that let's that is true. So let's just
06:05Let's just take back just for a second just so we could practice one more time because it is a hard change
06:09alright, so I'm just
06:12Going back a bit just bringing everything back to the way it was for a second
06:15And what I want to do is we have the direction I'm not gonna change anything about this direction
06:20I'm gonna we're taking it all the way back. So it's gonna be a Boolean. It's still a Boolean
06:25We haven't done anything. What we want to do is we're gonna go right here and we're gonna just create your two variables
06:31So with these two variables are going to be basically if well, first of all my VAR is going to be
06:40Gonna have two vars one is going to be the direction which is going to be a string and
06:45The other one is going to be opposite direction, which is going to be a string same as we've just done right here
06:51We could ask here if is X
06:54Do one thing
06:56Else do another thing basically we're gonna flip it or this or that this is another way of doing it
07:02If it is X same deal if it is X
07:04What do we want to do we want to have our directions stick to that Y and?
07:11we want our
07:13we want our opposite direction opz to be our X and
07:18If it's opposite, then we just want to do the opposite now, you could choose whichever
07:23tactic
07:23Works better for you
07:25And then we when we get into this code already again
07:27we could just get rid of anything that we could get rid of all these stuff and
07:31Basically condense all of our logic into a smaller a much smaller bucket here that will just contain the information that we need
07:38all right, so
07:40That will basically in the same way that it uh, is is basically going to capture both edges. It's going to capture both directions
07:48all right, so
07:50Let's replace any place that we have your Y. We're gonna place that on deer instead
07:57So let's do this quickly because we've done it already before
08:01And the beauty of this is that you don't really have to understand this and it kind of like a nice thing about this
08:05Oh, so if you're not sharing your code
08:06Well, this is definitely gonna make it impossible for anyone to read this because even when you're building it
08:12You would probably never do this while you're building it
08:14You would only do it after you ready set your logic set and it's really that second pass where you're just condensing
08:20things so
08:21It's probably not the most user friendly thing to do
08:24But it's a really really great experience of knowing how to do these type of stuff
08:28All right, so really what we want to do is we just want to test this out and actually see that it's still working
08:33So just test this out
08:36So first of all if I go to the right
08:39It's still working fantastic if I go to the left
08:45fantastic
08:46Still working beautiful now. Let's check one more thing what happens if we just go a straight line
08:51So two three is our first line
08:56And it doesn't really know how to sort that out
08:58So we're gonna have to figure out a way to sort that out as well in a minute
09:03Alright, let's double check just for a second
09:06Two three is our beginning line and then when it stops
09:11Nothing, okay, so we're gonna sort that out in a minute, but let's first before we even solve such a big issue
09:17Let's move on to the next step which our next step is going to actually create those objects

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!