[Newbies] I am a total beginner with some questions

Michael Haupt mhaupt at gmail.com
Wed Apr 9 12:40:08 UTC 2008


Hi,

I wonder why no one has recommended the excellent "Squeak by Example"
book yet... to get a hang of what programming in Squeak looks (and
feels) like, you *really* ought to work your way through this book.
It's available as a free PDF download: http://www.squeakbyexample.org/

Now, on to your problem...

On Wed, Apr 9, 2008 at 2:38 AM, kennellgr <cboneg5 at gmail.com> wrote:
>  I appreciate the help. I tried it out and it works. I have one problem
>  though. I created a method called "gdkDKeyDown" (gdk is my innitials so I
>  put it before my methods) and when I type gdkDKeyDown in a workspace and
>  print it it says nil (it's supposed to say true or false) how do I make it
>  return a value?

In Smalltalk (and hence, also in Squeak), you cannot simply invoke a
method by typing its name and issuing "do it" or "print it" - instead,
you send a message to an object. To be able to do that, you need to
implement the method in some class so that instances of that class can
actually understand the message (and effectively execute the method).

Please look at "Squeak by Example"; I really feel you need to do that
before you start. :-)

>  This is the method:
>
>  gdkDKeyDown
>         | x |
>
>         x := Sensor keyboardPeek asciiValue.
>         x == 99
>                 ifTrue: [^ true].
>         ^ false

May I be so bold and suggest a few improvements? ;-) (Please don't
misunderstand me as patronising; I just want to point out some of the
beauties possible in Smalltalk, or programming in general.)

First of all, expressions like "x == 99 ifTrue: [ ^true ]" smell bad
in almost every programming language. You can as well (and much more
concisely) write "^x=99" - and you don't even need the "^false" any
more!

Why is that? - The expression "x=99" (you don't need to use == here; =
is commonly used for equality checks) evaluates to a Boolean value -
either true or false, so you can as well just return that.

Also, you don't really need the temporary variable "x" as you only
ever store once into it and then read it again.

In summary, your method could as well be as short as this:

gdkDKeyDown
    ^Sensor keyboardPeek asciiValue = 99

Nice, eh? And we haven't even started to abstract away from the D yet. ;-)

Best,

Michael


More information about the Beginners mailing list