Course contentsShow
← Computer Science
Lesson 2337 of 2,87252. Clean Code and RefactoringFree lesson

Shotgun Surgery Smell

Recognize when a single change requires modifications across many different classes, indicating poor cohesion.

Shotgun Surgery Smell

What you'll learn: You'll recognize when a single logical change forces you to hunt through many different classes, signaling that related responsibilities are scattered instead of cohesive.

The Core Problem

Imagine needing to change your home address. If you had to update it in 15 different places—your driver's license, bank accounts, insurance policies, medical records, school records, etc.—that's tedious and error-prone. Shotgun Surgery is exactly this problem in code.

When you need to make one conceptual change (like adding a new field to a customer record or changing how dates are formatted), but you find yourself modifying 10+ classes scattered across your codebase, you've encountered this smell. It's the opposite of the God Object smell—instead of everything crammed into one place, related logic is fragmented everywhere.

Why It's Problematic

This smell indicates poor cohesion. Related responsibilities that should live together are spread thin across the system. The consequences:

  • High risk of missing spots: You might forget to update one of the 12 classes
  • Maintenance nightmare: Every feature change becomes a scavenger hunt
  • Coordination overhead: Multiple developers touching many files for one feature
  • Brittle system: Easy to introduce inconsistencies

Example Scenario

Suppose you need to change how phone numbers are validated. You discover you must edit: UserService, CustomerValidator, OrderProcessor, ShippingCalculator, NotificationManager, BillingSystem, and RegistrationForm. Each has its own phone validation snippet.

The Remedy

Use Move Method and Extract Class refactorings to consolidate scattered logic. Create a PhoneNumber class with centralized validation. Now one change updates one place.

Key Takeaway: Shotgun Surgery smell means a single change ripples across many classes—consolidate related behavior to improve cohesion and reduce maintenance burden.