Wednesday, June 18, 2008

Modern Java From a Python Perspective

I had a friend visit me today. He's a Java programmer, but he also knows enough Python, Ruby, JavaScript and Scheme to be dangerous. I asked him to show me around his code so I could see some modern Java. I learned Java back in the 1.0 days, but I stopped paying attention back around 1.4. Here are some of the things I saw.

Generics are as I expected.

Annotations are interesting. There was code like:
    @SomeClass
public void method() {
...
}
"SomeClass" acts like a "tag" for "method". Elsewhere in the code, you can define wrapper methods (i.e. decorators) for all the methods that have been wrapped by "@SomeClass". Clearly, this was the inspiration for Python's function decorators. I had simply never seen them in Java before.

Dependency injection is interesting, as I expected it to be. In general, you accept parameters for everything you need, and you don't worry a lot about how things get instantiated. You avoid using the "new" keyword, and instead let the dependency injection framework instantiate things for you. This takes care of circular dependencies. There was a Java file that configured what gets bound to what. Hence, if I ask for SomeFoo, it'll give me an instance of the right SomeFooImpl. Basically, it's a whole layer to add more dynamism.

IntelliJ was impressive. I asked him to write a module from scratch so I could see his workflow. He right clicked and hit "add new class". IntelliJ created a file and asked if he wanted to add it to Subversion. Then he simply started typing in the implementation for the class. Whenever he typed something like FileWriter, it automatically added the right import line. Whenever he used a class attribute that didn't exist yet, it showed up in red until he told the IDE to create the attribute. Then, he had to click on the attribute to add a getter and setter.

I knew that Java IDEs held your hand a lot. I didn't know that the work flow was to simply start using something and then have the IDE generate the boilerplate for you. That was interesting. I liked how nicely he was able to navigate the code base. Best of all, IntelliJ has really nice Vim keybindings, which he was using.

He showed me Struts 2 and his usage of JSP. Those weren't all that remarkable or new to me.

I returned the favor by showing him some of the code from my current project. He said, "Wow, your Python code is really beautiful," which I took as quite a compliment. He also said that there was clearly a lot less boilerplate and that more functionality was achieved in fewer lines.

Clearly, a lot of useful code is written in Java. glassfish, Lucene, HiveDB, and IntelliJ are just a few things that come to mind. I'm not going to give up Python anytime soon, but it was nice to get a peek into another world.

24 comments:

Brian Luft said...

Nice write up. It's a great to be passionate about a language but I get a little worried when I see developers becoming too fanatical about a particular language. Learning or learning about other languages helps solidify one's knowledge about their language of choice. When we compare and contrast different languages we gain a deeper understanding.

philhassey said...

Really nice post :)

lotrpy said...

funny post, r u ONLY using python now?

Shannon -jj Behrens said...

> When we compare and contrast different languages we gain a deeper understanding.

Agreed, although I've been spending a lot more time with weirder languages like Erlang and Haskell than I have with Java ;)

Shannon -jj Behrens said...

> funny post, r u ONLY using python now?

I was using Ruby for a previous startup. I often use a bit of sh, sed, awk, and make. I often play with other weird languages.

However, I feel most comfortable using Python.

Anonymous said...

Weird. I use ruby myself but I never understood people who had the need for awk, sh etc..

I write the code in Ruby, sometimes a bit C if needed, and the rest will lie in human readable files (either yaml or a simpler and error tolerant format yet as short or even shorter as yaml)

I could see myself picking up Python but I would never see myself picking up Java. I simply dont see any gain in it compared to ruby or python.

Shannon -jj Behrens said...

> I never understood people who had the need for awk, sh etc..

Sometimes a tiny bit of sh really hits the spot. When I use awk, it's mostly for one liners. Awk is still lean and fast.

> I could see myself picking up Python but I would never see myself picking up Java. I simply dont see any gain in it compared to ruby or python.

You're preaching to the choir ;)

Nonetheless, I hate not knowing stuff. Hence, if there's something cool going on in some other language, I want to know about it to see if I can apply it to my own code.

ngn said...

As some who would qualify as ``dangerous'' (lots of Java, IntelliJ fan, good command in JavaScript, moving to Python), I have to say that I would love to see an IDE to do the same job for Python as IntelliJ does for Java. And being a dynamically-typed language is no excuse for Python not to have good tool support. You should just see how IntelliJ guesses types of JavaScript variables, for instance, and gives adequate completions.

ngn said...

As someone who...

M said...

>> He said, "Wow, your Python code is really beautiful," which I took as quite a compliment. He also said that there was clearly a lot less boilerplate and that more functionality was achieved in fewer lines.


I think this is why there aren't many IDEs for Python as Java IDEs. Python needs tools, but they need to be different. IPython is one good example.

lotrpy said...

@jj
sorry, I am not a native english speaker, what's the mean of "You're preaching to the choir" ?
@ngn
My favorite Python IDE is wingide, especially for the auto complete and debuger. maybe it's not as powerful as IntelliJ , no clues here.

Anonymous said...

'Preaching to the choir': promoting a belief to people who already hold it.

lotrpy said...

i understand "'Preaching to the choir'" now,thanks for the reply

Shannon -jj Behrens said...

> I think this is why there aren't many IDEs for Python as Java IDEs. Python needs tools, but they need to be different. IPython is one good example.

Agreed. I don't write my code in an IDE with autocompletion. I *run* my code as I write it in IPython, which *has* autocompletion.

Shannon -jj Behrens said...

> sorry, I am not a native english speaker, what's the mean of "You're preaching to the choir" ?

It's like telling President Bush that he should love America.

I already love Python. No one needs to tell me how good it is ;)

> My favorite Python IDE is wingide, especially for the auto complete and debuger. maybe it's not as powerful as IntelliJ , no clues here.

WingIDE is generally considered the best IDE for Python. It's good. However, IntelligJ probably has an order of magnitude more features, such as refactoring support.

Anonymous said...

To have some nice autocompletion, auto adding import lines... etc. try Pydev which is a nice Eclipse plugin for Python development.

There's also Pydev Extensions which is payware, but seems to complete what Pydev lacks.

Give it a spin.

Shannon -jj Behrens said...

> Pydev

Thanks for the tip. I always feel overwhelmed when I try out Eclipse. One of these days, I think I need to take a tutorial, or something. I'm sure it's nice once you get up to speed.

Shannon -jj Behrens said...

Heh, this post made it to DZone. Thanks guys!

Andreas Schobel (aschobel) said...

Dependency Inject is very cool stuff. As Joshua Bloch said, it is the future of Java.

The framework I showed you yesterday is called Google Guice:

http://code.google.com/p/google-guice/

And thanks again for lunch =)

Brandon L. Golm said...

I have to say that this talk of IDE and toolchain misses something critical. You need autocomplete in Java because that language is so hideously verbose. Think about the rest of the tool-chain and similar thoughts follow (like that more code fits on a screen and isn't as ugly, so it's easier to deal with; more fits in your brain so there's less need for tool support, etc). The IDE solves a problem of a language's, not general coding problems. That's, IMO, why they aren't as popular in other languages. You just don't need eclipse in Python. Though, maybe I'm overstating this all a tiny bit.

Alex said...

Those Java IDEs look impressive until you start using them for real project. That's where nightmares start. I'm not saying they are evil but... they are. I've seen so many dummy Java class constructors, getters/setters, etc. People get used to code that their favorite IDE produces. They don't know the language any more. They learn to click buttons to produce code.

Anonymous said...

> You should just see how IntelliJ guesses types of JavaScript variables, for instance, and gives adequate completions.

my vim guesses types and gives adequate completions

ey said...

There is actually a big difference between Java annotations (@SomeClass) and Python decorators (@SomeCallable).

With Python decorators, you can actually modify the function by passing it to a callable which can wrap it in another function. With Java annotations, @SomeClass is just a simple struct of primitives (strings, numbers, arrays) that can be queried using reflection, but has no impact on the function itself -- annotation classes have no executable code.

Andreas Schobel (aschobel) said...

@ey

I showed jj annotation in the context of using them with AOP Alliance method interceptors.

Guice does a really nice job in making AOP much more convenient to use.

Good overview of Guice and Interceptors