Monday, July 25, 2016

A recent provocatively titled blog post bashing Java annotations kicked up an amusing amount of controversy. What I find notable about the post and its responses is that there doesn’t seem to be much agreement on annotations’ place in Java. My own take on this is that many popular usages of annotations are questionable, but we’re stuck with them.



Annotations started off innocently enough. They are simply a way to attach metadata to source code. I don’t think the folks who originally introduced annotations would have any idea that annotations would be so heavily (mis)used throughout the ecosystem. The Java Language Specification states that "An annotation is a marker which associates information with a program construct, but has no effect at run time." (9.7 Annotations)

No effect at run time, huh? But run time behavior seems to be the dominant use of Java annotations. A lot of Java code today are useless without the containers and frameworks that read their annotations and alter their behavior based on those annotations. Moreover, there are annotations that actually modify the class signature and bytecode, such as those from Project Lombok. If you use Project Lombok constructs such as @Getter, you end up with code that won’t even compile under vanilla Java. Talk about giving your code a Lombok-tomy.

Still, I think anti-annotations rants are ultimately futile. The reality is that there really is no alternative. We’ve tried XML, configuration files, various DSLs, vast class hierarchies, and nothing works better. I see that Gavin King, the inventor of Hibernate, is among those responding negatively to that post. And no wonder: pre-annotations Hibernate was an ugly mess of XML files. Nobody wants to go back there. Spring was originally XML-based but annotations seem to have won out. I know some people claim that convention over configuration (CoC) would work, but I think it’s no panacea. CoC carries a heavy cognitive load: you need to carry around that unwritten convention in your head all the time. There’s a practical complexity quota beyond which CoC frameworks simply run out of room.

Java has emphasized readability as it evolved over the years. That is, everything is explicit, even if it has to be written twice (where’s my type inferencing?). In his Computer article The Feel of Java, James Gosling describes Java as a “blue collar language”, “deterministic”, and “You feel like it’s going to do what you ask it to do”. I guess embedding annotations in code, rather than convention or fancy meta programming, is more in-your-face and more Java-like. And unless someone can demonstrate viable alternatives to, say, JPA persistence annotations, these annotations are not going away.

Related Links


Evil Annotations

4 comments:

  1. That was a great message in my carrier, and It's wonderful commands like mind relaxes with understand words of knowledge by information's.
    Security Services in Chennai

    ReplyDelete
  2. Great post!I am actually getting ready to across this information,i am very happy to this commands.Also great blog here with all of the valuable information you have.Well done,its a great knowledge.

    Digital Marketing Company in Chennai

    ReplyDelete
  3. I am not sure the place you are getting your information, however good topic.I needs to spend some time studying more or understanding more.Thank you for wonderful information I was in search of this info for my mission.

    HR Consultancy in Chennai
    Recruitment Consultancy in Chennai
    Manpower Consultancy in Chennai

    ReplyDelete
  4. I have definitely picked up anything new from right here. I did however expertise a few technical points using this site, since I experienced to reload the web site a lot of times previous to I could get it to load correctly.
    Best Interior Designers in Chennai
    Interior Designers in Chennai

    ReplyDelete