To annotate or not: that is the question

It seems there is a diversity of opinions regarding Java 5 annotations. So I decided to write a little bit on my (current) thoughts on this. Maybe you want to express your ideas too. I would appreciate that. That would help me make my mind on this.

Annotations, by accident

The fact is that I happened to read my favourite Scheme book lately and I found this by accident:

"The contrast between function and procedure is a reflection of the general distinction between describing properties of things and describing how to do things, or, as it is sometimes referred to, the distinction between declarative knowledge and imperative knowledge."
Structure and Interpretation of Computer Programs 2nd. Edition, page 22. (Nice read)

And I thought this could say it all about annotations. I mean: since annotations contain declarative knowledge of things they should be used to describe the properties of things (but not how things work).

Let's rewrite this. What does "things" mean? What can I annotate in Java? Well, it seems we can annotate annotations themselves, packages, classes (interfaces), constructors, methods, method parameters, class fields and local variables.

That's a lot! ;-)

So, rewriting the paragraph above, we end up with:

Annotations usage idea I: describing properties

Annotations should be used to describe the properties of annotations, packages, classes (interfaces), constructors, method parameters, class fields and local variables.

So that's what I understood about annotations, too. And this is a Good Thing to have in Java. JAXB2 annotations and EJB 3.0 annotations conform to this rule: they contain a description of the properties of things (properties of entity beans, for instance).

Annotations and that little footnote down there

So I kept on reading my favourite book and happened to read that little footnote down there. I mean this one. Let me quote a little bit:

In a related vein, an important current area in programming-language design is the exploration of so-called very high-level languages, in which one actually programs in terms of declarative statements. The idea is to make interpreters sophisticated enough so that, given ``what is'' knowledge specified by the programmer, they can generate ``how to'' knowledge automatically. This cannot be done in general, but there are important areas where progress has been made.

Structure and intepretation of Computer Programs 2nd. Edition, footnote # 20 on page 22

Oh, ah! This is very interesting! It seems you can use declarative programming to specify how things work too!!. So you can use annotations to "generate "how to" knowledge automatically". (and this is where the apt annotation tool fits in).

Well, this is nothing new if you think of it. Take, for instance, the Spring framework. In Spring (I'm not a Spring expert so I may be wrong here) you build things declaratively. You end up programming things with XML. Indicating how things work. Which beans are "SimpleUrlHandlerMapping" and which beans are "org.springframework.jms.support.converter".

So annotations (or Spring declarations, or XDoclet) can be used to generate "how to" knowledge automatically. This is very important for those of us responsible for defining frameworks. By using annotations the "how to" is generated automatically. You can include best practices and best of breed, high quality, field-tested patterns automatically. This reduces development time, reduces the probability to do things badly and thus augments the probability of success in your software.

So we end up with another idea:

Annotations usage idea II: automatic code generation

Annotations can be used to generate code automatically, so as to automatically include (bugs? ;-)) best practices in your software for you.

But this automatic code generation is a double-edge swiss knife. What happens if the automatically generated code is a bad one? Will the automatically generated code correctly address your non functional requirements? Is it scalable? Is it performant? Can it be distributed in a cluster of application servers? How easily will it be maintained within two years from now?

This is, if you automatically generate code with annotations, is it being correctly generated??...

... To annotate or not to annotate: that (whether the generated code is good or not) is the question!!

Have a good weekend,
Antonio

Comentarios:

Scheme... mmmm, lisp... Hrm, now really wanting to work on trying this stuff out!!! Darn work and darn my stupidity on forgetting to make a JNI security wrapper multithreaded preventing me from trying this stuff out!!! \*bangs head repeatedly\*

Think I might have to buy that book now too! Darn you! ;)

Enviado por Jeffrey Olson en octubre 28, 2005 a las 05:17 PM CEST #

"This is very interesting! It seems you can use declarative programming to specify how things work too!!." incorrect. The original author said nothing like it. You still only specifiy "what" using annotations, but the generator has extensive knowledge of how based on the "what". It is quite different from specifiying "how", like embed SQL statement in annotation.

Enviado por andrew en noviembre 01, 2005 a las 03:29 PM CET #

Enviar un comentario:
Los comentarios han sido deshabilitados.
About

swinger

Search

Archives
« abril 2014
lunmarmiéjueviesábdom
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    
       
Hoy