DELAPARRA
Index
Color
NL
Category: Programming paradigms

Decorator / Annotation

Explained by Updated

What is Decorator / Annotation?

Decorator / Annotation: Syntax that attaches metadata or wraps a function/class with extra behaviour without changing its own source. A decorator or annotation is syntax that attaches extra information or behaviour to a function, class or property without changing its body. Python uses @decorator above a function, Java and C# use annotations or attributes, and PHP 8 has attributes like #[Route('/orders')].

Definition source Coders Lingo, CC BY 4.0

Frequently asked questions about Decorator / Annotation

How is Decorator / Annotation used in practice?

Some only add metadata, which a framework reads later. A route attribute tells the router which URL belongs to a method. Others wrap the function, for example to cache its result or check permissions before it runs.

What should you keep in mind with Decorator / Annotation?

They keep cross-cutting concerns close to the code they affect, which is easy to read. The risk is hidden behaviour. A function with five decorators does much more than its body suggests. Keep them to well-known, documented ones, and test the combined behaviour, not just the function on its own.

At a glance

CategoryProgramming paradigms
Explained by
Updated

More Programming paradigms terms

All terms starting with D

Skills

Download all skills (zip)

Manuals

Related services

Need any help?

Stuck on one of these in your own project? Tell me what you are working on and I will tell you how I would approach it.