Two additional Ruby features I wish were in Java

I’ve been tinkering with Ruby a little, mainly so I know what I’m missing since a lot of people really seem to dig it. Ruby has a lot of nice features that I like including mixins, closures, Rails scaffolding, active record ORM, etc…

The things I don’t like about Ruby are that it’s weakly typed which I’ve written about before and has a very terse syntax that makes it hard to understand at times if you don’t “know” Ruby. For example when I first started looking at Ruby code these constructs left me wondering: variable, :variable, *variable, @variable, |variable|, and @@variable.

While playing with Ruby over the weekend I found two more language features that Ruby has that I really wish we had in Java:

1. Immutability: you can freeze objects so that any attempt to change an immutable object will result in TypeError exception reporting “can’t modify frozen TYPE”. I would love to have this in Java.

2. Native getter/setter support: declaring a list of variables as attr_reader or attr_writer allows getter/setter functionality of a variable without writing/maintaining any redundant getter/setter code, yet if necessary, you can override the default getter/setter behaviour without affecting the API. In Java it drives me crazy that we waste time and clutter our classes unnecessarily with hundreds of lines of getter/setter methods that all do the same thing. Java really needs a construct for default getter/setter behaviour on selected variables that can be overriden when necessary.

The only other feature that neither Ruby nor Java have that I’ve written about before is language support to guarantee that a method variable cannot be passed as null such as public boolean doSomething(notnull Object foo)

What features do you wish you had in Java that you’ve seen in other languages?

This entry was posted in Java, Ruby. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *