Skip to content

Commit 2904b7f

Browse files
authored
Merge pull request #212 from vplentinax/sync-codeQuality
Sincronización-03-code-quality
2 parents 76857bc + 2bdd2fb commit 2904b7f

20 files changed

Lines changed: 1662 additions & 1456 deletions

1-js/03-code-quality/01-debugging-chrome/article.md

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,39 @@
22

33
Before writing more complex code, let's talk about debugging.
44

5-
All modern browsers and most other environments support "debugging" -- a special UI in developer tools that makes finding and fixing errors much easier.
5+
[Debugging](https://en.wikipedia.org/wiki/Debugging) is the process of finding and fixing errors within a script. All modern browsers and most other environments support debugging tools -- a special UI in developer tools that makes debugging much easier. It also allows to trace the code step by step to see what exactly is going on.
66

7-
We'll be using Chrome here, because it's probably the most feature-rich in this aspect.
7+
We'll be using Chrome here, because it has enough features, most other browsers have a similar process.
88

9-
## The "sources" pane
9+
## The "Sources" panel
1010

1111
Your Chrome version may look a little bit different, but it still should be obvious what's there.
1212

1313
- Open the [example page](debugging/index.html) in Chrome.
1414
- Turn on developer tools with `key:F12` (Mac: `key:Cmd+Opt+I`).
15-
- Select the `sources` pane.
15+
- Select the `Sources` panel.
1616

1717
Here's what you should see if you are doing it for the first time:
1818

1919
![](chrome-open-sources.svg)
2020

21-
The toggler button <span class="devtools" style="background-position:-168px -76px"></span> opens the tab with files.
21+
The toggler button <span class="devtools" style="background-position:-172px -98px"></span> opens the tab with files.
2222

23-
Let's click it and select `index.html` and then `hello.js` in the tree view. Here's what should show up:
23+
Let's click it and select `hello.js` in the tree view. Here's what should show up:
2424

2525
![](chrome-tabs.svg)
2626

27-
Here we can see three zones:
27+
The Sources panel has 3 parts:
2828

29-
1. The **Resources zone** lists HTML, JavaScript, CSS and other files, including images that are attached to the page. Chrome extensions may appear here too.
30-
2. The **Source zone** shows the source code.
31-
3. The **Information and control zone** is for debugging, we'll explore it soon.
29+
1. The **File Navigator** pane lists HTML, JavaScript, CSS and other files, including images that are attached to the page. Chrome extensions may appear here too.
30+
2. The **Code Editor** pane shows the source code.
31+
3. The **JavaScript Debugging** pane is for debugging, we'll explore it soon.
3232

33-
Now you could click the same toggler <span class="devtools" style="background-position:-200px -76px"></span> again to hide the resources list and give the code some space.
33+
Now you could click the same toggler <span class="devtools" style="background-position:-172px -122px"></span> again to hide the resources list and give the code some space.
3434

3535
## Console
3636

37-
If we press `Esc`, then a console opens below. We can type commands there and press `key:Enter` to execute.
37+
If we press `key:Esc`, then a console opens below. We can type commands there and press `key:Enter` to execute.
3838

3939
After a statement is executed, its result is shown below.
4040

@@ -56,8 +56,8 @@ A *breakpoint* is a point of code where the debugger will automatically pause th
5656

5757
While the code is paused, we can examine current variables, execute commands in the console etc. In other words, we can debug it.
5858

59-
We can always find a list of breakpoints in the right pane. That's useful when we have many breakpoints in various files. It allows us to:
60-
- Quickly jump to the breakpoint in the code (by clicking on it in the right pane).
59+
We can always find a list of breakpoints in the right panel. That's useful when we have many breakpoints in various files. It allows us to:
60+
- Quickly jump to the breakpoint in the code (by clicking on it in the right panel).
6161
- Temporarily disable the breakpoint by unchecking it.
6262
- Remove the breakpoint by right-clicking and selecting Remove.
6363
- ...And so on.
@@ -70,7 +70,7 @@ That's handy when we need to stop only for a certain variable value or for certa
7070

7171
## Debugger command
7272

73-
We can also pause the code by using the `debugger` command, like this:
73+
We can also pause the code by using the `debugger` command in it, like this:
7474

7575
```js
7676
function hello(name) {
@@ -89,7 +89,7 @@ That's very convenient when we are in a code editor and don't want to switch to
8989

9090
## Pause and look around
9191

92-
In our example, `hello()` is called during the page load, so the easiest way to activate the debugger is to reload the page. So let's press `key:F5` (Windows, Linux) or `key:Cmd+R` (Mac).
92+
In our example, `hello()` is called during the page load, so the easiest way to activate the debugger (after we've set the breakpoints) is to reload the page. So let's press `key:F5` (Windows, Linux) or `key:Cmd+R` (Mac).
9393

9494
As the breakpoint is set, the execution pauses at the 4th line:
9595

@@ -105,7 +105,7 @@ Please open the informational dropdowns to the right (labeled with arrows). They
105105

106106
At the current moment the debugger is inside `hello()` call, called by a script in `index.html` (no function there, so it's called "anonymous").
107107

108-
If you click on a stack item, the debugger jumps to the corresponding code, and all its variables can be examined as well.
108+
If you click on a stack item (e.g. "anonymous"), the debugger jumps to the corresponding code, and all its variables can be examined as well.
109109
3. **`Scope` -- current variables.**
110110

111111
`Local` shows local function variables. You can also see their values highlighted right over the source.
@@ -118,52 +118,65 @@ Please open the informational dropdowns to the right (labeled with arrows). They
118118

119119
Now it's time to *trace* the script.
120120

121-
There are buttons for it at the top of the right pane. Let's engage them.
122-
123-
<span class="devtools" style="background-position:-7px -76px"></span> -- continue the execution, hotkey `key:F8`.
121+
There are buttons for it at the top of the right panel. Let's engage them.
122+
<!-- https://github.com/ChromeDevTools/devtools-frontend/blob/master/front_end/Images/src/largeIcons.svg -->
123+
<span class="devtools" style="background-position:-146px -168px"></span> -- "Resume": continue the execution, hotkey `key:F8`.
124124
: Resumes the execution. If there are no additional breakpoints, then the execution just continues and the debugger loses control.
125125

126126
Here's what we can see after a click on it:
127127

128128
![](chrome-sources-debugger-trace-1.svg)
129129

130-
The execution has resumed, reached another breakpoint inside `say()` and paused there. Take a look at the "Call stack" at the right. It has increased by one more call. We're inside `say()` now.
130+
The execution has resumed, reached another breakpoint inside `say()` and paused there. Take a look at the "Call Stack" at the right. It has increased by one more call. We're inside `say()` now.
131+
132+
<span class="devtools" style="background-position:-200px -190px"></span> -- "Step": run the next command, hotkey `key:F9`.
133+
: Run the next statement. If we click it now, `alert` will be shown.
134+
135+
Clicking this again and again will step through all script statements one by one.
136+
137+
<span class="devtools" style="background-position:-62px -192px"></span> -- "Step over": run the next command, but *don't go into a function*, hotkey `key:F10`.
138+
: Similar to the previous the "Step" command, but behaves differently if the next statement is a function call. That is: not a built-in, like `alert`, but a function of our own.
139+
140+
The "Step" command goes into it and pauses the execution at its first line, while "Step over" executes the nested function call invisibly, skipping the function internals.
141+
142+
The execution is then paused immediately after that function.
143+
144+
That's good if we're not interested to see what happens inside the function call.
131145

132-
<span class="devtools" style="background-position:-137px -76px"></span> -- make a step (run the next command), but *don't go into the function*, hotkey `key:F10`.
133-
: If we click it now, `alert` will be shown. The important thing is that `alert` can be any function, the execution "steps over it", skipping the function internals.
146+
<span class="devtools" style="background-position:-4px -194px"></span> -- "Step into", hotkey `key:F11`.
147+
: That's similar to "Step", but behaves differently in case of asynchronous function calls. If you're only starting to learn JavaScript, then you can ignore the difference, as we don't have asynchronous calls yet.
134148

135-
<span class="devtools" style="background-position:-72px -76px"></span> -- make a step, hotkey `key:F11`.
136-
: The same as the previous one, but "steps into" nested functions. Clicking this will step through all script actions one by one.
149+
For the future, just note that "Step" command ignores async actions, such as `setTimeout` (scheduled function call), that execute later. The "Step into" goes into their code, waiting for them if necessary. See [DevTools manual](https://developers.google.com/web/updates/2018/01/devtools#async) for more details.
137150

138-
<span class="devtools" style="background-position:-104px -76px"></span> -- continue the execution till the end of the current function, hotkey `key:Shift+F11`.
139-
: The execution would stop at the very last line of the current function. That's handy when we accidentally entered a nested call using <span class="devtools" style="background-position:-72px -76px"></span>, but it does not interest us, and we want to continue to its end as soon as possible.
151+
<span class="devtools" style="background-position:-32px -194px"></span> -- "Step out": continue the execution till the end of the current function, hotkey `key:Shift+F11`.
152+
: Continue the execution and stop it at the very last line of the current function. That's handy when we accidentally entered a nested call using <span class="devtools" style="background-position:-200px -190px"></span>, but it does not interest us, and we want to continue to its end as soon as possible.
140153

141-
<span class="devtools" style="background-position:-7px -28px"></span> -- enable/disable all breakpoints.
154+
<span class="devtools" style="background-position:-61px -74px"></span> -- enable/disable all breakpoints.
142155
: That button does not move the execution. Just a mass on/off for breakpoints.
143156

144-
<span class="devtools" style="background-position:-264px -4px"></span> -- enable/disable automatic pause in case of an error.
157+
<span class="devtools" style="background-position:-90px -146px"></span> -- enable/disable automatic pause in case of an error.
145158
: When enabled, and the developer tools is open, a script error automatically pauses the execution. Then we can analyze variables to see what went wrong. So if our script dies with an error, we can open debugger, enable this option and reload the page to see where it dies and what's the context at that moment.
146159

147160
```smart header="Continue to here"
148161
Right click on a line of code opens the context menu with a great option called "Continue to here".
149162
150-
That's handy when we want to move multiple steps forward, but we're too lazy to set a breakpoint.
163+
That's handy when we want to move multiple steps forward to the line, but we're too lazy to set a breakpoint.
151164
```
152165

153166
## Logging
154167

155-
To output something to console, there's `console.log` function.
168+
To output something to console from our code, there's `console.log` function.
156169

157170
For instance, this outputs values from `0` to `4` to console:
158171

159172
```js run
160173
// open console to see
161174
for (let i = 0; i < 5; i++) {
162-
console.log("value", i);
175+
console.log("value,", i);
163176
}
164177
```
165178

166-
Regular users don't see that output, it is in the console. To see it, either open the Console tab of developer tools or press `key:Esc` while in another tab: that opens the console at the bottom.
179+
Regular users don't see that output, it is in the console. To see it, either open the Console panel of developer tools or press `key:Esc` while in another panel: that opens the console at the bottom.
167180

168181
If we have enough logging in our code, then we can see what's going on from the records, without the debugger.
169182

@@ -172,12 +185,12 @@ If we have enough logging in our code, then we can see what's going on from the
172185
As we can see, there are three main ways to pause a script:
173186
1. A breakpoint.
174187
2. The `debugger` statements.
175-
3. An error (if dev tools are open and the button <span class="devtools" style="background-position:-264px -4px"></span> is "on").
188+
3. An error (if dev tools are open and the button <span class="devtools" style="background-position:-90px -146px"></span> is "on").
176189

177-
Then we can examine variables and step on to see where the execution goes wrong.
190+
When paused, we can debug - examine variables and trace the code to see where the execution goes wrong.
178191

179192
There are many more options in developer tools than covered here. The full manual is at <https://developers.google.com/web/tools/chrome-devtools>.
180193

181194
The information from this chapter is enough to begin debugging, but later, especially if you do a lot of browser stuff, please go there and look through more advanced capabilities of developer tools.
182195

183-
Oh, and also you can click at various places of dev tools and just see what's showing up. That's probably the fastest route to learn dev tools. Don't forget about the right click as well!
196+
Oh, and also you can click at various places of dev tools and just see what's showing up. That's probably the fastest route to learn dev tools. Don't forget about the right click and context menus!

1-js/03-code-quality/01-debugging-chrome/chrome-open-sources.svg

Lines changed: 1 addition & 15 deletions
Loading

1-js/03-code-quality/01-debugging-chrome/chrome-sources-breakpoint.svg

Lines changed: 1 addition & 20 deletions
Loading

1-js/03-code-quality/01-debugging-chrome/chrome-sources-console.svg

Lines changed: 1 addition & 14 deletions
Loading

1-js/03-code-quality/01-debugging-chrome/chrome-sources-debugger-pause.svg

Lines changed: 1 addition & 67 deletions
Loading

1-js/03-code-quality/01-debugging-chrome/chrome-sources-debugger-trace-1.svg

Lines changed: 1 addition & 15 deletions
Loading

1-js/03-code-quality/01-debugging-chrome/chrome-tabs.svg

Lines changed: 1 addition & 55 deletions
Loading
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<style>
22
span.devtools {
33
display: inline-block;
4-
background-image: url(/article/debugging-chrome/toolbarButtonGlyphs.svg);
5-
height:16px;
6-
width:16px;
4+
background-image: url(/article/debugging-chrome/largeIcons.svg);
5+
height:18px;
6+
width:18px;
77
}
88
</style>

0 commit comments

Comments
 (0)