Search code examples
javagroovyverbosity

I'm looking for a solution to the excessive verboseness of Java without using Groovy


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?


Solution

  • 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);