SOLID principles Flashcards

(9 cards)

1
Q

S — Single Responsibility Principle

A
  • A class should have only one reason to change.
  • Analogy: hire accountant, cleaner, developer instead of one person who does all of that
  • Prevents: “Everything breaks when one thing changes.”
  • A class should only be responsible for validation, not validation, data access, and email sending
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
1
Q

What Is SOLID

A

5 design principles that make code easier to change, test, extend

  • S - Single Responsibility Principle (SRP)
  • O - Open/Closed Principle (OCP)
  • L - Liskov Principle (LSP)
  • I - Interface Segregation Principle (ISP)
  • D - Dependency Inversion Princinple (DIP)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

O — Open/Closed Principle (OCP)

A
  • Code should be open for extension, closed for modification
  • Analogy: don’t open phone to add features, instead use cases, attachments, snap-on lenses etc
  • Prevents: “We keep modifying the same fragile file.”
  • DiscountCalculator -> Calculate()
  • vs.
  • IDiscountStrategy -> RegularDiscount : IDiscountStrategy -> GetDiscount()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

L — Liskov Substitution Principle

A
  • Subtypes must be substitutable for their base types
  • Analogy: if renting a car, instead I get a scooter
  • Meaning: even it’s a vehicle, it doesn’t behave correctly
  • Prevents: “Swapping implementations crashes production.”
  • Example: swapping any PaymentProcessor should work for any IPaymentProcessor implementation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

I — Interface Segregation Principle (ISP)

A
  • Don’t force classes to implement methods they don’t use
  • If you see NotImplementedException, something’s wrong.
  • Analogy: restaurant where every customer has to order steak, sushi, dessert, wine even if they’re vegetarian
  • Prevents: “Why does this class implement 12 methods it doesn’t need?”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

D — Dependency Inversion Principle

A
  • Depend on abstractions, not concrete implementations
  • If you see this inside business logic: “new SomeConcreteClass()” -> you’re probably breaking DIP.
  • Analogy: an appliance that connects directly to power plants is wrong. An appliance that connects to a wall outlet is correct. Allows the power plant to be able to change
  • Prevents: “We can’t test this without hitting the database.”
  • Example: call IPaymentProcessor instead of PayPalProcessor
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Abstract vs. Interface

A

Use Abstract when:
- you need base type inheritance
- Is a true “is-a” relationship
- CreditCardProcessor IS A PaymentProcessor
- PayPalProcessor IS A PaymentProcessor

Use Interface when:
- you need interchangeable implementations
- you need to define a contract

public class OrderService
{
private StripeProcessor _processor;
}

vs

public class OrderService
{
private readonly IPaymentProcessor _processor;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Why SOLID is important for coding?

A
  • More maintanable
  • Separation of concerns and loose coupling
  • Easier to add new features, change implementations, and test without affecting other parts of system
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How have you implemented SOLID at current position?

A
  • We intentionally don’t use direct SQL queries in the UI layer
  • Instead the UI layer calls a business logic layer
  • Business logic layer calls data logic layer
How well did you know this?
1
Not at all
2
3
4
5
Perfectly