//polyglot programmer /
Polyglot Programming on the JVM
Tips about how and why to choose a non-Java language for your project
The following is an excerpt from The Well-Grounded
Java Developer, which Manning
Publications is due to release in
January 2012.
The phrase polyglot programming on the JVM is relatively new.
It was coined to describe projects
that utilize one or more non-Java
Java Virtual Machine (JVM) languages alongside a core of Java
code. One common way to think
about polyglot programming is
as a form of separation of concerns. As you can see in Figure 1,
there are potentially three layers
where non-Java technologies can
play a useful role. This diagram
is sometimes called the polyglot
programming pyramid and comes
from the work of Ola Bini.
Within the pyramid, you can
see three well-defined layers:
domain-specific, dynamic, and
stable. Table 1 shows these three
layers in more detail.
As you can see, there are patterns in the layers—the statically
typed languages tend to gravitate
toward tasks in the stable layer.
Conversely, the less powerful and
more specific-purpose technologies tend to be suited to domain
roles at the top of the pyramid.
In the middle of the pyramid, we
see that there is a rich role for languages in the dynamic tier. These
languages are also the most flexible; in many cases, overlap might
exist between the dynamic tier and
either of the neighboring tiers.
Let’s dig a little deeper into this
diagram and look at why Java isn’t
the best choice for everything on
the pyramid.
LAYER
DOMAIN-SPECIFIC
DYNAMIC
STABLE
DESCRIPTION
EXAMPLES
APACHE CAMEL DSL, DROOLS,
WEB TEMPLATING
JAVA IN ACTION
GROOVY, JYTHON, CLOJURE
JAVA, SCALA
Table 1
BENJAMIN J. EVANS AND
MARTIJN VERBURG
However, these same attributes
become a burden in the middle
and upper tiers of the pyramid;
for example:
■ ■ Recompilation is laborious.
■ ■ Static typing can be inflexible
and lead to long refactoring
times.
■ ■ Deployment is a heavyweight
process.
■ ■ Java’s syntax is not a natural fit
for producing DSLs.
A pragmatic solution is to play
to Java’s strengths and take advantage of its rich API and library
support to do the heavy lifting
for the application—down in the
stable layer.
If you’re starting a new project
from scratch, you might also find
that another stable layer lan-
guage (for example, Scala) has a
particular feature (for example,
superior concurrency support) that
is important to your project. In
most cases, however, you should
not throw out working stable layer
code and rewrite it in a different
stable language.
ABOUT US
blog
PHOTOGRAPHY BY
JOHN BLYTHE AND BOB ADLER
Figure 1
Why Use a Non-Java Language?
Java’s nature as a general-purpose,
statically typed, compiled language provides many advantages.
These qualities make the language
a great choice for implementing
functionality in the stable layer.
How to Choose a Language
for Your Project
Once you’ve decided to experi-
ment with non-Java approaches
in your project, you need to iden-