-We are in part 5 of the redo
and we are looking at how to initialize our objects correctly.
What we did in the previous parts
was calling "new" to the class we want to instantiate
and then attributing manually the values we want to the instance variables.
Let's see what happens
if we simply call "c increment" right now.
Let's just do it, Cmd+P.
Okay, so "+ was sent to nil".
This means that "count" by default,
which we have not initialized in any way,
will have as value "nil", which Pharo chose for us.
This is not the most useful default value
and imagine we could put "0".
Let's write a test that would cover this case.
We will not need...
Let's rename it first, "testInitialize".
We will not need these two lines.
We want right after creation
that the "count" instance variable is equal to 0.
Let's save this. Okay.
Now, if we initialize it,
let's run the test.
It fails because we have not changed anything
and the default value is still "nil".
In order to change it, we have to define an "Initialize" method.
This method is called at the creation of any object
so it is good practice to call the superclass initialization methods
and then implement the behavior we wanted to have,
so "count" is equal to 0.
Okay, save this.
The test method has been detected. Let's run it.
It is working so this is great.
We have the default behavior of putting the instance variable count to 0.
Now, imagine we want to have a value of 5 at creation.
We could change the "initialize" method
but we would like to keep this one as default.
We can write a class method that would handle this for us.
Let's write a test.
Let's write the "testStartingAt5".
Instead of "new", we will call it "startingAt: 5".
We want to assert that the count would equal 5.
Okay, let's save this.
The method is not detected, but it is normal. Let's run it.
Okay, "Instance of Counter class did not understand #startingAt".
We have to implement the class method "startingAt" in the Counter.
In order to implement a class method, you go to the class you want.
"Inst. side", that is where we are right now.
Let's go to "Class side"
and implement "startingAt: anInteger".
Okay and we want to write: "self new count: anInteger".
This means a new counter will be created
and the instance variable will be the integer we will provide.
Let's save this and now if we run our tests,
they are all green. Cool.
Let's write a final test that handles most of our functions.
We will call it "StartingAt5Increment".
We will start at 5,
but we will just add an increment step right there
and then call "count" on the incremented counter.
This should be 5 incremented, so 6.
Let's see.
If we run all our tests,
the six ran and the six passed.
This is a great time to commit your changes.
We will go to the Iceberg Working copy,
press Commit
and write...
"Initialization methods (tested)".
Let's commit that.
We learned how to initialize correctly your objects
with a default behavior
and provide class methods to have more examples
of working instances for classes you want.