4

Talking generic, there are programming languages who make more or less use of annotations and decorators.

Be it to add type information to variables in a dynamically typed language or to add "metadata" to methods and classes in a framework context. The "purpose" of them is always to in some way extend the language in it's abilities or to make it more flexible in a certain way.

Now this all seems nice right? Using a dynamically typed language and want to add some type information to your variables? - Just use them decorators. Need to make that class to be managed remotely by an app-server? Tell it by adding an annotation.

Those things look like symptoms to me. Symptoms being caused by an underlying design problem. Or am I massively wrong about that?

5
  • 3
    Huh? Decorators are great because they allow you to add functionality to instances of a class in strictly typed languages. With type-as-you-go objects, that kind of thing is already possible to begin with. Commented Dec 21, 2015 at 13:38
  • 2
    You'll have to focus on a specific example of what you're describing to make this an answerable question. At the moment, the only correct answer would be the rather unhelpful "sometimes yes, sometimes no, depends on what decorator/annotation/language/project you're talking about".
    – Ixrec
    Commented Dec 21, 2015 at 13:42
  • I think you should separate decorators and annotations. The two are completely different concepts used for completely different purposes.
    – Euphoric
    Commented Dec 21, 2015 at 14:07
  • 1
    @Euphoric: I disagree. They are similar. The main difference is that in Java, annotations and their processors are distinct entities, which means that the same annotation can be processed by multiple processors (e.g. the @Override annotation can be processed by both the Java compiler and a static analyzer) whereas in Python, decorators are their own processors. Commented Dec 21, 2015 at 16:07

1 Answer 1

6

You're massively wrong about that. ;)

A decorator in Python is more than just information. It's runnable code. That means you can apply concerns in a declarative manner. A declarative style keeps your code tidy -- there's no need to torture your plumbing/design to express relationships and intent. Decorators are absolutely brilliant at applying middleware.

In static type systems, annotations are information, but it's incredibly valuable information. If I can identify, though annotations, characteristics of a code unit, I can then generically assign/modify behaviors based on those characteristics. Again, this allows nice clean declarative modifications versus hacking up my software model to express the same intent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.