Analyze code to suggest and add Design by Contract specifications (preconditions, postconditions, invariants, semantic invariants) in any language. Trigger on: "Design by Contract", "DBC", "preconditions", "postconditions", "class invariants", "code contracts", "formal specifications", "document assumptions", "add contracts", "make this function safer", "define what this function guarantees", "add assertions to document behavior", Bertrand Meyer, Eiffel contracts, or when discussing invariants that should always hold. Do NOT skip for non-Eiffel code — DBC applies everywhere via assertions, type guards, properties.
92
90%
Does it follow best practices?
Impact
95%
1.05xAverage score across 5 eval scenarios
Passed
No known issues
Analyze, document, and implement DBC (Design by Contract) in any language. Core rule: if the caller satisfies all preconditions, the routine must guarantee all postconditions and restore invariants. A violation is a bug.
Read the relevant function(s), class, or module. Identify the inputs, outputs, and state mutations.
For each function, identify:
For the system or subsystem: Semantic invariants — what fundamental business rule must never break?
Express contracts in a way that matches the language and project context. Use language mechanisms for language-specific enforcement options.
Not every function needs an elaborate contract. Prioritize: public APIs, complex input constraints, hidden assumptions, correctness-critical code, and boundary code between your system and external libraries.
When adding contracts to existing code:
After adding contracts:
When the user asks you to analyze code, structure your output like this:
## Contract Analysis: `functionName`
### Preconditions
- ✅ / ❌ / ⚠ (status) — description
*Suggestion*: how to document/enforce this
### Postconditions
- ✅ / ❌ / ⚠ (status) — description
*Suggestion*: how to document/enforce this
### Invariants
- ✅ / ❌ / ⚠ (status) — description
*Suggestion*: how to document/enforce this
### Semantic Invariants (system-level)
- description of any overarching business rules
### Summary
Count of existing vs. missing contracts. Key recommendations.Use:
"Here's a function that calculates the discount for a customer order. Can you add contracts?"
def apply_discount(order_total, customer_tier, items):
if customer_tier == "gold":
discount = 0.2
elif customer_tier == "silver":
discount = 0.1
else:
discount = 0.0
return order_total * (1 - discount)## Contract Analysis: `apply_discount`
### Preconditions
- ❌ `order_total` must be positive (currently unenforced)
*Suggestion*: `assert order_total > 0, "order_total must be positive"`
- ❌ `customer_tier` must be one of "gold", "silver", "standard" (currently unenforced)
*Suggestion*: `assert customer_tier in ("gold", "silver", "standard"), f"Unknown tier: {customer_tier}"`
- ❌ `items` must be a non-empty list of items (currently unenforced)
*Suggestion*: `assert items, "items list must not be empty"`
### Postconditions
- ❌ Discount must be between 0 and order_total (result should be >= 0)
*Suggestion*: `assert 0 <= result <= order_total, f"Result {result} out of range"`
- ⚠ `items` list should not be mutated (side effect free) — currently OK but should be stated
### Invariants
No class invariants — this is a standalone function.
### Summary
- **0** contracts documented
- **4** contract suggestions (3 preconditions + 1 postcondition)
- Key recommendation: start by asserting the precondition on `customer_tier` — this catches a real bug vector