Chapter 2: Designing Classes with a Single Responsibility Flashcards

Learn the concepts in chapter 2 of the Practicing Object-Oriented Design book

1
Q

What is considered as a TRUE code?

A

A code that is:

  • Transparent
  • Reasonable
  • Usable
  • Exemplary
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does a TRUE code measure?

A

That a code written now will be easily changed in the future

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

What does a Transparent code mean?

A

The consequences of change should be obvious in the code that is changing now and in distant code that relies on it

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

What does a Reasonable code mean?

A

The cost of any change should be proportional to the benefits the change archives

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

What does a Usable code mean?

A

Existing code should be usable in new and unexpected contexts

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

What does an Exemplary code mean?

A

The code itself should encourage those who change it to perpetuate the qualities it has

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

What is the first step in creating code that is TRUE?

A

It is to ensure that each class has a single, well-defined responsibility

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

How do you determine if a class has Single Responsibility?

A
  • You can ask questions using the method names
  • You can create sentences with the methods names and check if you need conjunctions like ‘and’ or ‘or’ to build the sentence, if so, maybe the method are doing too much.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is Cohesion?

A

When everything in a class is related to its central purpose.

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

What are the techniques to create code that embraces changes?

A

Depend on behavior, not data
- Hide instance variables
- Hide data structures
Enforce Single Responsibility everywhere
- Extract extra responsibilities from methods
- Isolate Extra Responsabilities in classes

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

How does the “Hide instance variables” technique work?

A

Always wrap instance variables in accessor methods instead of directly referring to them.

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

How does the “Hide data structures” technique work?

A

You need to separate structure from meaning. For this, we can use the Ruby Struct class to wrap a strucuture

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

What are the benefits of use methods that have single responsability?

A
  • Expose previously hidden qualities
  • Avoid the need for comments
  • Encourage reuse
  • Are easy to move to another class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly