7 Apr 2010 - Digital Strategy // By Productive Edge Team

To Bean or not to Bean!

Not claiming to be a Shakespeare of Java, sometimes I ask myself if having everything revolving around beans in Java was the right thing to do. Those stuck in the Java world long enough probably remember that the original Java Bean specification came out around 1997 and was directly targeting the Java tools such as IDEs. Those really interested  what Java Beans are, can take a look at my examples and slides for the course I teach at DePaul University on this subject: http://igorpolevoy.com/public/Wiki.jsp?page=JavaBeans.

Ever since this spec was introduced there has been a proliferation of various  "bean" technologies in Java, such as Entreprise Beans (EJBs), Managed Beans (JMX), Managed Beans (JSF), singleton beans (Spring), etc.

If you ask a modern Java developer, what a bean is, the question will invariably be "a thing with getters and setters".

IMHO, it appears that this mindless approach to volumes of setters and getters eventually led to millions (billions?) of useless lines of code (getters and setters) that clutter most Java projects. The worst part is that in 99.99% cases, these are one line methods that do nothing. Developers tell themselves that the "encapsulation is one of the cornerstones of OOP". I say: "encapsulation is one of the cornerstones of OOP ... if you have something valuable to encapsulate.".

Of course if Java is dependent on this type of a paradigm, why not support it? I think that  Java is long overdue for a direct property (simple types at least) support in the language. For instance, Java Annotations could be used to add this information and a compiler would just generate appropriate method calls:

@Property("firstName", "lastName")
class Person{
private String firstName, lastName;

Here, with one line of code, I can tell compiler to generate appropriate getters and setters, while accessing these in code would be something like this:

//context of another class
Person p = new Person();
p.firstName := "John";

This is really not a JCP proposal, or a serious undertaking, just daily bitching whenever I have to write/generate these. When I see other languages like Ruby having first class property support built directly into the language on one hand, and Java depending on these badly and having no support whatsoever, I ask myself a question: why???

To Bean or not to Bean!