Introduction

Building on our discussion of AI.TDD as a new paradigm, let's explore the different roles AI can play in the TDD process. Sometimes AI should lead (pilot), and sometimes it should assist (copilot).

AI as Pilot: Leading the Development


// AI suggests the test structure
describe('ShoppingCart', () => {
  test('should calculate total with items', () => {
    const cart = new ShoppingCart();
    cart.addItem({ price: 10, quantity: 2 });
    expect(cart.getTotal()).toBe(20);
  });
});

// AI implements the solution
class ShoppingCart {
  constructor() {
    this.items = [];
  }

  addItem(item) {
    this.items.push(item);
  }

  getTotal() {
    return this.items.reduce((total, item) => 
      total + (item.price * item.quantity), 0);
  }
}
            

AI as Copilot: Assisting the Developer


// Developer writes the test
test('should apply discount to total', () => {
  const cart = new ShoppingCart();
  cart.addItem({ price: 100, quantity: 1 });
  cart.applyDiscount(0.1); // 10% discount
  expect(cart.getTotal()).toBe(90);
});

// AI suggests implementation
class ShoppingCart {
  // ... existing code ...

  applyDiscount(percentage) {
    const discount = this.getTotal() * percentage;
    this.discount = discount;
  }

  getTotal() {
    const subtotal = this.items.reduce((total, item) => 
      total + (item.price * item.quantity), 0);
    return subtotal - (this.discount || 0);
  }
}
            

When to Use Each Role

  • AI as Pilot:
    • Routine implementations
    • Well-defined patterns
    • Boilerplate code
  • AI as Copilot:
    • Complex business logic
    • Architecture decisions
    • Performance optimizations

Best Practices

  • Start with AI as copilot
  • Gradually increase AI's role
  • Maintain human oversight
  • Review AI's decisions

Conclusion

The key to successful AI.TDD is knowing when to let AI lead and when to keep it as an assistant. It's about finding the right balance for each situation.