Xtend: First Impressions

We’re hiring a lot, and that means a lot of interviews. Being keen to help find our future colleagues, a bunch of us are training to run the technical selection process, which includes a Java coding test.

As a trainee interviewer for Java developers myself, I need to get intimate with the test’s codebase. What better way to do so than to port it to an interesting new language?

Xtend?

Head to the Xtend website for juicy details, but in summary, Xtend:

I won’t share the resulting Xtend coding test implementation with you: it‘s close enough to the original Java version that it would ruin the surprise for candidates. However, I can give you my impressions of Xtend after a few hours of dabbling.

 

The Language

The language delivers on its promise: it is very approachable from the perspective of a Java developer and is effective at making code more concise.

 

Extension Methods

This is my favourite feature. Extension methods can be called as though the first argument were the receiver type, regardless of where the method is declared. So for example, Xtend’s own library (which is written in Java) provides the following extension method for Iterable:

[cc lang=”java” escaped=”true” line_numbers=”0″]public class IterableExtensions {
//…
/**
* Determines if the given iterable is null or contains no elements.
* @param iterable the to-be-queried iterable. May be null.
* @return {@code true} if the iterable is null or contains no elements
*/
public static final boolean isNullOrEmpty(Iterable<?> iterable) {
return iterable == null || isEmpty(iterable);
}
//…
}[/cc]

IterableExtensions.java

That method can can be consumed in Xtend as follows:

[cc lang=”java” escaped=”true” line_numbers=”0″]class Zombie implements Dismemberable {
    def feedOn(Collection<Brain> brains) {
if (!brains.nullOrEmpty) {
brains.forEach(brain | this.eat(brain))
}
}
}[/cc]

Zombie.xtend

In the code above, brains.nullOrEmpty invokes IterableExtensions.isNullOrEmpty(), and brains.forEach invokes another such extension method that takes a closure as an additional parameter.

Xtend’s runtime library provides a bunch of these utility extensions, and you can of course implement your own.

 

Templating

Templating using Rich Strings is presented as one of the language’s main features. I haven’t really made use of it yet; this video shows how it can be used for code generation.

 

Misc observations

Aside from that, here’s what I considered noteworthy after a couple of hours:

 

The IDE Experience

Overall, for a young language, the Eclipse tooling is excellent. This is unsurprising given Xtend’s background, and is one of the language’s big selling points. Nonetheless, there are a few gotchas:

There is no IDEA tooling that I know of for Xtend. Use Eclipse.

 

Conclusion

I won’t try to claim you should stop learning your favourite new language and switch to Xtend: it’s relatively frugal on the feature front, and therefore perhaps less exciting than others.

That said, it’s quick to pick up. You could get the hang of it during a short break away from that beefy Scala book. Porting a couple of simple classes was enough for me to realise that the syntactic sugar is simple, yet very neat. I predict I’ll soon regret not having it at hand when writing plain Java.

So, if you’re after a gradual improvement on top of Java, or are keen to write more succinct code with minimal impact on the runtime requirements of an existing Java app, give it a try!

By the way, check out my Grovember Mo’. Donate if I look ridiculous! 🙂

Exit mobile version