EMZETT.
Login

SRP

In short: The single responsibility principle (SRP) states: a class should have only one reason to change.

In more detail: It is the “S” in the SOLID principles. Each class has exactly one responsibility.

In Depth

Example

An Invoice class that calculates, saves as PDF and sends by e-mail has three reasons for changes. Better: Invoice (data and calculation), InvoicePdfExporter, InvoiceMailer.

Benefit

Smaller classes, easier to test and reuse, fewer side effects when changing.

See OCP, spaghetti code.

See also: SOLID-Prinzipien, OCP, LSP, Spaghetti Code