CtrlK
BlogDocsLog inGet started
Tessl Logo

pantheon-ai/design-principles

Strategic architecture, tactical design, and testable code principles (SOLID, Clean Architecture, Design Patterns, Testable Design)

97

Quality

97%

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Passed

No known issues

Overview
Quality
Evals
Security
Files

task.mddesign-patterns/evals/scenario-1/

Task: Apply Strategy Pattern to Eliminate an If/Else Chain

The following ShippingCalculator uses an if/else chain to select between shipping strategies. Refactor it using the Strategy pattern.

Code to Refactor

// src/ShippingCalculator.ts
export class ShippingCalculator {
  calculate(method: string, weightKg: number, distanceKm: number): number {
    if (method === 'standard') {
      return weightKg * 0.5 + distanceKm * 0.1
    } else if (method === 'express') {
      return weightKg * 1.2 + distanceKm * 0.3 + 5.0
    } else if (method === 'overnight') {
      return weightKg * 2.0 + distanceKm * 0.5 + 15.0
    } else if (method === 'economy') {
      return weightKg * 0.3 + distanceKm * 0.05
    } else {
      throw new Error(`Unknown shipping method: ${method}`)
    }
  }
}

Output Specification

Produce:

  1. IShippingStrategy.ts — the strategy interface with a calculate(weightKg: number, distanceKm: number): number method.
  2. One implementation file per strategy: StandardShipping.ts, ExpressShipping.ts, OvernightShipping.ts, EconomyShipping.ts.
  3. ShippingCalculator.ts — refactored to accept an IShippingStrategy and delegate to it. Must contain no arithmetic.
  4. pattern-analysis.md — describe the problem (two sentences), the win condition (one sentence), and the cost (one sentence).

All TypeScript files must be valid.

tile.json