Edit Rename Upload Download Back to Top

control flow

Control flow statements are another area where Smalltalk syntax can trouble the curly-brackets programmer but not the novice. Even simple if-statements are not quite so simple for Bob as for Alice. At the end of the first day, Jennifer might email Alice and Bob asking their view of it. She might write

   If you attended the course, please let me know what you think.

or else she might phrase it

   Did you attend the course?  If so, please let me know what you think.

As this is English, Bob and Alice are equally happy with either way of asking the question. In the course, it's another matter. Alice has no special problem with

   (self attend: course)
      ifTrue: [self sendOpinionOf: course to: jennifer].

as it's a straightforward codification of the second English style above. However, Bob was expecting a codification of the first style; something like

   if (self attend: course)
      [self sendOpinionOf: course to: jennifer].

as a Smalltalk version of Java's

   if (this.attend(course))
      {this.send_opinion(course, jennifer)};

and he is quite capable of arguing at length not merely that the Java version is what he is used to (true) but that it is much more like English than the other (not true).

All this is fairly trivial; Bob can grasp Smalltalk if-statements. The real trouble starts when he asks about case-statements. 'What do you mean, there aren't any!!' Worse, if the tutor is a Smalltalk enthusiast, it's quite possible that the tone in which he denies that Smalltalk needs case-statements implies that only an idiot would want one. Alice doesn't mind; she's never met a case-statement and it's one less thing to learn. But Bob has written thousands of case-statements. He knows some ways to avoid them but he's sure case-statements are often essential. It sounds like the tutor is insulting his abilities.

Here I think the average Smalltalk evangelist does underestimate the need to clarify this issue for the Bobs of this world. The VW tutorial and Ivan Tomek's 'Joy of Smalltalk' are two of several expositions I've seen where the Smalltalk ambassador says, 'Use polymorphism instead of a case statement; that's the OO way' and moves rapidly on, leaving the curly-brackets programmer unconvinced. So let's explore this issue in more detail.

There are four ways to express a case-statement in Smalltalk.

(Back to new code syntax.)

(Written by Niall Ross as part of Smalltalk for Java Programmers.)


Edit Rename Upload Download Back to Top