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?

Robin
  • 165

1 Answers1

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.

Scant Roger
  • 9,048