2. Single Responsibility Principle (SRP) Flashcards

1
Q

What are the SOLID principles comprised of?

A

Five individual principles for writing better software, especially in object-oriented languages.

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

What does the SRP state?

A

Each software module should have one and only one reason to change.

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

What is a module in the context of a C# program?

A

A module in C# might refer to a class or even a single function.

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

How can we improve the design of our software when it comes to the proximity between what the application does and how it does it, and how?

A

By separating the “what” from the “how”. We do this through delegation and encapsulation.

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

How can we improve the design of our software when it comes to the proximity between what the application does and how it does it, and how?

A

By separating the “what” from the “how”. We do this through delegation and encapsulation.

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

What should the classes encapsulate?

ps. Encapsulation in the general sense.

A

Doing a particular task in a particular way.

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

What can be said about single-purpose classes?

A

They do a single particular task in a particular way, and they are easy to use.

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

What can be said about the performance of multipurpose tools vs dedicated tools?

A

Multipurpose tools don’t perform as well as dedicated tools.

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

What can be said about the performance of multipurpose tools vs dedicated tools?

A

Multipurpose tools don’t perform as well as dedicated tools.

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

What is a responsibility in the context of the SRP?

A

It’s the answer to the question of how something is done.

It’s a decision our code is making about the specific implementation details of some part of what the application does.

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

What are some examples of responsibilities?

A
  1. Persistence
  2. Logging
  3. Validation
  4. Business Logic
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What do responsibilities in our code represent in regards to change and the SRP?

A

Responsibilities represent things that may change at different times and for different reasons — each responsibility is a different axis of change.

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

What happens when two or more details are intermixed in the same class?

A

It introduces tight coupling between these details.

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

What is tight coupling?

A

Binding two or more details together in a way that’s difficult to change.

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

What’s the difficulty that tight coupling brings?

A

If the tightly coupled details change at different times for different reasons, it’s likely to cause problems in the future with code churn in the class in question.

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

What’s the difficulty that tight coupling brings?

A

If the tightly coupled details change at different times for different reasons, it’s likely to cause problems in the future with code churn in the class in question.

17
Q

What does loose coupling offer?

A

A modular way to choose which details are involved in a particular operation.

18
Q

What is a typical example of loose coupling?

A

One class being responsible for some high-level concern and delegating to other classes who are responsible for the details of how to perform low-level operations.

19
Q

What is a typical example of loose coupling?

A

One class being responsible for some high-level concern and delegating to other classes who are responsible for the details of how to perform low-level operations.

20
Q

What does the seperation of concerns state?

A

That programs should be separated into distinct sections, each addressing a separate concern or set of information that affects the program.

21
Q

What can be thought frequently of as low-level plumbing code?

A

The specific implementation details of how a program does something.

22
Q

What is the key benefit of following the separation of concerns?

A

That high-level business logic avoids having to deal with low-level code.

23
Q

What should ideally be the case in regards to high-level and low-level code’s knowledge of one another?

A

High-level code should not know about how low-level implementation details are implemented, nor should it be tightly coupled to the specific details.

24
Q

What does cohesion describe?

A

How closely related the elements of a class or module are to one another.

25
Q

What makes a class less cohesive?

A

The fact that its methods don’t share much data or behaviour with one another.

26
Q

What makes a class less cohesive?

A

The fact that its methods don’t share much data or behaviour with one another.

27
Q

What do relationships between classes represent?

A

Coupling.

28
Q

What do relationships within each class (between methods, properties, etc.) represent?

A

Cohesion.

29
Q

Why is loose coupling favoured over tight coupling?

A

Because it results in code that is easier to change and test.

30
Q

What can be said about responsibilities in relation to testability?

A

As many responsibilities a class have, as difficult it is to test them — especially when single methods are doing a lot of different things.

31
Q

What do tests become for a given class when that class have many responsibilities?

A
  1. Longer
  2. More complex
  3. Brittle
  4. Coupled to implementation
32
Q

What are the four main takeaways of this module?

A
  1. Practice Pain-Driven Development (PDD)
  2. Each class should have a single responsibility or reason to change
  3. Strive for high cohesion and loose coupling
  4. Keep classes small, focused, and testable