Hello team, Today I will reveal the secret which will help you
to become senior developer. Today I will show you how debugger works
and how you can investigate execution of your program to find the
defects and to improve your code. What is a debugger? Debugger is
a special tool which helps you to run program under controlled conditions
that allow you to monitor all changes in your program. You can
pause execution of your program whenever you want, you can display contents
of memory, you can investigate state of variables and lots more. How
this will help you to become senior developer? Debugger tool helps you
to understand how even complex enterprise programs work. You can spend some
time to investigate what is happening inside the program – what methods
are called when checkout is happening on the website, you can see
what arguments are passed to the method and what are their values,
probably there are specific logic for discount calculation is also on place.
This will help you to understand where to add additional logic, or
in other words – where to write new code for tax calculation,
let’s say, or where you need to fix defects. Even now, when
you did your home task, I’m sure there were cases when you
run you program, and you didn’t understand why console output is not
the one you expected. To get answer on ‘why’ question and to
understand how program works – debugger tool exists and today, I will
teach you how you can use it. As always, to understand how
specific tool works – let’s jump to practice. For example, let’s try
to implement one of the home tasks. Do you remember home task
when you needed to filter a string by words length? Let’s debug
this program. The first thing you need to do is to set
a breakpoint. Breakpoint is a line in your program where execution will
be paused. Let’s set the breakpoint in this line. Just hover mouse
over this row and do mouse double left click. To debug your program,
you need to do mouse right click and press ‘Debug As’ ->
‘Java Application’. Our program waits for user input in this line. Let’s
enter some text here. Now our program expects another user input in
this line. Minimum word length will be four. And now program execution
paused in the line where we set breakpoint. Eclipse asks a permission
to switch perspective. Let’s switch perspective to debug perspective, because there are
a lot of useful views on it. So now we switched to
debug perspective. Here on the right you always can track how state
of the variables are changed. For example, at this moment of program
execution, we have: args variable which is empty; Scanner object;
; userInput String - and minLength variable. Let’s pretend that we want
to understand which regular expression works the best for this particular case.
Or we want to make sure that this regular expression works as
expected. Let’s select expression -> mouse right click -> watch. Here in
expression tab we see the result of execution of this expression. Let’s
add new with another regular expression, probably, we just need to split
userInput by space character? Click here – ‘Add new expression’ – userInput,
split, space character. And we can see the result. In this case
empty strings will be present in our result array. You can also
use debug shell. Let me show you one more example. You can
select expression, mouse right click and press ‘display’ to see the result
right here. Additionally, you can press Ctrl Shift D to switch to
debug shell view. Here it is. What is good about debug shell?
There is autosuggestion support here. Let’s remove this by selecting and clicking
backspace. Now I can start type ‘user’ and press Ctrl plus Space
– and here is autosuggestion. Now I can enter dot, and again
autosuggestion works as expected. Let’s again split userInput by space character. To
execute code in debug shell you need to select it and press
this button – ‘Display result’. And again, you can clear it if
you wish. What else you can do here? It is very useful
in debug mode to check your code with different values and to
change them during the program execution. For example, let’s check our program
with different userInput. userInput equals and specify any random string here. Now,
you can execute this code. Select it and press this button –
‘Execute selected text’. Let’s switch to variables view here and you can
see that our userInput variable has been changed. So, during the debugging
you can execute code which doesn’t exist in your current program. That
can help you a lot, when you want to make sure whether
you need to add some code or no. Definitely you can check
this in different ways, for example with unit tests, or just add
some code and run your program one more time. But when you
deal with huge programs you realize that making changes in one line
and compile all files, build the .jar or .war file to deploy
it on web server or just to run it – this may
take up to thirty minutes. That’s why such manipulations in debug mode
can help you be more efficient and productive. Ok, let’s move on.
Here on top you have buttons to control execution of your program.
This play button means jump to another breakpoint. Stop button obviously means
‘stop’ execution. And here are two buttons which we will use. Step
into and step over. Step over brings us to the next line
of the execution. Let’s press it. In this line I see that
filterWordsByLength method is going to be executed. And I want to investigate
what is happening inside it. I press ‘step into’ button. This button
brings me inside this method. You can also notice that variables list
here is changed. Now I can see only variables which are visible
in scope of this method. Let’s press ‘step over’ button multiple times.
And you see how counter variable is changed. When you are tired
of this loop, you can set one more breakpoint. Let’s say after
this loop. Now I can press ‘Play’ button and jump to another
breakpoint. Now I can press ‘step over’ multiple times and see filtered
words even without printing them to console. Press ‘Play’ button – to
finish the program. Important thing which students always ask me is how
to restore previous view in eclipse. Just navigate back to Java or
Java EE perspective here. That’s it. So now, you know how to
debug program of any level of complexity. This will help you a
lot in finding bugs and in investigation how program works. That’s all for today.
Thank you for your attention and see you in the next lesson.