Last updated : 30/11/2002; 15:30:32

Joe's Jelly

Joe Walnes rambling on and on about Software Development, Java and Extreme Programming.

NAVIGATION >>

/WIKI /WEBLOG /THOUGHTWORKS /OPENSYMPHONY /EMAIL

:: ELSEWHERE
xp developer wiki
c2 xp wiki
the server side
javaworld
sd magazine
jakarta

:: BLOGS
Ara Abrahamian
Mathias Boegart
Mike Cannon-Brookes
Paul Hammant
Aslak Hellesøy
Darren Hobbs
Patrick Lightbody
Charles Miller
Brett Morgan
Rickard Öberg
Joseph Ottinger
Mike Roberts
Chris Stevenson
James Strachan

:: INVOLVEMENTS
SiteMesh
QDox
MockDoclet
OSCore
OSUser
PropertySet
RichTags
Marathon
SiteMesh C++
Alt-RMI
Jelly MockTags
more...
:: BACKLOGS
November 2002
Sun Mon Tue Wed Thu Fri Sat
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Oct   Dec



Click here to visit the Radio UserLand website.

Click to see the XML version of this web page.

joe@truemesh.com

:. 27 November 2002

  9:33:45 PM  

Blocks in Java and C#

James talks about some syntactical sugar enhancements that can be made to Java borrowed from C#. Although it sounds petty, this stuff really makes a difference when you're staring at code all day. Clean and readable code makes for relaxation which makes for smarter thinking. Syntax is closely related to karma.

The foreach and using operators are particularly nice as it provides a cleaner sequence for commonly executed sequences of events. But why stop there?

One of the features that made Smalltalk and Ruby so popular was the use of blocks. These allow you to create your own constructs.

Suppose blocks were added to Java/C#. We could create our own constructs. We wouldn't be limited to what the language designers thought we may need.

Here's an example of how a foreach facility could be added to Java/C#.

public class List {
  ...
 
  public block void foreach() {
    for(int i = 0; i < internalArray.length; i++) {
      Object item = internalArray[i];
      yield(item); // execute block
    }
  }
}

Usage:

list.foreach(Person person) {
  print(person.getName());
}

Okay, that's foreach added.

How about something similar to using that ensures a resource is always closed properly. Let's add some custom transaction management for fun.

public class Database {
  ...
 
  public block void using() {
    Connection con = ...; // open connection
    Transaction tran = con.beginTransaction(); // start transaction
    try {
      yield(con); // execute block
      tran.commit(); // commit
    } catch (Throwable error) {
      tran.rollback(); // rollback if error
      throw error;
    } finally {
      con.close(); // always close con
    }
  }
 
}

Usage:

database.using(Connection connection) {
  connection.execute("UPDATE blah");
  connection.execute("DELETE xxxx");
}

Let's add something to easily scroll through a result set:

public class Connection {
  ...
 
  public block void query(String sql) {
    ResultSet rs = getResultSet(sql); // open results
    try {
      while(rs.next()) { // iterate through results
        yield(rs.get(1), rs.get(2)); // execute block
      }
    } finally {
      rs.close(); // close results
    }
  }
 
}

Usage:

int totalAge = 0;
connection.query(String name, int age; "SELECT name, age FROM people") {
  print("Name : " + name);
  totalAge += age;
}

This is a sample of the syntax I'm suggesting - I'm sure it can be improved:

public class Test {
  public block int myBlock(int arg) {
    number = arg2 + 1;
    print(".start block");
    yield(n);
    yield(n);
    print(".end block");
  }
}
// usage
int result = test.myBlock(int number; 4) {
  print("..start custom");
  print("..." + number);
  print("..end custom");
  number += 2;
}
print("result: " + result);
// output
.start block
..start custom
...5
..end custom
..start custom
...7
..end custom
.end block
result = 9

Blocks are similar to anonymous inner classes, but offer two distinct advantages:

  1. The calling syntax is much simpler. Imagine using an inner class to iterate over a collection! Or imagine how ugly it would be have to use an inner class to apply a compartor to a sorting algortithm or if you wanted to use the visitor pattern. Oh wait, we do that don't we :).
  2. Scoping. Blocks have access to values available to the code calling the block and values inside the block.

Forget adding a couple of keyword extensions - add the ability to create our own extensions. Thanks, Steve for the conversation about this last night.


  7:31:26 PM  

Hmmmm..... now that this JavaBlogs thingy exists, I best organise by bloggings into categories before I get shot for harping on about .NET.


Web-design by John Doe from open source web design.