How do you identify whether a class violates the Single Responsibility Principle?
Try to describe the class's job in a single sentence without using the word "and" — if you genuinely can't, each "and" usually marks a separate responsibility that should be pulled into its own class. A more structural signal is asking what would force this class to change: if you can list multiple, unrelated triggers — a new database, a new email provider, a new tax rule — and none of them depend on each other, the class is bundling independent axes of change. Watch also for classes that mix a noun (like Invoice, representing data) with verbs that belong to unrelated concerns (like saveToDatabase or sendEmail) — that mixing is one of the most common real-world SRP violations. The fix is not to count methods, since a class can have many methods and still have one responsibility, or have very few methods and still violate SRP if those few methods serve unrelated purposes.
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response