tags:

views:

65

answers:

3

I'm a big fan of concise method names, so when our codebase has something like:

Account.getAccountId();

I like to add an alias so that I can just do:

Account.getId();

However, I've heard ... murmurs from elsewhere in my company about how this might be problematic, because the getId I define will interfere with a built-in getId that all Java objects have, or something like that. Also we use JRuby to reference our old Java classes, so the issue might have something to do with a built-in Ruby getId method too.

Still, I'm not entirely convinced that there is a problem (and I really like my short method names). So, does anyone out there know whether there are problems with defining getId methods (either in Java or JRuby), and if so what are they and can they be worked around?

EDIT: It seems from the responses so far (and what I already knew about Java) that the issue can't be with some core Java getId functionality (as there is none). So really this question is for JRuby people; if the getId "conflict" is coming from anywhere, it's got to be from JRuby stuff.

A: 

I tend to prefer readability over conciceness. To me getAccountId is clearer than getId

Given the 2 lines below it's much clearer that foo is an account object. (though I wouldn't really call an object foo)

foo.getId();
foo.getAccountId();

edit, the java class Object has no method getId(). I don't know ruby so you'll have to rely on others to let you know about that.

Glen
"(though I wouldn't really call an object foo)" I think that's the key: in a real situation you'd say "myAccount.getAccountId", "bobsAccount.getAccountId", etc. If you have good variable names, it's obvious what the variable is, and if you know what the variable is then "getAccountId" is just as redundant as "getAccountTotal", "getAccountPhoneNumber", or getAccount-anything else. I think most people would agree that prefixing every method name with the class name is kinda lame, so why should getId be any different?
machineghost
How about if you're accessing a collection of accounts using an iterator? I agree prefixing the class name to every method is bad, but getId() is just so generic that it reads better if you make it clearer what it refers to
Glen
accountIter.next().getId() seems ok to me :-) For me getId > getAccountId, so if I can get away with it I will, but ultimately it's a subjective preference thing, and I don't expect to convince you one way or another.
machineghost
+1  A: 

However, I've heard ... murmurs from elsewhere in my company about how this might be problematic, because the getId I define will interfere with a built-in getId that all Java objects have, or something like that.

The java.lang.Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.

But Object doesn't have any getId a method. And so do all Java objects.

Pascal Thivent
Thanks; it really sounds like it's either a JRuby issue or a non-issue then (but certainly not a Java issue).
machineghost
Yes, that's it (actually, I think it's a non-issue but I'm not able to confirm).
Pascal Thivent
A: 

The problem I would see is if on an object a getId method (or similar method name) were later added that conflicted with your alias, it would break code. When someone has an object, adding a new method should be a non-breaking change (serialization aside), but in fact they would be breaking your code because of some aliasing that you did.

So the problem isn't that it conflicts with a getId that is defined for all objects, but that it could conflict with a method that will be added later to the same class. One way around this might be to make a wrapper class that has the short method names, so that the delegation is clear and static and if the method name is added, it might create confusion, but it would be more containable.

Yishai
That is a real risk, but not in these cases (nothing expected getId before I came along and started adding it to our classes). The concern has (ostensibly) something to do with core Java/JRuby stuff, not with our issues in our code.
machineghost
Although I'm far from a JRuby expert, I'm pretty sure there is no such global method affecting JRuby. You can try yourself by calling that method on an object without adding the alias. My point was more the concern that this would conflict with some future method, not something existing.
Yishai