Readable Java 1.5
Subject:   Reduce syntax, do not expand it
Date:   2003-10-17 06:49:46
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:


or let's use it as a parameter

public void myMethod(Block b){

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){

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();){

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

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

(Object o){
Task t = (Task)o;
System.out.println("Executing task: " + t);
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 (String s){

return s.toUppercase();

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