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 <T> 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?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值