This post by J Skeet just brought me found memories of the “C” language ‘#’ preprocessor operator.
Monthly Archives: December 2009
ACM article by B. Stroustrup
The January 2010 edition of Communications of ACM contains an interesting article by Bjarne Stroustrup, named “What Should We Teach New Software Developers? Why?”
Recommended reading for this holiday season.
P.S.: Another related articleby Bjarne Stroustrup: Programming in an undergraduate CS curriculum, WCCCE’09, May 2009
JAXB Fluent API
Two weeks ago I taught a short session on C# 3.0 and LINQ, including LINQ to XML. One of the things that I really admire in this technology is the functional construction of XML elements.
Last week I started using JAXB to construct and consume XAdES digital signatures. One of the aspects that I immediately disliked was the way to build the object trees, using the classes generated by the binding compiler (XJC). Each of these classes has a parameterless constructor and a set of setter and getter methods to assign and retrieve each of the properties. Unfortunately the setter methods have void as the return type, meaning that an object initialization requires multiple statements. When building a deeply nested hierarchy, this implies code like the one showed below, where the tree is built in reverse order (from leafs to root).
1: Person alice = new Person();
2: alice.setName("Alice");
3:
4: Person bob = new Person();
5: bob.setName("Bob");
6:
7: Person carol = new Person();
8: carol.setName("Carol");
9: carol.setFirstChild(alice);
10: carol.setSecondChild(bob);
I find this code very unpleasant to write, namely when I’m familiar with better idioms, so I immediately started thinking in a way to solve this.
Fortunately, I found the XJC Fluent API Plugin, which extends the classes generated by the binding compiler with extra methods called “withXXX”. These methods are similar to the setter methods with the important difference that they return the instance on which they were called, allowing for method chaining. Using this, the above initialization code becomes
1: Person carol = new Person().withName("Carol")
2: .withFirstChild(new Person().withName("Alice"))
3: .withSecondChild(new Person().withName("Bob"));
Notice that the code structure now follows the element structure (from root to leafs). This makes it easier to read and also to write, since the IDE’s auto-completion feature can be used to “guide” the method chaining.
Some remarks regarding this plugin:
- The easier way to execute XJC with this plugin is to use an Apache Ant task.
- When using the Java SE 6 JAXB implementation, consider the following notes: Migrating JAXB 2.0 applications to JavaSE 6.
Using _ as an extension method
In C#, as in many other languages, “_” is a valid identifier (as reminded to me by Erik Meijer on a C9 Lecture)
Recently, I used this knowledge to create the following extension method:
public static class StringExt {
public static XElement _(this string s,
params object[] objs) {
return new XElement(s, objs);
}
}
Why? To enable this coding style
"html"._(
"body"._(
"table"._(
"tr"._(
from colName in colNames
select "th"._(colName)),
from e in seq
select "tr"._(
from f in colFuncs
select "td"._( f(e))))))
.Save(name);
The above fragment creates an HTML document containing a table, where:
- IEnumerable<T> seq is a sequence of elements, where each element corresponds to a table row.
- List<string> colNames is a sequence of column names
- Linked<Func<T, string>> colFuncs is a sequence of functions, where each function is used to project one sequence element (e) into a column cell.
Without extension methods, this fragment would be written as:
new XElement("html",
new XElement("body",
new XElement("table",
new XElement("tr",
from colName in colNames
select new XElement("th", colName)),
from e in seq
select new XElement("tr",
from f in colFuncs
select new XElement("td", f(e))))))
.Save(name);
More “noisier”, on my opinion.
It’s almost 2010…
… and sharing passwords is still a discussion subject!