In this session, I’d like to show you how
to use the debugger to program differently.
We will implement again the counter application
but in a different way, with a development flow more interactive.
First I will create
a new package with Counter, as previously.
I will also filter to have less elements.
In MyCounter, I first define
a test, a test class.
Test classes inherit from TestCase.
I will call it Counter Test.
I will compile. I do it once more in the menu and then
I do it with the comments.
It has created a test class.
In my test class, I write a test method.
The test methods begin with test.
What do I do?
As previously, I define a local variable. In
this local variable I put an
object from the Counter class.
I initialise the Counter value and
check that the Counter has the right value.
I compile.
Now the system tells me: “Warning! I don’t know
what Counter is.” It was already indicated
with the italics here.
It proposes: “Do you want to define a new class?
A new global or a class variable?
Here it is a new class.
It offers the class definition.
I say: “ok, it’s correct, it’s in the right package, it inherits
from object, all right, do it.”
So it creates an empty class.
Now we are going to arrange this.
I can see that my Counter class is empty.
If I execute my test, it is red.
Why is it red?
Because I have an error.
So how should I read this message?
Let’s take 2 minutes to do it.
The system says: “a Count message has been sent to
an instance of the Counter class and the corresponding
method wasn’t found.” I’m going to look…
I open the debugger.
The debugger displays the execution stack, so I can
see that a message hasn’t been understood.
And there it shows where the message has been invoked.
I see that if I go there, I can see the
system variables. If I look at C, I can access
to the temporary C variable, which is valued a Counter.
Anyway, if I write C class for instance,
self class, it says it is the Counter class.
This is obvious. For the moment, in my class, I don’t have any
method. I will ask the system to create one for me.
I do Create.
It proposes the classes.
I choose Counter class.
Can you create this?
Yes, in Accessing.
And finally what did he do?
It compiled automatically a method with the right signature.
For the method body, it doesn’t know what to do,
so it uses ShouldBeImplemented. What does it do?
It raises an exception at the execution and the system
execute again the method.
Now there is a new error and I have to
implement the method body.
I know Count. What am I going to do?
I want to assign this value to an
instance variable. So here
it’s red, because for the moment my class doesn’t have this instance.
I compile.
The system proposes: “Do you want a temporary variable
or an instance variable?”
I want an instance variable.
So I do this. If I look in my class, I do have
a Count instance variable which was created and
the method, the Setter, which
will set the value.
Once I’ve done this, I can tell the debugger
to continue, Proceed means continue the execution
as if nothing happened.
What happens?
There is still an error.
The system still says that there is an instance
of Counter class which received the count message and that
it hasn’t found the appropriate method. This is normal because
C does have 7
in its Counter, in its count variable, however
the count method doesn’t exist.
I’m gonna create it.
I will do Create. In which class?
Still the Counter class, in the accessing protocol,
because it’s an accessor. And there you
see that the system is smarter.
It noticed that there was an instance variable named
count, and it wants to define a method named count,
so let’s propose it an accessor.
That’s what I wanted.
So now I can Proceed.
What do I see?
My test is green. I have a test green and
2 methods have been created in my class.
Now I will do the same for increment.
In fact, I am
specifying what the method has to do
and checking that it does do it.
So there I must have 9, I compile, I execute.
And the system tells me that it doesn’t know the increment method.
Yes, it’s normal. I do Create, here in operation.
If I look at increment, I can do count.
= count + 1. I compile.
Proceed. I do have my test.
While I’m at it, I create decrement.
The idea is for you to do it, to practice it.
So here it will be 5. I define my test.
I create my method.
OK. So now you know, count: = count,
I compile in the debugger.
I click on Proceed. And my tests are green.
Now my class has 2 operations: increment and decrement.
It’s time to save, for example.
I’m going to save like I’ve shown you before.
I click on Save, I save and
here I write "with
accessors and decrement increment covered by green tests".
So here it is! I’ve saved that.
Allright.
The important thing to remember in this session is that
the debugger gives access to the object’s
state, it places you in the right call context and
enables you to use the call context to have all the necessary
information to write your code.
And this is really very, very powerful.