Skip to content

Decorator Definition #7

Description

@harell

Decorators have the following qualities:

  • Syntactic Compatibility. The decorated function can replace undecorated functions without modification of the codebase. This leads to two requirements:
    • The decorator doesn't add new input arguments, including new input args with default values.
    • The decorator doesn't change the returned data structure in case of successful execution of the undecorated function.

Decorators may alter the behaviour of a function in one or more ways:

  • Strengthening function preconditions. E.g. validate_arguments() throws new expections.
  • Weakening function postconditions. E.g. purrr::saftly + purrr::compose + Null object
  • Report additional information for function execution. E.g. print info in console or open intermediate results with View.
  • Modification of return values (not structure). E.g. rounding numeric values.

Decorators violate several SOLID principles:

  • Single-responsibility principle (see excerpt)
  • Liskov substitution principle (because they can strengthen input conditions)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions