Good Production Code Flashcards

1
Q

What does SOLID stand for?

A
  1. Single Responsability
  2. Open Closed
  3. Liskov’s Substitution
  4. Interface
  5. Segregation
  6. Dependency Inversion
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the Single Responsibility Principle?

A

There should NEVER be more than ONE reason for a class to change → ONE JOB

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

What are (3) indicators for the Single Responsibility Principle?

A
  1. Many public functions
  2. Unrelated functions
  3. Various class clients
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a class client?

A

A class using another class’s public funtions

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

How to follow the Single Responsibility Principle?

A

Division into multiple simple classes with…

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

What is the Open Closed Principle?

A

Classes should be open for extension but closed for modification.

→ Never modify a class in order to handle a new one

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

What are (2) indicators for the Open Closed Principle?

A
  1. Modification needed to handle a new class

3. Switch-Case to check object type

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

How to follow the Open Closed Principle?

A
  1. (Abstract classes)

2. Interfaces

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

When is it okay to use switch-case?

A

In a Factory Pattern to ask “Who do you wanna be?”

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

Explain the Dependency Inversion Principle

A

Never depend on (call) a concrete class! Always depend on your own abstractions.

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

What are (2) indicators for the Dependency Inversion Principle?

A
  1. Dependency on concrete classes

2. Long chain of dependencies (coupling)

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

How to follow the Dependency Inversion Princliple?

A
  1. Dependency upon high level abstractions with low level implementation
  2. Interfaces as Boundaries between layers
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What patterns use the Dependency Inversion Principle?

A
  1. Adapter
  2. Template
  3. Strategy
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Explain the adapter pattern

A

Integration (use) of any class without your own Interface and without modifying the target class

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

Explain the template pattern

A

Seperation of algorithm and its implementations with inheritance

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

Explain the strategy pattern

A

Algorithm and Implementation depend on Abstraction (Interface and Composition)

17
Q

Was is the Interface Segregation Principle?

A

Don’t force clients to depend upon interfaces they do not use.

→ No fat interfaces!

18
Q

What is a client interface?

A

Public Function

→ Service proposed to other classes

19
Q

What are (3) indicators for unterface segregation?

A
  • Fat interface
  • High coupling
  • Complex classes
20
Q

What is a fat interface?

A

One that has many public functions

21
Q

How to follow interface segregation?

A

Split up interfaces so they are…

  1. Thin
  2. Client-specific
  3. Independent
22
Q

What general problem is described by abstraction inconsistency?

A

Bad functions and classes are everywhere!

→ abstraction naming and behaviour are inconsistent

23
Q

What (3) can you do to avoid abstraction inconsistency?

A
  1. never assume behaviour but test it and read the doc
  2. Principle of Least Astonishment for naming and method signatures
  3. abstraction hierarchy → one level of abstraction per class/function
24
Q

Explain Liskov’s Substitution Principle in your own words!

A

A child class should have the same public behaviour as it’s parent. Thus it should be able to replace it at any time.

25
What happens when you don't follow Liskov's Substitution Principle?
1. Rigid and fragile system 2. Open-Closed Principle violated (switch-case needed) 3. Silent bugs
26
What is the public behaviour of an object?
What it's doing → Behaviour that it's clients depend on
27
What is the private behaviour of an object?
How it works
28
What are pre/post conditions?
State of an object before/after a function call 1. Must be true to execute 2. Will be true after execution
29
What is a silent bug?
One that is not visible during runtime (no crash)
30
What is the newspaper metaphor?
Classes should be structured from abstraction to detail
31
What is the state of an object?
Value of it's variables at a certain time
32
What are the advantages of "assert" over "throw new exception"? When do you use it in terms of Liskov's Substitution Principle?
You can disable "assert" to improve runtime Used before and after function computation to test pre/post conditions