Software Best Practices Flashcards
What does the S in SOLID stand for and give an example
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.
What does the O in SOLID stand for and give an example
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.
What does the L in SOLID stand for and give an example
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
What does the I in SOLID stand for and give an example
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
What does the D in SOLID stand for and give an example
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.