4. Liskov Substitution Principle (LSP) Flashcards

1
Q

How does LSP-compliant code relate to the OCP?

A

Liskov Substitution Principle tends to produce code that better follows the Open/Closed Principle.

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

What does the LSP state?

A

Subtypes must be substitutable for their base types.

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

What does basic object-oriented design teach to think about objects and their relationships with one another?

A

Using two characteristics — inheritance (IS A) and properties (HAS A).

Inheritance: An eagle IS A bird.
Property: An address HAS A city.

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

How does LSP differ from the teaching of basic object-oriented design?

A

LSP states that the IS A (inheritance) relationship is insufficient and should be replaced with IS SUBSTITUTABLE FOR.

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

How does the relationship between a rectangle and a square relate to LSP?

A

Square has an invariant (its sides must be equal) that clashes with the rectangle’s invariant (its sides are independent). Therefore, a square is not substitutable for a rectangle because the code that works with a rectangle might expect to set different lengths for the width and the height of the rectangle.

This design violates LSP.

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

What are the three common symptoms that LSP might be being violated in real-world code?

A
  1. Type checking with is or as in polymorphic code, since polymorphism implies substitution.
  2. Null checks — often looks quite similar to type checks.
  3. NotImplementedException — pretty much every time you see this exception, it’s an indication that an interface or base class was only partially supported, and some of its features were left unimplemented. By definition, this type cannot be substitutable for its interface or base class for all places it might be called.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What can be said about nulls in regards to the LSP?

A

That they’re not substitutable for actual instances and therefore violate the Liskov Substitution Principle.

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

What is the relationship between LSP and polymorphism?

A

Liskov Substitution principle is a subset of polymorphism — all types that follow LSP also support polymorphism. More importantly, anywhere that you want to use polymorphism, you almost certainly want substitutability (LSP).

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

What is the “Tell, Don’t Ask” principle?

A

DON’T ASK instances for their type and then conditionally perform different actions, but instead, encapsulate that logic in the type itself and TELL it to perform an action.

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

How can we minimize null checks in C#?

A
  1. C# features — nullable reference types, null conditional operators, null coalescing operators.
  2. Guard clauses — prevent null values from reaching the primary logic in your methods.
  3. Null Object design pattern — provides a useful way to prevent null checks and provide an LSP-compliant instance to use when you might otherwise have used a null.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How can we fix LSP violations?

A
  1. Follow the “Tell, Don’t Ask” principle.
  2. Minimize null checks
  3. Whenever you implement an interface or inherit from a base class, make sure you fully implement it (Interface Segregation Principle).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the key takeaways of this module?

A
  1. Subtypes must be substitutable for their base types.
  2. Ensure base type invariants are enforced.
  3. Look for type checking, null checking, and NotImplementedException
How well did you know this?
1
Not at all
2
3
4
5
Perfectly