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')].
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.
Implement proven backend architecture patterns including Clean Architecture, Hexagonal Architecture, and Domain-Driven Design. Use this skill when designing clean architecture for a new microservice, when refactoring a monolith to use bounded contexts, when implementing hexagonal or onion architecture patterns, or when debugging dependency cycles between application layers.