0:14

In this video I'm going to talk about debugging.

Â People often don't like debugging and part of

Â that is because it can be difficult, right.

Â Well, [LAUGH] my response to that is then don't do it.

Â Alright, well, that sounds silly.

Â But my point here is that there are ways

Â in which you can make it easier on yourself, okay.

Â As you're writing your software, if you think

Â about what you're doing a little bit more carefully.

Â You follow the style guidelines.

Â You write code thoughtfully and you test this code, as you go.

Â You write a function, you test it.

Â Then you write the next function, you test it.

Â Then you start building up code based on other working code

Â instead of building up code based on unknown code, all right?

Â And this decreases the likelihood of bugs or, at least makes it easier to.

Â Find the bugs that you do introduce, because you're

Â look at small well written pieces of code, okay.

Â Now despite your best efforts you're probably

Â still going to have bugs in your software, okay.

Â So you are going to have to debug, okay.

Â Or, worse yet you might be given software of somebody else's that has bugs in it and

Â you've gotta use and you gotta deal with it

Â and it has bugs that you've got to find.

Â You can't imagine how much of Joe's code I have to debug, for instance.

Â Alright, so we have to debug.

Â Well, debugging doesn't have to be painful.

Â Okay, it can be interesting and fun to hunt

Â down these bugs, alright can be little puzzles, alright.

Â But the thing that you have to remember about debugging,

Â the simple most important thing you need, is patience, okay?

Â You have to be thoughtful about what you're doing, okay?

Â You have to think about, What am I doing right now?

Â What is the program supposed to be doing and what is it actually doing, okay?

Â And if you bring this patience to the, to

Â the process, and you think about what you're doing.

Â It can actually be a relatively painless process, or at least a systematic

Â process that allows you to get to the bug without a lot of frustration.

Â 2:01

Debugging takes several steps, right?

Â And the first one is recognizing that a bug actually exists.

Â So how do you do that?

Â Well, you have to actually run your program, and you

Â have to think about what the outputs are supposed to be.

Â And compare them with, to the to what the outputs actually are, right?

Â And when they don't match, well, now you know that there's a bug, okay?

Â But if you skip this step and you only run your program once.

Â Or you don't really thoughtfully test your program,

Â you may never actually realize there's a bug, okay?

Â So this is the important first step here.

Â I'm recognizing that a bug exists.

Â Now, once you know the bug exists that's where the debugging begins, okay?

Â So what do I do first?

Â Well, I need to isolate the source of the bug.

Â I basically need to find out where the bug is, okay?

Â And so this is, you know, an important step here

Â where you have to think about how do I divide up

Â my program in to smaller and smaller pieces and how

Â do I figure out if those smaller pieces are working, okay?

Â And, one of the key elements here is that you have to come up

Â with these hypothesis, you have to say hey I believe this is true, okay.

Â And instead of just saying oh I know it's true, because I looked at

Â the code, and the code, it must be correct, right, you want to confirm this.

Â Alright, so a lot of debugging is about confirming these

Â hypotheses, alright, saying I know this is true, let's confirm it.

Â Alright so I have a little function here that I

Â know works, well, don't just say I know it works rather.

Â Run it with a bunch of different inputs, and confirm

Â that the outputs are exactly what you expect them to be.

Â Then you can rule that piece of code out as having a bug, and move on to the next.

Â All right, and you do the same thing you keep

Â coming up with these hypothesis saying, yeah, this is what I

Â believe to be true about this code, and then you

Â confirm it and then you move on to the next step.

Â And you do that again, and again, and again,

Â and eventually you are able to isolate the source.

Â You are able to say, heh, this function, or these, you know,

Â ten lines of code, or whatever, this is where the bug is okay?

Â 3:51

Then you have to move on to the next step,

Â where you sort of identify the cause of the bug.

Â You actually figure out which line of code is really the problem, or which lines.

Â It doesn't necessarily have to be a single line.

Â Say hey, this is what is breaking my program.

Â This is how it's breaking my program and understanding

Â that, doesn't necessarily mean you know how to fix it.

Â But at least now, you have isolated the source

Â of the bug, you've identified what exactly is causing the

Â bug, and then you can move on to the

Â final stage where you actually fix the it all right?

Â You come up with a new way of implementing the

Â code so that you don't have the bug anymore all right?

Â If you follow these four steps, okay, then you will

Â end up with a more robust, better working program all right?

Â Now these steps are easy to say, they're much harder to do.

Â 4:34

So here's a program I wrote to implement the game math dice, and it doesn't work.

Â Okay so we've already passed step one here.

Â We recognize that there's a bug.

Â So let's talk about what this program actually does.

Â Okay Math dice, I have five dice.

Â Two are 12 sided dice, three are six sided dice.

Â I roll the two 12 sided dice.

Â I multiply them together.

Â That's my target number, that's the number I'm trying to get.

Â Then I multiply the three six sided dice, and I try to put them together to

Â form an equation that gets me a result that is close to the target as possible.

Â Now use the three dice, and I can use add,

Â addition, subtraction, multiplication, division, or

Â exponentiation in between them, alright.

Â And I'm trying to make an equation of the form A op B op C, alright.

Â And, you know, A and B, A op B is

Â in parentheses here for the purposes of this game, okay.

Â So I evaluate A op B first, and then the result there, I do that result op C.

Â Okay?

Â Now, let's play alright?

Â Let's run this program.

Â And run it, okay, so it says the target is 48,

Â because I rolled 6 and 8 on my 2, 12 sided dice.

Â And then my 3, 6 sided dice; I got 5, 6 and

Â 1, and I try to make an equation that got me close.

Â It says, six minus one, plus five, divided by five.

Â That doesn't seem like it's right because, there are

Â too many numbers in there right, and too many operations.

Â So it's not of the form A op B equals C all right?

Â So clearly, I have a problem.

Â 5:59

All right, I know I have a problem, so let's try and find it.

Â Well, let's go down to the bottom here first to the play function.

Â This is actually playing the game.

Â Let's look at what it does.

Â First it rolls two 12-sided dice as the target

Â dice and takes their product and then it rolls three

Â six-sided dice as the other dice that I'm trying to

Â make the equation out of and that prints them out.

Â Let's look at these printouts.

Â Well, the target is 48, rolled six and an eight.

Â Well, that seems like that's reasonable and

Â the regular dice are five, six, and one.

Â Well, that seems like that's reasonable too.

Â So, I believe, that's probably okay.

Â Now I should actually confirm that but for the sake

Â of time in this video, okay, I'm going to skip that step.

Â But you should go off and confirm that, that's working correctly.

Â Next, I make a bunch of equations here, out of the dice.

Â All right, so I have a set of ops plus, minus, times, divided by, and power.

Â And the dice and I make the equation.

Â So first, well I know this is correct, right?

Â But let's print them out.

Â Print equations.

Â Okay, so, let's run this game again.

Â 7:04

okay.

Â Here's the first mistake you might make, okay, trying to debug this,

Â now, when I have this many equations is going to take forever, okay.

Â I'm not going to, I can't really see, are these right, I have no idea, okay.

Â Well, yeah, I can kind of tell right off the bat 4 plus 4 plus 3 plus 3 equals 14,

Â well, that has four dice in it and I

Â only have three dice so, yeah, this is probably wrong.

Â But, the first thing I want do is, you know, sort of cut things down, okay?

Â And I also don't like this randomness okay?

Â This randomness is going to make it so that it does a different thing every time.

Â So, I'm going to actually change this here,

Â 8:40

Okay, yes, now I have many fewer equations,

Â all right, So, now I've cut things down.

Â I've got repeatable behaviors here that inputs are always going to be the same.

Â I have fewer equations that I need to deal with, all right.

Â So, I have some more hope here of figuring out what's gong on, all right.

Â Well this print out that I've go here is not all that useful.

Â Let's print the length of the equations, how

Â many there are and for equations in equations.

Â Print tqn, all right.

Â Hopefully this now, okay, much more readable, much easier to understand.

Â I have eight equations all right and just for fun you know, let's put this back.

Â 9:43

Alright, I know the equations are to big so let's see if i can figure out why.

Â Let's go up here to make equations.

Â Alright here we are making our equations, and first of all we generate all the

Â permutations of the dice and then we generate

Â all the sequences of operations that we have.

Â Alright, well, let's look at that.

Â So what are all the permutations of these dice?

Â Print dice

Â 10:14

One three three one.

Â All that seems reasonable, right?

Â Okay.

Â If I have the dice one and three, all the

Â possible orderings of that is one and three, three and one.

Â All right, fine.

Â Now, let's print ops orders.

Â Okay, so, now I'm trying to get all the sequences of the operations.

Â And remember I cut this down to plus and minus to make my life a little bit easier.

Â Let's see that, alright.

Â We have plus plus, plus minus, minus plus and minus, minus.

Â Well that seems right, doesn't it?

Â 11:04

Okay.

Â And let's scroll back up and yes.

Â I am getting plus, plus, plus, plus, plus, minus.

Â So, it is enumerating all the possibilities, all the sequences that I

Â can get with plus and minus but it's not the right size.

Â Okay?

Â So, I'll put this back to one and three.

Â 11:31

Well, I look through this and, do you see the bug?

Â [LAUGH] Maybe, maybe not.

Â All right, but basically what's happening here is that I now immediately see this.

Â And you may not.

Â You may need to put print statements in here.

Â But again, for the sake of time, I'm going to jump to it.

Â I have a plus one in here that shouldn't be there.

Â [LAUGH] All right?

Â Let's run this now.

Â Okay, now we get plus and minus.

Â 11:56

This seems Okay?

Â I have the single operator.

Â Okay.

Â The only way of doing it, if whether I have

Â plus or minuses, I can either adjust plus or adjust minus.

Â Okay.

Â Well, now I have got a complete, like error, down here.

Â okay.

Â Well that's because I wrote this silly string

Â equation, where I am going to print out the equations.

Â In such a way that it only works if my equation is a certain size.

Â Okay.

Â This is also a bug, right?

Â I should fix this.

Â Okay?

Â But let's ignore it for now, all right?

Â Instead let's go back to three dice and see if everything has been fixed.

Â 12:30

Okay, so now my target looks right, my dice look right.

Â I got all the permutations, the sequences of operators are okay.

Â I now have 24 combinations.

Â When I look at these equations they look reasonable, all right?

Â And the result now though says the closest equation is 1 plus 3 plus 5 equals 9.

Â And let's look in the results here, 1 plus 3 plus 5 equals 9.

Â Is that the best I can do?

Â Well, yeah, it seems like it.

Â 12:59

So we actually walked through a systematic process there to find that bug.

Â So I put things back now and let's see if everything works, okay?

Â Alright.

Â Now our target is 5.

Â We've got the Dice: 3, 6, and 6.

Â And 6 divided by 6 is 1 times 3 is 3.

Â Is that the best I can do?

Â Well, could be.

Â Alright.

Â Here's 42.

Â Now, here I'm not so convinced.

Â That the closest I can get to 42, with 5, 5 and 6, is 1.6.

Â That's clearly not true.

Â I could do 5 plus 5 times 6, all right?

Â That's 60.

Â That's much closer than 1.6.

Â All right?

Â Perhaps you can find something else.

Â Okay so, I don't think it's working yet, right?

Â My equations look okay now though.

Â So I think that I am making an equation and it's printing out

Â nicely 5 + 5 divided by 6 = 1.667, I think it's valued incorrectly.

Â So what's left here, right?

Â Find closest, all right.

Â Well, again, let's do the same thing, all right.

Â Let's make things consistent so I.

Â Don't have randomness going on here.

Â Six, seven, and let's make the dice almost, five, five, six.

Â Let's run that again just to confirm that I'm going to get the same thing.

Â I am.

Â Alright, so let's look at this find closest function.

Â Alright, here we are.

Â Find closest.

Â Now we want to be systematic here.

Â We, I, I know this works, right?

Â I wrote it.

Â Clearly, but letâ€™s confirm how it works.

Â Right?

Â So, you can see what it does.

Â It, you know, sets the closest to B, infinity

Â and then it tries to find things that are closer.

Â So letâ€™s print out what happens here.

Â Letâ€™s print the equation.

Â Every time we find something thatâ€™s closer, letâ€™s print it.

Â 14:38

Okay, 16, 31.

Â No we're that close, we're down at 19 ha, and then we went

Â up to 60, then down, all right, clearly this is not working, right.

Â It's not getting closer and closer and closer to 42, right.

Â I shall only swap something in if it's actually closer

Â to 42, not just bouncing up and down, all right.

Â So, clearly something's wrong here, all right.

Â So, let's, let's print out some more, let's.

Â Trying to figure out what's actually happening

Â so let's print each time the equation.

Â 15:24

which is the abs target minus equation minus 1.

Â Alright.

Â And let's print to blank line each time.

Â Alright.

Â Let's run through this.

Â Okay, I've got a lot of information here now.

Â Let's go back up.

Â Alright.

Â My equation gives me 16 and the closest is infinity.

Â The difference is 26.

Â All right.

Â Okay.

Â I've got another equation.

Â I got four out of the closest currently is 16 which

Â is the last time we saw the difference is 38 all right.

Â 16, 16, 16 so when do we leave 16, here we go.

Â 16 we go to 31, right because 31 was bigger, or closer to 42, rather.

Â Okay, now, here's the problem.

Â Right, right here.

Â Okay, our closest was 31.

Â We see 19, which has a difference of 23, and we take

Â 17:08

So hopefully watching me debug this program

Â helped a little bit in understanding the

Â kinds of processes that you might go

Â through, to successfully debug a program right?

Â Remember the first step is understanding that there actually is a bug, okay?

Â So you do have to run and test your program,

Â and think about the output is it right or not?

Â Okay the second one is isolating the source of the bug and I sort of showed.

Â Hopefully by example.

Â A little bit of how you might go about isolating the source of the bug, okay?

Â You want to take the pieces of your program.

Â You don't want to try and figure it out, from the big giant part of your program.

Â Where you have big giant outputs.

Â You want to narrow down the inputs and the outputs, so they're much smaller.

Â So that you can deal with them.

Â You can look at them, and understand what's going on.

Â Then you want to look at smaller and smaller pieces of your program.

Â Then you want to confirm the truth.

Â You know that it works, you wrote it, but you need to confirm that, okay.

Â Because often it doesn't, right.

Â That's why there's a bug, okay.

Â So you can see that I printed some things out, I sort of looked at what I expected.

Â And I found out things were not the way that

Â I expected them, and that helped me isolate the problem.

Â Okay?

Â Once I've done that, I now can identify.

Â And you know, that's a little bit harder for you

Â to see inside my head, but looking at the code,

Â I can say, oh, yeah, this is the line of

Â code that's actually broken and here's how you fix it.

Â Now the bugs that were in here were relatively simple.

Â Your bugs might be this simple, they might not, okay?

Â But the process remains the same, right?

Â Recognize there's a bug, isolate the source of the problem,

Â identify what the actual problem is, and then fix it.

Â