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']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
animals.each {animal puts animal }
java.util.List
has an "
each
" method that takes an implementation of an
OnEach
:
public interface OnEach<T> {Then our example would look something like this:
void run(T obj);
}
public interface List <T> ... {
void each( OnEach <T> action );
}
List<String> animals = Arrays.asList( new String[]{"lion", "tiger", "bear"} );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?
animals.each( new OnEach <String> () {
public void run( String animal ) {
System.out.println(animal);
}
});