Code quality and technical debt

Liskov substitution principle

Also known as Liskov substitution principle, Liskov substitution principle in software engineering

By WeavePublished 1 min read

Definition

A design principle that requires a subtype to remain valid wherever its declared base type is expected.

What it means

The Liskov substitution principle says that a subtype should honor the behavioral expectations of the abstraction it replaces. It is not enough for method signatures to match. Preconditions, postconditions, error behavior, and meaningful invariants must remain compatible so callers do not need subtype-specific exceptions.

Example

A read-only storage implementation throws on a method that the storage contract promises will succeed. Callers written for the contract fail when the subtype is substituted, revealing a broken behavioral boundary.

Limitations

Formal substitution can be difficult to verify, especially with side effects and undocumented assumptions. Inheritance may be the wrong mechanism when implementations do not share a true behavioral contract.

How to use the signal

Use behavioral contract tests where possible, since matching signatures do not guarantee substitutable outcomes.

Weave can help trace subtype changes to review findings and rework. Keep contract tests and domain reasoning in the codebase, while Weave shows whether the boundary causes delivery friction.

How this relates to Weave

Weave can help trace subtype changes to review findings and rework. Keep contract tests and domain reasoning in the codebase, while Weave shows whether the boundary causes delivery friction.

Explore Engineering intelligence

Sources and further reading

  1. SonarSource code metrics and analysis documentation