Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,600 --> 00:00:05,000
When your code is full of bugs, you're going to often wonder, why is my code behaving like this?
2
00:00:08,050 --> 00:00:11,380
In this lesson, you're going to learn how to debug using breakpoints.
3
00:00:14,940 --> 00:00:19,350
You can load the code for this lesson by following this path, if you don't see this path inside your
4
00:00:19,350 --> 00:00:23,130
resources, then make sure to download the updated resources from GitHub.
5
00:00:30,320 --> 00:00:35,780
Now, looking at the code, it seems like the intent is to return a random number between one and three.
6
00:00:37,040 --> 00:00:44,150
And so the option variable should randomly equal hits stay or fold, so ideally the code should randomly
7
00:00:44,150 --> 00:00:45,920
print, hit, stay or fold.
8
00:00:46,850 --> 00:00:47,870
But if you're on the code.
9
00:01:00,600 --> 00:01:02,010
It's always going to print Brentford.
10
00:01:03,840 --> 00:01:10,170
How strange, using breakpoints, we can visualize the runtime, so I'm going to add breakpoints everywhere
11
00:01:10,180 --> 00:01:10,680
for now.
12
00:01:20,690 --> 00:01:22,730
I'm going to launch a new debugging session.
13
00:01:34,320 --> 00:01:35,400
Launch current file.
14
00:01:41,300 --> 00:01:44,210
And I'm going to step through every line to visualize what's going on.
15
00:01:45,330 --> 00:01:50,760
All right, and now if you keep restarting the debugging session, notice the random number never equals
16
00:01:50,760 --> 00:01:51,330
one or two.
17
00:01:51,750 --> 00:01:53,400
It's always three three, three, three.
18
00:01:54,970 --> 00:02:01,030
So stop the session, and my first instinct now is that we're not defining the random value correctly,
19
00:02:01,630 --> 00:02:03,220
so remove all the breakpoints.
20
00:02:08,229 --> 00:02:09,850
In a two break points right here.
21
00:02:13,670 --> 00:02:15,530
Now, I'm going to start another debugging session.
22
00:02:20,330 --> 00:02:21,410
And press continue.
23
00:02:22,370 --> 00:02:26,250
And no matter how many times you restart the session, the random value is always three.
24
00:02:26,780 --> 00:02:30,470
This means for sure we're not defining the random value correctly.
25
00:02:40,230 --> 00:02:43,590
Let's do the math if math orendain, returns point one.
26
00:02:44,850 --> 00:02:46,530
And point one equals zero.
27
00:02:50,720 --> 00:02:56,810
OK, what if and returns a high number like point nine, once again, typecasting, point nine first
28
00:02:56,810 --> 00:02:57,950
is going to equal zero.
29
00:02:58,220 --> 00:03:02,720
So no matter what, the random value is going to be three, because the arithmetic operation starts
30
00:03:02,720 --> 00:03:04,760
by typecasting the decimal to zero.
31
00:03:05,420 --> 00:03:08,540
So we're going to do is add brackets to ensure that doesn't happen.
32
00:03:12,030 --> 00:03:16,380
All right, make sure you've already stopped the current session, then launch a new debugging session.
33
00:03:26,060 --> 00:03:27,830
Once again, the value is always three.
34
00:03:31,700 --> 00:03:36,830
Let's do the math again, if not the random returns to point one point, one times one plus three is
35
00:03:36,830 --> 00:03:38,840
equal to three point one typecasting.
36
00:03:38,870 --> 00:03:39,740
That gives us three.
37
00:03:40,500 --> 00:03:45,910
OK, if it returns point nine point nine times one plus three equals three point nine typecasting.
38
00:03:45,920 --> 00:03:47,340
That is going to return three as well.
39
00:03:47,750 --> 00:03:48,140
I see.
40
00:03:48,140 --> 00:03:49,520
What's wrong now, waps.
41
00:03:49,880 --> 00:03:53,780
I should be multiplying the random value by three, then adding one.
42
00:03:58,810 --> 00:04:02,740
All right, make sure to stop the current session and launch a new debugging session.
43
00:04:12,140 --> 00:04:16,100
And perfect, it picks randomly between one, two and three.
44
00:04:17,029 --> 00:04:21,200
Now, stop the debugging session and switch from the debug console to the terminal.
45
00:04:27,310 --> 00:04:30,070
And now run your code to see if that fix our problem.
46
00:04:40,860 --> 00:04:42,280
And it keeps folding.
47
00:04:43,380 --> 00:04:48,420
We know that the random value works perfectly now, so maybe Switch isn't setting the option variable
48
00:04:48,420 --> 00:04:49,590
equal to the correct value.
49
00:04:51,200 --> 00:04:55,070
So we're going at a break point before every case to test this part of our code.
50
00:04:58,340 --> 00:04:59,870
Launch a new debugging session.
51
00:05:04,090 --> 00:05:10,780
Aha, the correct case executes, but switch runs every case that follows the case much, eventually
52
00:05:10,780 --> 00:05:12,640
the value isn't going to be what it should.
53
00:05:15,980 --> 00:05:18,530
So I need to add the breaking word after every case.
54
00:05:26,270 --> 00:05:28,220
All right, longitude debugging session.
55
00:05:32,270 --> 00:05:37,070
And we're good now, so as you can see, great points are a great way to make sure different parts of
56
00:05:37,070 --> 00:05:38,570
your code are working properly.
57
00:05:39,200 --> 00:05:42,830
First, we used the breakpoint to make sure the variable equals random values of one to three.
58
00:05:43,130 --> 00:05:46,730
And then we used break points to make sure the switch statement is working properly.
59
00:05:49,500 --> 00:05:53,970
And now the code should be free of bugs or is it let's rerun the code.
60
00:06:05,990 --> 00:06:10,520
OK, I know that the random variable is fine, the switch statement works properly.
61
00:06:12,200 --> 00:06:18,980
So the if condition must be Iraq at a break point next to the condition, print statements debug the
62
00:06:18,980 --> 00:06:19,400
code.
63
00:06:34,980 --> 00:06:39,660
And now it's clear that the statement runs, no matter what the option is, even though the option is
64
00:06:39,660 --> 00:06:44,640
stay the statement or wrongfully executes the true and runs the code that would Folt.
65
00:06:45,780 --> 00:06:51,240
And so looking at the condition, we can quickly identify the problem, fold should only print the option
66
00:06:51,240 --> 00:06:52,470
that doesn't equal hit.
67
00:06:52,710 --> 00:06:58,040
And if it doesn't equal stay in this case, the operator is not appropriate.
68
00:06:58,740 --> 00:07:03,710
Assuming option equals hit, the first condition is false and the second condition is going to be true.
69
00:07:04,170 --> 00:07:05,640
Executing the if block.
70
00:07:06,150 --> 00:07:08,010
In this case, the option will stay.
71
00:07:08,040 --> 00:07:11,340
The first condition is true and the second condition would be false.
72
00:07:11,710 --> 00:07:14,130
And once again, executing the if block.
73
00:07:15,010 --> 00:07:20,080
So replaced your operator with the end operator, and now I'm going to keep restarting the debugging
74
00:07:20,080 --> 00:07:22,300
session until I've tested every scenario.
75
00:07:33,420 --> 00:07:35,010
Perfect, first it stays.
76
00:07:39,970 --> 00:07:42,370
Now the option is fold, so the extension should run.
77
00:07:48,930 --> 00:07:50,530
OK, now the option is hit.
78
00:07:50,550 --> 00:07:53,370
So the estimate would get skipped and indeed it does.
79
00:07:55,880 --> 00:08:01,760
Great, now I'm fully confident that my code is free of bugs so I can safely remove all of my breakpoints
80
00:08:01,760 --> 00:08:03,110
and rerun the code.
81
00:08:21,990 --> 00:08:28,800
Don't spam print line statements, don't debug by spamming print line, putting print lines all over
82
00:08:28,800 --> 00:08:29,880
your code is messy.
83
00:08:30,370 --> 00:08:36,330
It's going to annoy other developers working in the same code base and it doesn't paint the full picture.
84
00:08:37,970 --> 00:08:44,120
Instead, use breakpoints to visualize the runtime, the track, the state of your application Line-by-line.
85
00:08:47,690 --> 00:08:53,270
Let's recap, we used breakpoints to debug an application and breakpoints are a very powerful debugging
86
00:08:53,270 --> 00:08:57,200
feature, you can just visualize what's going on from point A to point B.
8581
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.