Code Monkeyism

Programming is hard by Stephan Schmidt

@Getter annotation?

With all the innovation in annotations, see Web Beans or Google Guice, just a saturday morning idea: Why not drop the annoying get* convention and replace it with some annotations? The API wouldn’t be as short, but the usage of a getter looks nicer and more fluent. And of course the IDE could hide the @Getter annotation, just as it hides the imports.

@Getter
public Context context() {
	return context;
}

public Context getContext() {
	return context;
}

About the author: Stephan Schmidt is currently a team manager at ImmobilienScout24 in Berlin. Stephan has been working as a head of development and CTO. He has used a lot of different technologies in the last 20 years including Java, Rails and Python. Stephans main field of interest is maintainablity and productivity in software development. Want to know more? All views are only his own.

If you did like this article but you don't want to subscribe to new articles with your reader, you can follow me on Twitter or subscribe to new posts with your email:

Comments

Not entirely sure what you’re looking for, but you can do this with PropertyDescriptors.

stephan

Care to explain?

And then we make the parenthesis..
or just use scala ;-)

http://www.scala-lang.org/docu/files/api/scala/reflect/BeanProperty.html
(with val only get* will be produced)

stephan

Or use Groovy ;-) Scala is nice indeed. I don’t think it will get into the mainstream but it might show the way for a Java future (type inference, actors, class matching).

And of course: away with the parenthesis.

Peter Lawrey

Another option, if the getter doesn’t do anything why not have a public final field?

stephan

A final public field makes it hard to change the behavior in the future I guess. Groovy/Scala/Ruby properties would be best. Hope for Java 7.

The lowercase getter is already used: It is the accessor to a final field. (See ordinal(), name() in Enum)

When I look at the discussions over Java 7 I am all but sure if I want to use this Java. Luckily there are Scala and C#. I don’t want a Smalltalk Java nor a Google Java (interesting that the Google Collections look horrible now but will look nice as soon as the Google suggestions will make it into the language)

Jim

Quite a lot of discussion about this for Java 7:
http://tech.puredanger.com/java7#property

stephan

@Carsten, @Jim: Yes, I’m following the discussion with much interest. Let’s hope Java gets better property handling with Java 7. Perhaps even something Javascript like (which is beautiful in it’s simplicity), combining properties with BCGA:

   myclass.setX = {int x => this.x = x}
   myclass.x = 3

Leave a Reply