2

After spending a great deal of time writing C# and looking at Java, it seems to me that annotations are just an ugly code smell that introduce another conceptual layer that could easily be replaced by existing language features, whether they be keywords (as in the case of C#'s override keyword versus Java's @override annotation) or inheritance (why not just have a ISerializable interface or base class instead of a [Serializable] attribute?) However, this conclusion could just as easily be due my relative inexperience as a professional programmer.

My question is compound:

  1. What do annotations bring to the table that cannot be accomplished in other ways, without complicating the grammar of a language?
  2. Are there things that can be implemented without annotations, but which would be much more difficult to implement without them?
  3. More generally, where (if anywhere) is the demarcation line between data and metadata in a programming language?

My question is different from What problems are Java annotations well suited to?. For starters, this question is not Java-specific. I'm wanting to know, in general, whether annotations are indispensable or merely useful. I am also curious as to the theoretical distinction between data and metadata, and how useful that distinction really is.

2 Answers2

6

Annotations can be user specified and accessible by reflection.

This means you can create a framework to look into a user's classes and pull out the needed information.

The annotations are also close to where they have effect. It's possible to extract all annotations out to a config file and have the framework parse that but that can lead to typo related bugs and forgetting to update the configs.

For example a testing framework would iterate over all functions of an object and use the annotations to check whether a test should throw an exception and if so which one. without this the test would need to catch the exception and do a FailTest() call after the method that should throw. Even marking which methods are tests and which are helpers, setup or tear-down would be annoying.

A DI framework could look at a constructed object and fill the annotated field with the correct implementation without needing to add yet another parameter to the constructor.

They make life easier on the user's side of the API at the cost of often being a pain to program and debug.

ratchet freak
  • 25,986
2
  1. There is no such thing, after all, before annotations were available, we have used XML files for everything.

  2. It is just nicer to have everything related to class or its field in one place/file. I guess method level annotations are hard to replace since the alternatives require providing class name, method name and sometimes list of parameters, if method is overloaded.

  3. There isn't any theoretical distinction beyond the basic "metadata is data about data". We might as well say that access modifiers (public/private) are metadata and replace them with annotations (Project Lombok does something related)