ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
Article:
  Readable Java 1.5
Subject:   Reduce syntax, do not expand it
Date:   2003-10-17 06:58:57
From:   anonymous2
Expanding the Java syntax? Why? Instead, search for possiblities to reduce it. Anyone programmed in Smalltalk. I really loved this language, except the case that it was type-less. It's syntax could be described on one (normal sized) page.


Smalltalk had something called blocks. Blocks were nothing more then to be able to create an object containing some statements, just like the statements you put between '{' and '}'.
To put it in Java syntax:


Block myBlock = {
System.out.println("This is a test");
// do something else
// put code here whatever you like
System.out.println("Last statement executed.");
}


The example above created a Block-object and stores it into the variable myBlock. Important is that this doesn't execute the statements in the block. Execution of the statements would look like this:


myBlock.execute();


or let's use it as a parameter


public void myMethod(Block b){
b.execute();
}


Above statements causes the blocks to be executed. Now what is the advantage of such a feature? Well, in Smalltalk block's could be paramaterized. Take a look at this example:


Block printBlock = (Object printMe){
System.out.print(printMe);
}


The code above created a block which prints it's parameter printThis which should be a String. The example below uses this block.


for(Iterator i = c.iterator();i.hasNext();){
printBlock.execute(i.next());
}


In the case above the printBlock get executed with each element of the collection. As we see block can have parameters. In addition, blocks (like any other object) can be used as parameters too. THIS MAKES BLOCKS EXTREMLY USEFULL AND REDUCES A LOT OF SYNTAX!!! Take a look at the example below, where the collection class has a new method:


public forEach(Block b)


This method executes the block b for each of the elements it has. Thus the collection class uses an iterator (or whatever it likes) to iterate each element and executes the block b with the element as the parameter. We van use this method as follows:


Collection collection;
// add elements to c
collection.forEach(printBlock);


Of course block do not have to be assigned to a variable first. Let's use them directly:


collection.forEach(
(Object o){
Task t = (Task)o;
System.out.println("Executing task: " + t);
t.perform();
System.out.println("Finished task: " + t);
}
);


See, blocks make it very usefull for iterating. In fact, all looping constructs like for and while are obsolete with blocks. This greatly reduces the syntax.


Blocks look like anonymous inner classes, which were commonly used for to provide event-listeners. Blocks can have return types too. Lets look at another example. This block has the return type String and takes a String as parameter:


Block decisionBlock = String (Object o){
return ((String)o).toUppercase();
}


the collection class is extended and has the method collect(Block b):


Collection uppercased =
collection.collect(decisionBlock);


making it very usefull to transform one collection into another. Some other examples:


// select: takes a subset of a collection
// it executes the block for each of its elements and creates and answers a new collection with only those elements for which the block-execution return true


collection existingFiles = collection.select(
boolean (Object o){
File f = (File)o;
return f.exists();
}
)


also think of providing exception handlers:


public void performTask(Task t, Block errorBlock){
try{
t.perform();
} catch(Exception e){
errorBlock.execute();
}
}


1 to 1 of 1
1 to 1 of 1