Debug super fast for newbies

Tram Ho

Really, no joke. Many students in 3rd year, 4th year still don’t know how to debug. And if you are too, then this is the article for you. Debug is not difficult, but it is a basic skill that every developer needs to know. If you have not touched on debug for a long time, you should also check out this article to review. Ok let’s go.

1. What is Debug?

Recalling when you first learned to program, everyone must have used the print statements to display the value of the variable to the screen. Java, for example.

This way is quite simple and easy to get used to. However, it is only suitable for newbies who have first contact with code. If you’ve been coding for a while, practice using another tool called the debugger .

Most IDEs and some text editors have debuggers. Debugger is a super useful tool to do debugging – find and fix errors in programs. That said, the debugger’s purpose is much more than that:

  • Track the flow of the program (if the program is stopped suddenly, the debugger can know which line is wrong)
  • View the values ​​of variables, objects
  • View logs printed to console, call stack, …

Many of you (and myself too) think debugging is something sublime, so I hesitate to learn. Actually using the debugger is extremely simple, just 5 minutes to read the article, you will understand immediately. Debug also includes only a few basic operations, the same on all IDEs, but the benefits are huge.

Developers who don’t know how to debug will throw them away

2. How to debug the standard?

Different IDEs have different debuggers, so it’s a little different. But in general, debugging includes only the following steps:

  • Set breakpoints on lines that need pausing for debugging
  • Run the program with debug mode
  • Manipulate the program, running to the breakpoint
  • When the IDE stops at the breakpoint, it is necessary to check the value of the variables, see the log, … to check for bugs.
  • Then go through the next line of code, watching the variables change after each statement
  • Continue running the normal program, or stop the program.

Basically, in the next sections we will discuss them in more detail.

2.1. Breakpoint

What is marked on the line of code, when the program runs to the line with a breakpoint, it will be paused. Now you can use the debugger tools to view the values ​​of the variables, see the log, … Or you can go on next line by line, or let the program run normally (no longer paused).

How to set a breakpoint: Click on the left margin of that line. At this time the breakpoint will be toggle, click again to delete (as shown).

The breakpoint is usually a red circle, this circle may differ slightly to indicate the breakpoint state (passed or not). When the program pauses at the breakpoint, that line will be highlighted.

Note: The highlighted code line will not be executed, only when you move on to another line will the previous line be executed.

2.2. Local variables & watches

When the program stops at the breakpoint, we can use two tools, Local variables and watches, to view the values ​​of the variables:

  • Local variables are just for viewing variables in the current function (local). When passing another function, the variable list will be updated again.
  • Watch is used to view global variables, or any other variable. The value of a watch is monitored even when the program stops.

Local variables will automatically update the list of variables in the function. However, with watch you have to manually add the variables (right mouse button on the variable, Add to watches).

As shown in the picture, IntelliJ IDEA combined the above 2 tools into Variables.

2.3. Step over, other steps, stop / resume program

When the IDE pauses at the breakpoint, we want to continue to go through the next lines of action:

  • Step over: go on to the next line of code
  • Step into: jump inside the function (the current line of code contains the function call)
  • Step out: From inside the function jumps out, back to where the function is called
  • Run to cursor: continue running until the line has cursor

In addition, sometimes you will want to continue running the program normally (no more having to step over each line). Now you use the command Resume to continue the program, or stop to stop.

The good trick is that I just started using a debugger and didn’t know this resume, so every time I run to the breakpoint I have to run the program again. Now that I think about it, I’m really stupid

Often the above commands will have corresponding shortcuts. This depends on the IDE that specifies the keymap, you can learn more.

2.4. Other tools

Debugger also has other tools to support such as:

  • Expression (evalution): a program that calculates expressions, you can especially enter any variable name.
  • Console: too familiar, this is where the logs are printed
  • Call stack: stack contains a list of functions and commands that have been called. We can see which function was called in the end, to find and place the breakpoint there quickly

Okay, today’s post is over. Wish you have a happy 3rd day Tet with friends, relatives and even “tuna”.  Happy Vietnamese new year, happy Valentine day and happy coding.

Remember to upvote for me, Valentine doesn’t go out but still has to write articles for you to read

Share the news now

Source : Viblo