Recently I came to know and started to learn Smalltalk, an age-old pure Object-Oriented programming language. I fell in love with it quickly. Smalltalk is a very small language with some consistent concepts and abundant class libraries. I was puzzled at the very begining,  as Smalltalk has very few keywords(maybe 5 I think), and those keywords don't include any control structure(a.k.a if, while, for), how could we write programs? Althougth I know we could implement iterative sturucture via recursion, how about conditional execution? Finally I figured out that Smalltalk provides control strucutre in its class library only using some basic OO concepts. Followings are the codes from Smalltalk class library.


 1Boolean>>ifTrue: alternativeBlock 
 2  self subclassResponsibility
 3
 4Boolean>>ifFalse: alternativeBlock
 5  self subclassResponsibility
 6
 7"and True and False are subclasses of Boolean."
 8
 9True>>ifTrue: alternativeBlock 
10  ^alternativeBlock value
11
12True>>ifFalse: alternativeBlock
13  ^nil
14
15False>>ifTrue: alternativeBlock 
16  ^nil
17
18False>>ifFalse: alternativeBlock
19  ^alternativeBlock value

and then in some codes, we could do the conditional execution as follows

14 > 3 ifTrue: [Transcript show: 'Hello']


In Smalltalk, everything is Object, so that the code above means sending a message named '>' to an Integer object, whose value is 4, with an Integer object parameter. And '>' would return a Boolean object, and then we sent a message  named 'ifTrue' to it. This is a typical usage of State Pattern. Here are the equivalent Java codes.


 1public class abstract Boolean {
 2   public static final TRUE = new True();
 3   public static final FALSE = new False();
 4
 5   public abstract Object ifTrue(Block aBlock);
 6   public abstract Object ifFalse(Block aBlock);
 7}

 8
 9class True extends Boolean {
10
11   public Object ifTrue(Block aBlock) {
12       return aBlock.execute();
13   }

14
15   public Object ifFalse(Block aBlock) {
16       return null;
17   }

18}

19
20class False extends Boolean {
21
22   public Object ifTrue(Block aBlock) {
23       return null;
24   }

25
26   public Object ifFalse(Block aBlock) {
27       return aBlock.execute();       
28   }

29}

30
31public class Integer{
32  
33   public Boolean greaterThan(Integer anInteger) {
34      
35   }

36}

37

So that we could get the following code

 

14 〉3 ifTrue: [Transcript show: 'Hello']

 

which could be translated in Java like this:

  

new Integer(4).isGreaterThan(new Integer(3)).ifTrue(new BlockClosure() {

   
public Object value() {
      System.out.println(
"Hello");
      
return null;
   }


}
);

 

That's quite simple(some dynamic languages on JVM ,e.g. Groovy, do the same thing), but it indeed brings some profound changes to my thought.


First, considering that 'if''for' and 'while' are no longer pre-defined keywords, we could define our own control structures. For example, we could define Order has its own way to do something.

 

1anOrder ifExpired: [ do reactive order]
2        ifCanceled: [do something else]

 

Taking no account of performance and semantic speaking, there is no difference between the code above and undermentioned:

 

1   4 >3 ifTrue: [do something]
2        ifFalse: [do somthing]

 

More fancy, we could define a new test structure like this:

 

1anOrderTest should: [some assert] when: [do some initalize].

 

So we could have programmable control structure in Smalltalk ( something like we do in Lisp via continuation:) ), and define our own DSL easily.That's the amazing lightweight syntax feature in Smalltalk!

 

Second, we could get full capability of Von Nouma Style Programming Language via pure OO concepts.We could construct software in consistent OO concepts.

 

I began my adventure in Object technology by using C++ in 1995, and then I chose Java as my main working language. All my experiences about OO come from half-blooded OO language.So that for a long time, I thought Object-Oriented Programming is a big patch to imperative languages. The imperative languages are desgined  to record the sequence of instruments which are used to manipulate the computer, so they are lack in semantic, and poor in abstraction. Though the Object technology introduces a successful type system and provides some abstraction mechanism, I still feel sucks to program in inconsistent concepts, because I should be careful about keeping programming more in OO-style rather than in procedural-style(something like Domina Model or not, sucks!).

Once I accused Object-Oriented Methodology of all the fault, I blamed imperfection on Object-Oriented Methodology.But now, I found out it's only because that I have little talent and learning in OO, it turned out to be my fault, I should say sorry to Object-Oriented Methodology.