Course contentsShow
← Software Engineering
Lesson 200 of 1,9336. SOLID PrinciplesFree lesson

Common SRP Violations and Fixes

Study typical violations like God classes, mixed concerns, and learn systematic approaches to resolve them.

Common SRP Violations and Fixes

What you'll learn: You'll recognize typical ways code violates the Single Responsibility Principle and learn systematic approaches to fix them.

Now that you understand SRP, let's identify the most common violations you'll encounter in real code—and how to fix them.

The God Class

A "God class" does everything: business logic, data validation, database access, formatting output, logging, error handling, and more. It's called "God" because it knows and controls too much.

Fix: Extract responsibilities into separate classes. Create a validator class, a repository for database access, a formatter for output, and let the original class focus on coordinating these collaborators or handling just one core concern.

Mixed Concerns

A class handles both "what to do" (business logic) and "how to do it" (technical details). For example, a ReportGenerator that both calculates report data AND formats it as PDF.

Fix: Split into ReportCalculator (business logic) and PdfFormatter (technical formatting). Now each changes for one reason: calculation rules vs. PDF rendering requirements.

Multiple Unrelated Behaviors

A UserManager class that handles authentication, email sending, profile updates, and password encryption—four unrelated responsibilities.

Fix: Extract Authenticator, EmailService, ProfileService, and PasswordEncryptor. Each now has one clear job.

Systematic Fix Approach

  1. List everything the class does
  2. Group related behaviors
  3. Identify distinct "reasons to change"
  4. Extract groups into new classes
  5. Keep names focused and specific

Key Takeaway: God classes, mixed concerns, and unrelated behaviors are the most common SRP violations—fix them by extracting focused classes where each has one clear reason to change.