8

My project is slowly implementing Java annotations. Half of the developers - myself included - find that doing anything complex with annotations seems to add to our overall maintenance burden. The other half of the team thinks they're the bee's knees.

What's your real-world experience with teams of developers being able to maintain annotated code?

Dean J
  • 39,360
  • 16
  • 67
  • 93
  • 1
    how are you using the annotations? i don't think there is one answer here, i think it really depends on how you are using them. – james Oct 08 '09 at 16:46
  • Excellent question, as that probably matters quite a bit in this case. The pro-annotations devs have created a framework for our web application. The app uses Spring. The annotations automatically build web forms based on items in the Command objects, and avoid using JSPs entirely. Didn't mention it originally because I was curious for the general answer; I'm just as curious about the specific answer, as well. – Dean J Oct 08 '09 at 17:04
  • Bumping up a later comment:
    We used to have custom tags do all of the repetitive/error prone work for us in a JSP, so that it was very lightweight to begin with. Developers were familiar with it. It's not hard to follow, either from 10,000 feet or under the sheets, so when there was a problem, debugging was a snap.
    The new system is difficult to add onto and/or change. We also have a set of developers that are going to have trouble learning to use it. When there's a problem with the display, debugging through this involves reading code that most devs aren't ever familiar with.
    – Dean J Oct 08 '09 at 18:14
  • i replied below before i realized you copied the comment here. – james Oct 08 '09 at 18:36
  • 2
    To my mind, annotations should be used to hide boilerplate and simplify configuration. Used this way, I've seen some excellent home-brewed applications of them. But they should not be used for on-the-fly code generation of something you'll need to eyeball later or as a substitute for macros. For this specific use, annotations sound like a bad idea. – rtperson Oct 09 '09 at 14:39
  • how is "hiding boilerplate" and "on-the-fly code" two different things? if the annotations are hiding boilerplate code, then _something_ is generating that code on-the-fly. – james Oct 09 '09 at 16:48
  • @james - true, but most of the time with JPA and Spring, the code that's generated is all stuff I'm not going to want to eyeball later. That, to my mind, is the distinction. And I know from experience that I'm *always* going to want to eyeball my JSPs at some point. You can boilerplate some presentation-layer code, but not all of it. – rtperson Oct 12 '09 at 12:33

5 Answers5

6

My personal experience is that, on average, dealing with annotations is far easier for most developers than dealing with your standard Java XML Configuration hell. For things like JPA and Spring testing they are absolute life-savers.

The good thing about annotations is that they make configuration on your classes self-documenting. Now, instead of having to search through a huge XML file to try and figure out how a framework is using your class, your class tells you.

Usually the issue with changes like this is that getting used to them simply takes time. Most people, including developers, resist change. I remember when I started working with Spring. For the first few weeks I wondered why anyone would put up with the headaches associated with it. Then, a few weeks later, I wondered how I'd ever lived without it.

rtperson
  • 11,632
  • 4
  • 29
  • 36
4

I feel it breaks into two uses of annotations - annotations to provide a 'description' of a class vs. annotations to provide a 'dependency' of the class.

I'm fine with a 'description' use of annotations on the class - that's something that belongs on the class and the annotation helps to make a shorthand version of that - JPA annotations fall under this.

However, I don't really like the 'dependency' annotations - if you're putting the dependency directly on the class - even if it's determined at runtime from an annotation rather than at compile time in the class - isn't that breaking dependency injection? (perhaps in spirit rather than in rule...)

It may be personal preference, but I like the one big XML file that contains all the dependency information of my application - I view this as 'application configuration' rather than 'class configuration'. I'd rather search through the one known location than searching through all the classes in the app.

Nate
  • 16,748
  • 5
  • 45
  • 59
1

It depends highly on IDE support. I feel that annotations should be kept in sync with the code via checks in the IDE, but that support for this is somewhat lacking.

E.g. the older version of IDEA would warn if you overrode a function without @Override, but wouldn't remove the @Override tag if you changed the method signature (or the superclass signature, for that matter) and broke the relation.

Without support I find them a cumbersome way to add metadata to code.

Alex Feinman
  • 5,393
  • 1
  • 30
  • 48
0

I absolutely love annotations. I use them from Hibernate/JPA, Seam, JAXB....anything that I can. IMO there's nothing worse than having to open up an XML file just to find out how a class is handled.

To my eye annotations allow a class to speak for itself. Also annotations are (hopefully) part of your IDEs content assist, whereas with XML config you are usually on your own.

However, it may come down to how the XML configs and Annotations are actually used by any particular library (as most offer both), and what sort of annotation is used. I can imagine that annotations that define something that is build-specific (eg. file/url paths) may actually be easier as XML config.

Damo
  • 11,410
  • 5
  • 57
  • 74
0

i personally feel that the the specific use case you mentioned (auto-generate web forms) is a great use case for annotations. any sort of "framework" scenario where you can write simplified code and let the framework do the heavy (often repetitive) lifting based on a few suggestions (aka annotations) is, i think, the ideal use case for annotations.

i'm curious why you don't like annotations in this situation, and what you consider to be the "maintenance burden"? (and, i'm not trying to insult your position, just understand it).

james
  • 1,379
  • 7
  • 6
  • Not insulted at all. We used to have custom tags do all of the repetitive/error prone work for us in a JSP, so that it was very lightweight to begin with. Developers were familiar with it. It's not hard to follow, either from 10,000 feet or under the sheets. The JSP was *very* easy to follow, so if there was a problem, debugging was a snap. The new system is difficult to add onto and/or change. We also have a set of developers that are going to have trouble learning to use it. When there's a problem with the display, debugging through this involves reading code that most devs don't know – Dean J Oct 08 '09 at 18:12
  • so, it sounds to me that your problem is not with the annotations, but with the framework which is consuming the annotations. if so, then the problem would be the same whether you were using annotations, config files, or whatever. – james Oct 08 '09 at 18:34