-Hi. In this video, we are going to look
at how the Debugger might help you to understand your methods better.
I chose to use the package MyCounter
that we implemented in the redo of weeks 1 and 2.
Basically, we are going to look at this test right here, "testCounterDecrement",
that was using and initiating a Counter starting at 5 and decremented two times,
and checking that the instance variable count was actually 3.
We can open the Debugger on this instance
by right-clicking and going over to Debug tests
or pressing the shortcut Cmd+D.
By clicking on that, you can see that you have got your Debugger open
with "Break" written on top of it.
This is because it has created a breakpoint
right at the beginning of your test method.
Within the Debugger, you can see that there are three different areas
that are really important. You have got, first, the execution Stack on top.
This execution Stack is composed of all the different methods that were called
in order to give the test the ability to execute itself.
You have got a lot of graphical or process creation methods.
But the one that is the most interesting to us is the last one.
The actual call to testCounterDecrement.
This area here is the body of the source of the method that is being executed.
The highlighted portion is a version
that is currently being inspected by the Debugger.
We can get into that more later on.
The final part consists of all the different variables
that the system has for now and their value at this time in the execution.
Finally, we can navigate in the execution Stack and in the Debugger
with the different functions on top of it.
Basically, Proceed will resume the execution up to either its end
or the next error. Restart will relaunch the debug operation.
Then, Into and Over are the ones we will use right now.
Into helps you dive into the source code of the highlighted method
while Over will make the execution go to the next method.
If I press Over, we can see that right now
startingAt is the method being highlighted.
This is the method we implemented in the class side of the counter.
If you would like to have a look at it, we can simply press Into.
This will bring us to another step in the execution Stack.
We are now in the startingAt in the Counter class side.
You can see that "new" is now highlighted.
We do not really want to dive in the "new" source code,
so what we will do is press Over. This will highlight the affectation.
And you can see here that, for now, c has the value nil.
When we will press Over,
you can see that it has now the value of "a Counter".
We can look into it and see that for now its attributed count is equal to nil.
But we will set it right away right here.
Let us dive into this count to see if it is actually a setter.
We can see that it is a setter. A nice thing we can do,
we can highlight, for example, anInteger, press Cmd+D to print it.
You can see that it is equal to 5.
You can see that this part there is equal to nil
while this part here is equal to 5.
This is extremely useful. We press Over.
This will execute the method count
and we can now look at our temporary variable, c,
and look on the Raw tab that it now has a value 5 as its count attribute.
This will return the c, OK?
Right now, we came back to our testCounterDecrement body,
and we can see that c is still equal to nil.
But we are highlighting the affectation, so by pressing Over,
we will give c a value, the value of "a Counter" starting at 5.
Now, it is equal to "a Counter" and if we go to the Raw tab,
you can see that count is equal to 5.
This is really helpful to understand the objects you work with.
Right now, the decrement method is highlighted.
Once again, we dive into and look at, for example, count.
Now, it is equal to 5 and "count -1" is equal to 4.
So, basically it will simply do this affectation.
Press Over, Over, Over.
If we look back at our counter,
we can now see that its count is equal to 4. Press Over again,
Over again, and now we can look back at our c.
It should have a count equal to 3 because we decremented two times.
We can inspect this and we know that it is equal to 3,
so everything should be fine, we can press Proceed.
This is a small look into the Debugger,
but it is really helpful for you to understand
both the workflow of your methods and your objects better.
Another thing that is important to note
is that you can do the same thing from within the Playground.
For example, if I copy/paste this piece of code,
highlight it, right-click, and go over to Debug it,
or you can press the Cmd+Shift+D shortcut,
it will open the Debugger where you can do basically the same thing.
If I press Over a couple of times, we can look inside the value of c,
see that it is a counter with a value of 5 as count.
We can decrement it, look back at it, it is equal to 4.
You can do a couple of things and the last thing that you should not forget
is that the Debugger is a working environment, and if, for example,
I dive into the method decrement, and I change it to -2,
and I save this, because you can save this.
The orange corner meaning that it is not saved.
So, I save it pressing Cmd+S,
and my decrement method is now this: "count := count -2".
If I press Proceed... I can just close it.
If I go over to my test, it might not work
because we changed our decrement method, and if I click on it,
you can see that I obtain 1 instead of 3.
So, the Debugger is a working environment,
and it will really help you understand the flow of your methods
and your objects better. So I can only encourage you to use it.