(upbeat music)
- I'm trying to use my drill, but it doesn't work.
To figure out what's wrong and if it's a problem I can fix,
I might need to go through some testing.
Is the battery all the way in?
Yup.
Is the battery charged up?
Well, I can swap out,
grab a battery that I know just came off the charger.
Still no love.
Oh, wait, the switch doesn't move when I push it.
I engaged the safety lock.
If I switch that off, the drill works.
After you write a program,
it has to be executed in order for you to see the results,
but even the best developer writes code from time to time
that includes errors or bugs.
Just like a broken drill,
a computer program with a bug doesn't run as expected
or may not run at all,
but not all bugs are the same.
The process of identifying and fixing bugs
is known as debugging
and it's an important skill for every programmer.
There are three main types of bugs in computer programs.
A program has a syntax error when it includes code
that doesn't match the rules of the language.
For instance, if I wrote a conditional in Python
without including a comma after the if statement,
my code wouldn't execute
because the Python interpreter wouldn't be able to identify
the end of that particular statement.
It's the same thing if I misspell a word,
like if I say P-I-R-N-T instead of print.
Sometimes your code can comply
with all the rules of the language,
but still not execute successfully.
For instance, suppose you include a call to a function
that isn't ever defined.
A function call is a valid statement,
but when the interpreter tries to run your code,
it won't be able to find and execute the function specified
in your function call.
This type of bug is known as a runtime error
because it often isn't obvious
until the code is actually run.
A third type of error happens when you write code that runs,
but doesn't produce the results you expect.
For instance, a loop that counts in the wrong direction
continues infinitely and as a result,
the program never completes execution.
This is known as a logic error
because while the statements themselves
constitute valid code, the result of the statements
isn't what you were trying to achieve
when you wrote the code.
Debugging is just a part of programming.
Every programming language has tools available
that help programmers identify bugs
and in some cases suggest how to fix them,
but using those tools successfully
requires that you take a methodical approach to identifying
and then fixing errors.
When your code doesn't run as you expect,
remember that it may contain more than one bug.
However, execution will generally fail at just one point.
You can work with the feedback provided by your tools
to fix the indicated bug.
But afterwards, it's important to retest
and follow up on any other error messages
or other issues that crop up after that first bug is fixed.
You'll need to repeat this cycle
until your program finally executes successfully
and works as you intend.
Rather than seeing debugging as a chore,
I try to look at it as an interesting puzzle to solve.
I encourage you to approach debugging as an opportunity
to learn more about programming in general
and about the specific language you're working in.
When you write code in most programming languages,
you're generally just writing text.
In theory, you could write your code in a basic text editor,
save your code and then use the appropriate interpreter
to execute it.
However, there's a wide selection of dedicated code editors
which are also known
as integrated development environments or IDEs.
IDEs are built specifically
for writing code in different languages.
What makes an IDE special
is that when it's properly configured
for the language you're coding in,
it can not only catch bugs,
but it can help you avoid creating some of them
in the first place.
I'm using Visual Studio Code as my IDE
and I've installed an extension for Python syntax support.
This means that whenever I open or type code
in a Python file, the editor does a few things.
First, it parses the syntax of the code
and highlights different types of code in different colors.
This feature is known as syntax highlighting
and it makes it easier to read through code
and recognize what each part of each statement is doing.
For instance, here the variable name value is in white
and the method names range and print are in yellow.
Syntax highlighting really shines in complex code.
Instead of needing to read through a wall of text
that's all the same color,
the different colors help you visualize
the functions of different pieces of code
and understand it more quickly.
An IDE can also offer to finish code for you
while you're typing, saving you some keystrokes
and also helping you avoid spelling mistakes.
This feature is known as autocompletion.
I want to add another print statement
at the end of my program here.
So on line three, I'm going to type p
and I see a list of possibilities
that all start with the letter P.
There's a lot of them,
so I'll continue on and type r
and now the list is narrowed to just five options
that start with the letters PR
and the first is the print method that I want to use.
Now, I could switch to my mouse
and click the item from the list,
but sticking with the keyboard,
I can also just press Tab
to select the current item in the list and move on.
Then when I type my opening paren,
the IDE automatically adds a closing paren for me
and it also displays a short synopsis
of how the syntax works for the Python print method
in case I need to check
which order the arguments go in for instance.
Once I start typing in the parens, the pop-up goes away.
I'm going to say all done.
Syntax highlighting and code completion are useful tools,
but IDEs can do even more for developers.
In many languages, an IDE can check code for bugs
before you even execute it.
This process is known as linting.
For instance, what if I type my variable reference
on line two incorrectly using val instead of value.
Let me make that change
and then when I save that code,
a squiggly line is displayed under the text val.
When I move my mouse pointer over that part of the code,
a pop-up explains that the variable val is undefined.
My program is relatively small, just three lines,
so it's pretty straightforward for me to scan the code
and recognize that I probably meant to reference
the value variable on line one,
but in a larger, more complex program
with a lot more lines of code,
it can take more work to trace down the source of that error
and figure out how to fix the error.
Here, I can edit val back to value
and then when I save my changes,
that squiggle goes away and my code passes
the IDE's linting process.
Different IDEs are optimized
for different programming languages.
When you're learning a new programming language,
it can be useful to research
which IDEs are commonly used by developers in that language.
Picking the right IDE for a language
ensures that you have the best tools
to write code accurately and efficiently.
(upbeat music)