3. Open/Closed Principle (OCP) Flashcards

1
Q

What does the OCP state?

A

That software entities (classes, modules, functions, etc.) should be open for extension but closed for modification.

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

What does the OCP mean in relation to methods?

A

It should be possible to change the behaviour of a method without editing its source code.

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

What does “open to extension” mean?

A

That new behaviour can be added in the future.

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

What does it mean if a module in our code is “closed to extension”?

A

That the module has a fixed behaviour.

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

What does “closed to modification” mean?

A

That it’s unnecessary to change the source or binary code of the system.

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

What can be said about the closedness to modification of a class that is closed to extension?

A

The only way to change the behaviour of code that is closed to extension is to change the code itself. So, by definition, it is not closed to modification.

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

Why should code be closed to modification?

A
  1. The less we need to change the source of our code, the less likely it is that we’ll introduce new bugs to it.
  2. Less likely to break dependent code because we don’t have to deploy updates on the given module.
  3. Fewer conditionals in code that is open to extension results in simpler code.
  4. Bug fixes are OK.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the considerations around using abstractions in code?

A
  1. Balance abstraction with concreteness.
  2. Abstraction adds complexity.
  3. Predict where variation is needed and apply abstraction as needed.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you decide where to make your application extensible while balancing extreme concreteness and extreme abstraction?

A
  1. Start concrete
  2. When changes are needed, modify the code the first time or two
  3. By the third modification or so, consider making the code open to extension for that axis of change
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the three approaches to implementing OCP?

A
  1. Parameters
  2. Inheritance
  3. Composition/Injection
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Should you use new classes for new features, and why?

A

Yes!

  1. Have the chance to design the class to perfectly suit the problem at hand. No need to work around existing code or bad patterns that might be there.
  2. Nothing in the current system will depend on the newly created class because until now it didn’t exist!
  3. Can add behaviour without touching existing code.
  4. The newly created class can immediately follow SRP if the class that’s using it has a lot of responsibilities.
  5. Can be unit tested since you’re designing from scratch.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How does OCP relate to NuGet packages?

A

OCP is especially important in NuGet packages. Whether public or internal, NuGet packages are closed for modification by their consumers and new versions of the package must not break the existing code.

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

What are the key takeaways of this module?

A
  1. Solve the problem first using simple, concrete code
  2. Identify the kinds of changes the application is likely to continue needing
  3. Modify code to be extensible along the axis of change you’ve identified
How well did you know this?
1
Not at all
2
3
4
5
Perfectly