Use indentation consistently in your methods. Make methods short -- typically two browser panes worth of text at most. (Generally the style in Smalltalk is to make methods shorter than the typical C or C++ procedure.) If you do end up with a method that keeps getting longer and longer, define some private auxiliary methods, and then just invoke then using (for example) self helperMethod.
Instance variable, argument, and temporary names should start with a lower-case letter; global and class variables with an upper-case letter. Unfortunately, underscore isn't a legal part of identifiers in Smalltalk, so use capitalization to separate words, e.g. aVeryLongInstanceVarName.
gadget class = Flipper ifTrue: [...]. gadget class = BlackHole ifTrue: [...].Instead, turn this around and send a message to gadget so that flippers and black holes can respond differently:
gadget hitByBall: selfThen you can define different versions of hitByBall: in Flipper, BlackHole, and so forth.
If you really need to know whether an object obeys some message protocol, define a method to ask. For example, all objects in Smalltalk understand isNumber. Numbers return true, everything else returns false.
Unfortunately the syntax for conditionals in Smalltalk makes nested conditionals hard to read. You can judiciously use ^ (return) to improve readibility. Consider the sign method for Numbers, which returns -1, 0, or 1 depending on whether the number is negative, zero, or positive.
Here is a version using nested conditionals:
sign ^ self < 0 ifTrue: [-1] ifFalse: [self > 0 ifTrue: [1] ifFalse: [0]]Compare with:
sign self < 0 ifTrue: [^ -1]. self > 0 ifTrue: [^ 1]. ^ 0The sign method isn't too bad, but it gets worse with more complicated logic. Sometimes it's worth writing an auxiliary method just so you can return from the middle of the auxiliary method to make the code clearer. (Remember ^ pops out of nested loops, conditionals, etc.)
| s | s := Stack new. ...Often it's more appropriate to use self or a message to a class in place of a global variable. For example, in Smalltalk the color yellow is obtained by sending the message 'yellow' to the class color, rather than by storing it in a global variable.
Color yellowSimilarly, the constants pi and e are found in Smalltalk by sending messages to Float:
Float pi Float eOr here's a questionable example from the Smalltalk haikus the 505 students wrote some years ago:
i ride a turtle into object nirvana evan become: nilA haiku in better Smalltalk style would have been:
i ride a turtle into object nirvana self become: nil