S, Single Responsibility: a class should have one reason to change. A class that validates, saves to the database and sends email does three jobs. Split it.
O, Open/Closed: add new behaviour by adding new code, not by editing tested code. For example, add a new IDiscount implementation instead of growing a long if/else.
L, Liskov Substitution: a subclass should work wherever its parent is expected, without surprises. If Square extends Rectangle but breaks width and height rules, it violates this.
I, Interface Segregation: prefer small, focused interfaces. Do not force a class to implement methods it does not use.
D, Dependency Inversion: depend on abstractions, not concrete classes. A service should take an IEmailSender, not create a SmtpEmailSender itself. That is what dependency injection is for.
These are guidelines, not laws. Use them to reduce pain, such as code that is hard to change or test. Over-applying them to tiny programs creates needless complexity.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
IDiscountimplementation instead of growing a long if/else.SquareextendsRectanglebut breaks width and height rules, it violates this.IEmailSender, not create aSmtpEmailSenderitself. That is what dependency injection is for.These are guidelines, not laws. Use them to reduce pain, such as code that is hard to change or test. Over-applying them to tiny programs creates needless complexity.