Naming confusion

Hello all.
Noticed this today:

require ‘java’
textArea = javax.swing.JTextArea.new
textArea.preferred_size
=> nil

textArea.getPreferredSize
=> java.awt.Dimension[width=0,height=16]

Shouldn’t these two methods be the same?
Thanks.
-roger-

Good question. Component does in fact have a mangy old
preferredSize() method on it. That short cut probably points at that
instead of getPreferredSize().

-Tom

On Tue, Jun 19, 2012 at 12:44 PM, Roger P. [email protected]
wrote:

http://xircles.codehaus.org/manage_email


blog: http://blog.enebo.com twitter: tom_enebo
mail: [email protected]

yikes. That method should have been deprecated and removed years ago if
it doesn’t even call out to getPreferredSize.
But that probably explains it. Thanks!
-r

On Tue, Jun 19, 2012 at 12:48 PM, Thomas E Enebo [email protected]
wrote:

Good question. Component does in fact have a mangy old
preferredSize() method on it. That short cut probably points at that
instead of getPreferredSize().

-Tom

Can we anticipate a change to the effect of JRuby always trying the
get* version first? I would imagine that sort of error would take a
long time to track down; and maybe it’s just me, but when that sort of
thing bites me a few times, I start being extra defensive, e.g. adding
get_ in the 99% of cases where it’s not necessary.

On Tue, Jun 19, 2012 at 7:40 PM, Eric C.
[email protected] wrote:

Can we anticipate a change to the effect of JRuby always trying the
get* version first? I would imagine that sort of error would take a
long time to track down; and maybe it’s just me, but when that sort of
thing bites me a few times, I start being extra defensive, e.g. adding
get_ in the 99% of cases where it’s not necessary.

The problem with that is all the other cases where you do want the
non-get method to be bound to the shortcut name. It’s not a good
practice in API design to mix both bean-style and “bare” getters like
this, but it does happen.

Our policy has always been this:

  • Bind all standard Java names
  • Bind underscore names for all methods
  • Bind more complex renames like get/set

At each stage, we only bind the name if it has not already been bound,
so preferredSize wins preferred_size because it’s the earliest,
simplest alias.

Probably just have to chalk this up to a bad API in Java. Our aliasing
is done mostly as a convenience and it usually works fine. Using get_
defensively isn’t a bad idea, but it’s only necessary in cases like
this.

  • Charlie