WEBVTT

0
00:00.050 --> 00:00.560
Hey, guys.

1
00:00.560 --> 00:03.560
Welcome back to 100 Days of Code.

2
00:03.680 --> 00:10.610
Today we're going to look at Debugging, the process of removing bugs from your code.

3
00:10.670 --> 00:15.710
If you've managed to get to this point you've probably written quite a bit of code already.

4
00:15.860 --> 00:22.610
And I will bet that you've probably made some mistakes, some typos, some errors, and some bugs.

5
00:22.640 --> 00:30.260
So in this lesson, I want to talk about some techniques and tips for how to find bugs and how to get

6
00:30.260 --> 00:32.020
rid of them from your code.

7
00:32.290 --> 00:37.780
Now, the first documented bug was actually found by this lady, Grace Hopper.

8
00:37.900 --> 00:44.320
She was probably one of the first programmers and one of the pioneers of the job that we're undertaking

9
00:44.320 --> 00:45.220
right now.

10
00:45.400 --> 00:51.310
Now, in her notes, you'll find a moth that's been taped to the notebook.

11
00:51.370 --> 00:57.160
And this was found in a relay that was preventing her code from running properly.

12
00:57.200 --> 01:03.770
and this was pretty much the first actual case of a bug that's been found in a computer.

13
01:04.010 --> 01:10.430
Even though we're not dealing with moths these days anymore, we're still going to end up finding bits

14
01:10.430 --> 01:16.100
of things in our code that prevent the code from doing the things that we want it to.

15
01:16.130 --> 01:23.740
The important thing to remember is don't feel down when you've created a bug because everyone gets bugs.

16
01:23.740 --> 01:27.850
And when I say gets bugs, I mean we create them, right?

17
01:28.090 --> 01:34.240
So once you've gotten over this next, I want to talk about some of the tips and techniques that you

18
01:34.240 --> 01:39.430
should follow on how to quickly be able to identify and remove these bugs.

19
01:39.460 --> 01:42.910
So the first step is to describe the problem.

20
01:42.910 --> 01:50.010
If the problem is messy and it's not well understood in your head, then it's almost impossible to debug

21
01:50.010 --> 01:50.250
it.

22
01:50.250 --> 01:54.450
So untangle the problem and try to make sense of what's going on.

23
01:54.990 --> 02:00.780
Now, in this block of code, we're going to practice describing the problem to help us solve this debugging

24
02:00.780 --> 02:01.500
problem.

25
02:01.530 --> 02:07.260
Now if you take a look at this function, you can see that at some point in the function, we're supposed

26
02:07.260 --> 02:09.720
to print this line out into the console.

27
02:09.990 --> 02:14.610
But if we go ahead and hit Run you can see that nothing gets printed.

28
02:14.790 --> 02:16.930
So what's going on here?

29
02:17.140 --> 02:22.600
I want you to take a look at this function and describe the actual problem.

30
02:22.630 --> 02:24.670
What is the for loop doing?

31
02:24.670 --> 02:26.560
When is the function meant to print?

32
02:26.560 --> 02:27.520
You got it.

33
02:27.520 --> 02:30.670
What assumptions are you making about the value of i?

34
02:31.360 --> 02:36.250
So pause the video, read the function and have a go at describing the problem.

35
02:38.110 --> 02:38.800
All right.

36
02:38.800 --> 02:46.650
So what we've got here is we've got a function that loops through all the numbers between 1 and 20,

37
02:46.650 --> 02:50.760
and then once that number reaches 20, it's supposed to print,

38
02:50.760 --> 02:51.660
"You got it",

39
02:51.660 --> 02:58.740
but the problem is that when i reaches 20, it doesn't print this line into the console.

40
02:59.160 --> 03:05.490
So now that we've described the problem, and we understand what's going on, let's see if we can solve

41
03:05.490 --> 03:05.790
it.

42
03:06.450 --> 03:12.060
If we think about this problem when i reaches 20, it doesn't print out this line.

43
03:12.060 --> 03:13.890
There's an assumption in there, right?

44
03:13.890 --> 03:18.390
The assumption is that i will definitely reach 20.

45
03:18.510 --> 03:27.480
But if you think back to how the range function works, the stop or the upper bound is actually omitted.

46
03:27.480 --> 03:33.990
So when you write range 4, it actually produces a number from 0 up to 3.

47
03:34.540 --> 03:43.930
And in our case, when we write 1, 20, it actually goes from 1 all the way up to 19, but not including

48
03:43.960 --> 03:44.920
20.

49
03:44.950 --> 03:53.920
The problem here is that i actually never reaches 20, so this assumption is completely false.

50
03:54.310 --> 04:00.040
Pause the video and see if you can fix the code so that this line actually gets printed.

51
04:01.280 --> 04:01.760
All right.

52
04:01.760 --> 04:06.110
So all we need to do is change this to 21 instead of 20,

53
04:06.110 --> 04:10.220
and now when we hit run you can see it actually works,

54
04:10.220 --> 04:12.530
and we manage to debug this problem.

55
04:12.620 --> 04:18.590
Always when you come across a problem in your code, try to describe it so that you really understand

56
04:18.590 --> 04:26.600
what the issue is, and then test your assumptions and see which of those assumptions is actually false.