How Java annotations might help to bring world peace

… well ok, that title might be a little too catchy. But here’s the story:

Using Java annotations to make programmers’ lifes a bit easier, is one of the language features that is, in my opinion, underestimated. A long time ago I’ve introduced a mighty @Workaround annotation in our code base in order to mark, guess it, workarounds! I simply felt bad to implement temporary workarounds that should be removed once an issue of a third party framework was resolved. I felt bad because I thought of the poor guy(s) (including myself) who will maintain the software in future. How should he or she know that some weird code snippets were introduced due to bugs or inadequateness of third party software? How should he or she know if the ugly code can be removed and when? Well, here is the solution:

@Retention(RetentionPolicy.CLASS)
@Documented @Target( value = { ElementType.CONSTRUCTOR,ElementType.FIELD,
   ElementType.METHOD,ElementType.TYPE,ElementType.PARAMETER } )
public @interface Workaround{
   String desc();
   String developer();
   String waitingIssue() default "";
   String issue() default "";
}

And the javadoc belonging to the annotation:

Signifies that a workaround was implemented due to bugs/inadequateness of third party software or to mark “code smells” and hacks. Makes workarounds explicit and code more maintainable. Reduces moments like “Why the heck is this code here?!”. Required attributes are ‘desc’ and ‘developer’. It provides the ‘issue’ attribute to link to internal bug tracking issue which describes the workaround in more detail if the ‘desc’ field ist not sufficient. The ‘waitingIssue’ attribute can be used to reference to an issue in bug tracking system of the third party framework. Usually the Workaround can be removed if the waitingIssue is resolved.

Here it is in action:

The annotation turned out as incredible useful and is used heavily. The cool thing is, that once in a while you can simply search for references on the annotation type and you get all the workarounds to check if they are still needed.

It will make your code base a bit cleaner in the mid-term and may save some peoples’  nerves. Probably too few nerves to actually bring world peace. But anyway, I hope this post will inspire you to think about how to make your and the life of your programmer fellows a bit easier using simple techniques. I would really appreciate to see some of your ideas right here in the comments.

 

About the author

Arthur Hupka

Arthur Hupka

I have graduated in Computer Science and I am working for Bosch Software Innovations since 2009 as a Java Developer and Technical Consultant with expertise in web applications and batch processing. The past two years I have been involved in several credit risk rating projects and spent months at different customer sites. Recently, I have joined the product development team. I’m interested in all aspects of software development like (component based) software design, architecture, code quality, tests, scrum, etc. In my spare time I’m also experimenting with my own project based on Apache Wicket, Brix CMS, and Google Guice. I enjoy doing sports like bike cycling, beach volleyball and snowboarding. Lake Constance is a great area for doing that btw! I also like reading and visiting festivals during summer. Sometimes I enjoy cooking but I hate housekeeping.