ModeShape

An open-source, federated content repository

Java developers should learn Ruby

Okay, it doesn’t have to be Ruby. Pick some other language. Pick Erlang, or even JavaScript. It doesn’t matter really matter, as long as it’s different enough from Java that it actually requires some effort, and that you keep your mind open to new things.

Why bother? Because learning other languages will make you a better Java developer. Seriously.

Learning another language forces you to dive into a different community. You’ll find different ideas and different approaches to many of the same problems. They may not be better ideas and approaches, just different. Other communities often have a fresh perspective on similar problems. And sometimes it will even make you appreciate what we really do have in the Java community (like a huge number of really great libraries).

Learning another language can teach you new idioms. Some you may be able to use in Java, and others you won’t. Ruby blocks, for example, are Ruby’s form of closures and are widely used in most Ruby programs. They’re extremely useful for running predefined code that delegates (perhaps repeatedly) to the block for custom behavior. Here’s a simple example of iterating over an array and doing some custom behavior (printing the element):

animals = ['lion','tiger', 'bear']
animals.each {|animal| puts animal }

Unfortunately, Java doesn’t have closures. Not really. The closest thing in Java 6 is to pass an anonymous inner class in much the same way that listeners are often used in GUI applications. All we need is a predefined interface and a method on a class that accepts the implementation and that performs the iteration (like the “each” method in Ruby). So pretend that java.util.List has an “each” method that takes an implementation of an OnEach:

public interface OnEach<T> {
    void run(T obj);
}
public interface List<T> ... {
    void each( OnEach&lt;T&gt; action );
}

Then our example would look something like this:

List<String> animals = Arrays.asList( new String[]{"lion", "tiger", "bear"} );
animals.each( new OnEach<String>() {
  public void run( String animal ) {
    System.out.println(animal);
  }
});

Kinda gross, huh? But even though it’s not as easily done, it’s a pattern that you can use in your designs to allow custom behaviors without requiring subclasses. There are several closure proposals for Java 7, but none are as easy as in Ruby or JavaScript. By the way, Alex has the best resource for all things Java 7.

Learning another language also forces you to use different tools and processes. One example in Ruby is RSpec, which is a Behavior Driven Development framework focused on specifying and verifying behaviors. BDD is a rich topic that I’ll explore in another post.

Another example is autotest, a great little tool from ZenTest that takes continuous integration to a whole new level. It works on your local machine (rather than a remote continuous integration server), and it simply monitors your development environment’s file system for changes to source files and runs the unit tests corresponding to any changed file(s). If those tests pass, then it runs all of the tests. It’s simple, elegant, and allows you to focus on changing the code, yet still get feedback from your tests. It’s like JUnit or TestNG Eclipse plugins that automatically run your unit tests as you work on the code.

The bottom line is that the Java community doesn’t have the market cornered on good ideas. Java is great and will continue to be, but it does need to evolve. Java first appeared over 13 years ago, and some of us have been developing primarily in Java for most of that time. Go exploring, and I’ll bet you’ll become a better Java developer for it.

Have you changed how you develop Java after learning another language?

Advertisements

Filed under: techniques, tools

7 Responses

  1. Joshua Jackson says:

    Great Post. I totally agree with you. From my point of view the trends nowadays is to build library with Java, but build the apps with dynamic language like JRuby or Groovy

  2. Daniel K says:

    After working with Groovy quite some time, I miss the ability to pass snippets of code around (closures) and just yesterday my life would have been easier if I could have had Groovy’s support for “currying” (Higher order functions) in Java.Have you looked at easyb? It is like RSpec for Groovy/Java.

  3. Randall says:

    Joshua: I hear that more and more from people, especially since JRuby has made such significant gains in recent months. I don’t follow the Groovy community much, but have seen a lot of interest lately.

  4. Randall says:

    Daniel: Great pointer. I had not heard about easyb, but it looks pretty interesting. I’ll definitely take a deeper look.

  5. paulk says:

    Groovy is great choice for Java shops. It gives you everything that Ruby gives you but in a nice Java friendly way. Learning to use it properly certainly stretches you in new ways but you can also use it with a Java style if you just need to get something done.

  6. James says:

    Great post, and I think it would be great to have up on JavaLobby, to see what the community’s opinion is – if you’re interested please send me a mail to discuss further to james at dzone dot com

  7. dwairi says:

    I fully agree with you.learning another language also open your eyes on things in the very same language you are playing with. I am a Java Developer and I used to write ruby code for the last 2 years, the method_missing in ruby open up my eyes on “Java Reflection” (well, not exactly the same).Great post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

ModeShape is

a lightweight, fast, pluggable, open-source JCR repository that federates and unifies content from multiple systems, including files systems, databases, data grids, other repositories, etc.

Use the JCR API to access the information you already have, or use it like a conventional JCR system (just with more ways to persist your content).

ModeShape used to be 'JBoss DNA'. It's the same project, same community, same license, and same software.

ModeShape

Topics

%d bloggers like this: