1
00:00:00,030 --> 00:00:04,650
Hey guys! Welcome back to a 100 Days of Code. Today

2
00:00:04,680 --> 00:00:06,720
we're going to look at debugging,

3
00:00:07,020 --> 00:00:10,380
the process of removing bugs from your code.

4
00:00:10,920 --> 00:00:12,600
If you manage to get to this point,

5
00:00:12,690 --> 00:00:15,480
you've probably written quite a bit of code already,

6
00:00:16,200 --> 00:00:20,280
and I will bet that you've probably made some mistakes, some typos,

7
00:00:20,310 --> 00:00:24,060
some errors and some bugs. So in this lesson,

8
00:00:24,090 --> 00:00:29,090
I want to talk about some techniques and tips for how to find bugs and how to

9
00:00:30,000 --> 00:00:32,790
get rid of them from your code. Now,

10
00:00:32,790 --> 00:00:37,710
the first documented bug was actually found by this lady, Grace Hopper.

11
00:00:38,220 --> 00:00:43,220
She was probably one of the first programmers and one of the pioneers of the job

12
00:00:43,380 --> 00:00:46,830
that we're undertaking right now. Now in her notes,

13
00:00:47,250 --> 00:00:51,060
you'll find a moth that's been taped to the notebook,

14
00:00:51,720 --> 00:00:56,370
and this was found in a relay that was preventing her code from running

15
00:00:56,370 --> 00:00:59,400
properly. And this was pretty much the first

16
00:00:59,400 --> 00:01:03,540
actual case of a bug that's been found in a computer.

17
00:01:04,230 --> 00:01:07,140
Even though we're not dealing with moths these days anymore,

18
00:01:07,530 --> 00:01:12,530
we're still going to end up finding bits of things in our code that prevent the

19
00:01:13,110 --> 00:01:15,810
code from doing the things that we want it to.

20
00:01:16,380 --> 00:01:20,700
The important thing to remember is don't feel down when you've created a bug

21
00:01:21,000 --> 00:01:25,680
because everyone gets bugs. And when I say gets bugs,

22
00:01:25,710 --> 00:01:30,630
I mean, we create them, right? So once you've gotten over this, next,

23
00:01:30,690 --> 00:01:35,370
I want to talk about some of the tips and techniques that you should follow on

24
00:01:35,370 --> 00:01:39,240
how to quickly be able to identify and remove these bugs.

25
00:01:39,750 --> 00:01:42,720
So the first step is to describe the problem.

26
00:01:43,170 --> 00:01:47,760
If the problem is messy and it's not well understood in your head,

27
00:01:48,090 --> 00:01:50,160
then it's almost impossible to debug it.

28
00:01:50,460 --> 00:01:55,460
So untangled the problem and try to make sense of what's going on. To begin,

29
00:01:56,040 --> 00:02:00,360
go ahead and fork the starting repl for today's code.

30
00:02:00,990 --> 00:02:04,860
Now, the first thing to notice here is that I've written a bunch of code in the

31
00:02:04,860 --> 00:02:06,000
starting repl

32
00:02:06,300 --> 00:02:10,259
and we're going to be uncommenting these blocks one by one and eventually

33
00:02:10,259 --> 00:02:12,480
solving all of the debugging problems.

34
00:02:13,650 --> 00:02:18,240
So the first thing you're going to do is to uncomment the first block of code.

35
00:02:19,110 --> 00:02:20,700
Now in this block of code,

36
00:02:20,730 --> 00:02:24,660
we're going to practice describing the problem to help us solve this debugging

37
00:02:24,660 --> 00:02:27,480
problem. Now if you take a look at this function

38
00:02:27,690 --> 00:02:30,510
you can see that at some point in the function,

39
00:02:30,540 --> 00:02:33,300
we're supposed to print this line out into the console.

40
00:02:34,050 --> 00:02:38,190
But if we go ahead and hit run, you can see that nothing gets printed.

41
00:02:38,910 --> 00:02:40,530
So what's going on here?

42
00:02:41,220 --> 00:02:46,220
I want you to take a look at this function and describe the actual problem.

43
00:02:46,740 --> 00:02:51,150
What is the for-loop doing? When is the function meant to print "You got it"?

44
00:02:51,600 --> 00:02:56,340
What assumptions are you making about the value of i? So pause the video,

45
00:02:56,730 --> 00:02:59,830
read the function and have a go at describing the problem.

46
00:03:02,200 --> 00:03:02,680
All right.

47
00:03:02,680 --> 00:03:07,680
So what we've got here is we've got a function that loops through all the

48
00:03:08,080 --> 00:03:10,240
numbers between 1 and 20,

49
00:03:10,780 --> 00:03:15,220
and then once that number reaches 20, it's supposed to print "You got it".

50
00:03:15,820 --> 00:03:19,660
But the problem is that when i reaches 20,

51
00:03:19,780 --> 00:03:22,390
it doesn't print this line into the console.

52
00:03:23,290 --> 00:03:27,610
So now that we've described the problem and we understand what's going on,

53
00:03:28,000 --> 00:03:32,620
let's see if we can solve it. If we think about this problem,

54
00:03:32,680 --> 00:03:35,770
when i reaches 20, it doesn't print out this line.

55
00:03:36,130 --> 00:03:37,600
There's an assumption in there, right?

56
00:03:37,960 --> 00:03:41,860
The assumption is that i will definitely reach 20.

57
00:03:42,640 --> 00:03:46,240
But if you think back to how the range function works,

58
00:03:46,840 --> 00:03:51,190
the stop or the upper bound is actually omitted.

59
00:03:51,580 --> 00:03:53,740
So when you write range(4),

60
00:03:54,100 --> 00:03:57,610
it actually produces a number from 0 up to 3.

61
00:03:58,570 --> 00:04:01,690
And in our case, when we write (1, 20),

62
00:04:02,140 --> 00:04:06,280
it actually goes from 1 all the way up to 19

63
00:04:06,400 --> 00:04:08,410
but not including 20.

64
00:04:09,130 --> 00:04:14,050
The problem here is that i actually never reaches 20.

65
00:04:14,380 --> 00:04:19,089
So this assumption is completely false. Pause the video

66
00:04:19,180 --> 00:04:23,680
and see if you can fix the code so that this line actually gets printed.

67
00:04:25,270 --> 00:04:29,830
All right. So all we need to do is change this to 21 instead of 20

68
00:04:30,220 --> 00:04:34,090
and now when we hit run, you can see it actually works

69
00:04:34,120 --> 00:04:38,450
and we managed to debug this problem. Always when you come across a problem

70
00:04:38,480 --> 00:04:39,313
in your code,

71
00:04:39,340 --> 00:04:43,600
try to describe it so that you really understand what the issue is,

72
00:04:44,110 --> 00:04:49,110
and then test your assumptions and see which of those assumptions is actually

73
00:04:49,750 --> 00:04:50,290
false.

