Why “Eclipse Generates it Automatically” is a Bad Answer

A plead for conciseness

I want my code to be simple, short and concise. I hate silly boiler plate code. Code that doesn’t add business value is code I don’t want to see.

Welcome to the Java world. Well-educated Java classes – and beans in particular – consist almost entirely of boiler plate code:

  • Constructors or factory methods
  • serialVersionUID
  • Getters and setters
  • toString(), equals(), hashcode()

What about business code? Sometimes it’s hard to spot it among all the boiler plate code.

The fascinating thing is virtually nobody actually sees this boiler plate code. Experienced Java programmers are experts in masking out getters, setters and the like. It’s almost like the code wasn’t there in the first place.

And whenever I complain about it, the answer typically is: “It generated by Eclipse, so what’s the matter?”.

Code that ain’t there is code you ain’t have to understand

Obviously most of the people I talk to are concerned primarily with writing code. However, most source codes are read far more often than they are written. 1 So even if you’ve managed to turn your brain into a powerful pattern matching engine able to mask out boiler plate code using the pattern matching still slows you down. You may not recognize it, but this kind of code costs you energy and time.

However, it doesn’t cost you enough energy and time to do something about it. There are far more developers programming Java than any of the alternative JVM languages. So this doesn’t seem to be a real problem to most people.

Boiler plate code ruins your PowerPoint presentation

What verbose boiler plate code does to your presentation. (C) 2013 Stephan P. Rauh That’s a nasty one. It’s not a problem to the majority of Java developers. But it’s a problem to everyone holding a public talk about Java programming. The audience typically knows the boiler plate code by heart, but it belongs to the classes, so you have to decide wether to show or omit it. You don’t want your presentation look like the ebook example at the right hand side?

Boiler plate code doesn’t evolve
automatically with its class

Yes, it’s true: modern IDEs like Eclipse, Netbeans and IntelliJ IDEA generate all the boiler plate code at a mouse click. But what happens when the class evolves? Just think of the serialVersionUID. You are expected to modify it each time you add or change an attribute of your class. If you’re using generated versions of equals(), toString() and hashcode(), thing get even worse. Those methods will not be updated automatically. They will become wrong over time. Adding insult to injury, it may take a long time until anybody notices. The boiler plate code is important, but the errors are subtle and are not guaranteed to occur during the tests.

Apache’s common-lang libraries has some nice methods to solve the problem. The catch is Eclipse doesn’t generate this kind of code. You have to do it manually.

 @Override
    public boolean equals(Object o) {
      return EqualsBuilder.reflectionEquals(this, obj);
    }

    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }

How to get rid of boiler plate code

This is out of this article’s topic, so suffice it to present a short list:

  • Lombok is a somewhat dirty hack to the Java compiler. Apart from that, Lombok is a great tool to simplify your code.
  • You can use another JVM language like Groovy or Scala instead of Java.
  • In many cases, you can omit a lot of the boiler plate code. As long as you know what you’re doing, this may be a good solution. But beware, this approach can also cause errors.

Conclusion

It up to you wether you share my opinion that source codes should be short and concise. You are free to object. But if you do, beware of the caveat of virtually invisible code: you may not see it consciously, but your computer does. Even trivial code can contains errors, especially if your class grows. If so, your aptness in masking out boiler plate code mentally works against you:

Code you don’t see is code you can’t fix. 🙂


  1. To be precisely, every source code is written only once.

Leave a Reply

Your email address will not be published.