views:

324

answers:

8

I like how Groovy makes all of your instance variables and class variables public and writes getters and setters on your behalf. This saves a lot of unnecessary coding. On the other hand, my boss hates it, making me very sad :(

Is there a way to achieve the conciseness of Groovy in Java without having to use Groovy or some other framework/add-on?

I have a feeling the powers that be will not take kindly to the introduction of this foreign framework where I work.

So what I'm looking for is a solution to the excessive verboseness of Java without using Groovy or something like it.

Can it be done in Java alone - such as by simply making everything public?

+6  A: 

The Java language is what it is. You won't find much to change the core language without moving to a new language (like groovy). Getting buy-in from management might not be as hard as you think to move to groovy. I was in a similar position as you a few years ago. You can start introducing groovy into a few areas of your software that make sense.

A great place to start is to start using it for unit testing. Essentially when you write a class in groovy, it compiles down to Java bytecode. As long as you have the groovy jars on your classpath, others may not even know it is groovy code. By using it for testing, you can demonstrate how it simplifies your code and makes things easier. You should be able to sell that to management.

From there, keep introducing it in other new areas where a dynamic language would save you some work. Groovy and Java mix very well together since they use the same set of core libraries.

Chris Dail
A: 

I'm not aware of anything to make your source code smaller, apart from more effective frameworks for specific tasks (JPA, Swing, etc.).

That kind if functionality usually comes from your IDE.

I use Netbeans which has tools to write getters/setters, auto implement abstract methods and various other shortcuts. Of course the source code is still just as verbose.

Cogsy
+5  A: 

In this kind of situation, an IDE which can write the getters and setters for you can be your friend.

I find that having to write the getters and setters are the most repetitive and un-interesting part of writing new classes. With an IDE like Eclipse, which can automatically generate the getters and setters for you, it can reduce the pain of writing the redundant code.

Of course, it won't alleviate the need to use the getX and setX methods, but at least it makes the implementation of the classes a little bit easier and less of a burden.

coobird
Cool. I use Eclipse but didn't know it had this feature.
Harry Quince
Along with the refactoring tools, I found the getter/setter generation capability of Eclipse is a very useful feature.
coobird
IntelliJ IDEA will also generate getters and setters.
Josh Brown
+1  A: 

You could use AOP to help take out some of the verboseness.

For example, if you have a property: string name;

You could then define: @GetterSetter public String Name(String name) { };

Then have an aspect that looks for the annotation, and put an around on it, so that any reading, which is when the parameter is null, or writing (parameter isn't null) is done then it will take the appropriate action.

This would be pretty easy to write, but then you have to get buy-in for AOP, which may be challenging.

But, as others mentioned, Eclipse and Netbeans will easily create the getters/setters, so if you don't have to do anything special with them then it should be pretty easy.

I tend to put my properties in their own class, which is just getters/setters, then I don't have to look at the verboseness, as well as equals and hash methods if needed, then in my other classes I just use that, and hide the boring extra coding.

James Black
+3  A: 

Go with immutable datastructures. No getters, no setters, no hassle.

You may want to give Functional Java a try. It's just a regular Java library, but comes with powerful abstractions and useful (immutable) datastructures that let you say more in less code. For example, translate this:

List<String> s = new ArrayList<String>();
  for (String x : xs) {
    for (String y : ys) {
      for (String z : zs) {
        s.add(doSomething(x, y, z));
      }
    }
  }

... to this:

List<String> s = xs.bind(ys, zs, doSomething);
Apocalisp
Interesting. Does this add features similar to blocks in Ruby?
Harry Quince
Several types are provided that serve as generic closures ("blocks" in Ruby). There is F<A, B> which is a function from some type A to some type B. There's Effect<A>, which models an action that takes some argument of type A. And there's P1<A>, which yields a value of some type A.
Apocalisp
Also, if you're willing to go with the BGGA precompiler, it makes the use of these structures a lot less verbose. This is not required, but it looks nicer.
Apocalisp
My boss likes to keep things stupidly simple enough so that he can understand the code - so I have a feeling this won't fly. Interesting though. Has this library been adopted widely?
Harry Quince
Nice tutorial here: http://www.javac.info/
Harry Quince
Depends on what you consider "widely". The library is relatively new, but I've seen it used in mission-critical production systems. Developers like it because it reduces repetition. Your boss might like that too.
Apocalisp
if your boss does not like groovy for non-technical reasons (e.g., aesthetics, personal preferences etc), then he/she would be unlikely to accept functional java...
Chii
A: 

Try out my bean annotations : http://code.google.com/p/javadude/wiki/Annotations

The annotation processor generates a superclass for you with lots of the typical boilerplate you'd normally have to write:

@Bean(
    properties={
        @Property(name="name", bound=true),
        @Property(name="age", type=int.class, bound=true)
    }
)
public class Person extends PersonGen {
}
Scott Stanchfield
A: 

You can greatly increase Java's effective abstraction level by using domain-specific frameworks. For example, Restlet takes care of most of the boring details when you write RESTful web services. Or, if you need to map POJOs to and from XML or JSON, packages like XStream or JAXB automatically generate the mapping logic. To avoid tedious JDBC programming, try a persistence manager like iBatis or Hibernate. Templating systems like Freemarker and Velocity make it much easier to generate markup.

Even where there is no tool support, you can often find ways to factor out low level details into common methods and classes so that your top level code feels more abstract.

(You might consider asking your boss if you can evaluate Groovy in a supporting role on your project, for instance to write some of the unit tests.)

Jim Ferrans
A: 

You can label any property or method with any of the standard access modifiers or type declarations in Groovy.

Remember, Groovy is a superset of Java.

Allen