Uber-newbie: I wrote my first object!

Stephane Ducasse ducasse at iam.unibe.ch
Sun Feb 9 08:34:07 UTC 2003


Quite funny :)

now some feedback.
I'm not sure you need self into initialize. You can just say self 
startStepping. self step will be called automatically then.

something more important.

This is rare that you have to subclass collection or string. Here I 
would move all the string related methods (that are not in fact related 
to string) into your morph object.

Try to understand the difference between subclass (an object is nearly 
the same as another one) and aggregation (an object is composed of 
another one). Here your morph is composed of a collection of predefined 
insults. But see my next point.

Now the array of strings are created ***all*** the time you create a 
new sentence, which is a bit costly because you create them and throw 
them away all the time. The solution is to use a classVariable

BorderedStringMorph subclass: #InsultMorph
	instanceVariableNames: ' '
	classVariableNames: 'Adj13 Noun2 Noun4 Noun5'
	poolDictionaries: ''
	category: 'insults'

Then to initialize them


InsultMorph class>>initialize
	
	super initialize. "Skip this note in a first reading. normally you do 
not do that automatically because we should not invoke Object 
class>>initialize so one class should not call the superclass 
initialize method, here Morph does not invoke it so this is ok. On the 
instance side this is not the same, you normally always invoke the 
superclass initialize instance side methods."

	Adj13 := #('abject' 'abnormal'....)
	Noun2 := ...
	Noun4 :=
	Noun5 :=


In fact I would write it

InsultMorph class>>initialize

	super initialize.
	self initializeAdj13
	self initialize Noun2 := ...
	self initialize Noun4 :=
	self initialize Noun5 :=


InsultMorph class>>initializeAdj13

	Adj13 := #('abject' 'abnormal'....)


....


Then you can access for your instance side methods by simply typing the 
name of the variable.

contents
	"Generate a fresh insult from the wordlists"

	^ Adj13 atRandom , ' ' , Noun2 atRandom , ' ' , Adj13 atRandom , ' ' , 
Noun4 atRandom , ' ' , Noun5 atRandom , '!!' .! !


After coding, you have to call the initialize method InsultMorph 
initialize

A classVariable is initialized normally at load time. Save the file and 
look at the end you should see InsultMorph initialize.

I suggest you to read the freebook Smalltalk by Example which is 
available on my web page.

http://www.iam.unibe.ch/~ducasse/

Stef

On Sunday, February 9, 2003, at 06:01 AM, Glenn Alexander wrote:

> Hi,
>
> I'm very new to this.
>
> I wrote a little morphic object and I have put it at:
>
> http://www.shoalhaven.net.au/~glenalec/squeak/insults.st
>
> It is actually two objects as I thought it might be a good idea to
> split the string generation from the display morph so it is more
> re-usable.
>
> If anyone wants to give me some constructive criticism on my
> programming, it would be welcome! They are VERY small objects. If I
> get the code looking like an example of good programming, I can write
> a short tutorial on how I did it, if people think that's useful for
> other newbies.
>
>
> Thanx,
> Glenn Alexander
> glenalec(at)shoalhaven.net.au
>
> (As the network here is down, I'm on 28k dialup for a while, so i have
> turned off list mailings, but I will periodically check the archives
> and you can mail me direct if you like. Hopefully I'll be back online
> properly in a few days, but a few weeks is quite possible!)
>
> --------------------------------------------------------
> Glenn Alexander - The man with no surname and a silly hat.
> (B.Teach, B.Ed Major IT Education, University of Wollongong Australia)
>
> http://www.shoalhaven.net.au/~glenalec
>
>
Prof. Dr. Stéphane DUCASSE (ducasse at iam.unibe.ch) 
http://www.iam.unibe.ch/~ducasse/
  "if you knew today was your last day on earth, what would you do
  different? ... especially if, by doing something different, today
  might not be your last day on earth" Calvin&Hobbes




More information about the Squeak-dev mailing list