Software Best Practices Flashcards

1
Q

What does the S in SOLID stand for and give an example

A

Single responsibility principle

A class should have only one reason to change, meaning that a class should only have one job

Ex: AreaCalculator should only calculate area. Printing the output should be handled by another class, so if you want to change formatting it relegated to that one class and doesn’t add to the area calculator.

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

What does the O in SOLID stand for and give an example

A

Open-close principle

Open for extension, but closed for. modification

Example: Area calculator sum method should not need to determine type of shape and different formula. All shapes should be responsible for calculating their own area.

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

What does the L in SOLID stand for and give an example

A

Liskov substitution principle

Subtypes must be suitable for their base types. Cautions the use of inheritance, a base class should always be capable of being replaced with subclass

Example: TransportationDevice with a start engine method. Car can extend TransportationDevice, but adding bike would require implementing this method unnecessarily. Can then break up base classes

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

What does the I in SOLID stand for and give an example

A

Interface segregation principle

A client should never be forced to implement an interface that it doesn’t use or clients shouldn’t be forced to depend on methods they do not use

Examples: Shape interface that has a volume and area method. Many shapes don’t have volume, so this would require certain shapes to implement behavior they don’t need

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

What does the D in SOLID stand for and give an example

A

Dependency Inversion Principle

High level modules should not be impacted by low level modules and should be easily reusable

Example: DBConnector class that will allow a high level abstraction to be passed to constructor instead of a low level. A more generic DBDriver vs declaring MySQL driver.

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