From the course: Programming Foundations: Beyond the Fundamentals
Debugging code in an IDE - Python Tutorial
From the course: Programming Foundations: Beyond the Fundamentals
Debugging code in an IDE
- 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 on 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 popup 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 programmer 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.