SOLID principles Flashcards
What is the DRY principle?
DRY (Do not Repeat Yourself)
- avoid duplication: a piece of data has a single value stored on the system. Don’t want to store duplicated data
- centralise logic to make it easier to update/test/understand
- write concise, reusable code, this saves time
What are some violations of the DRY principle?
- storing repeated data across db tables
- storing derived data across db tables
- storing same data at client and server
What are the 5 SOLID principles?
1) Single Responsibility
2) Open Closed principle
3) Liskov substitution principle
4) interface segregation principle
5) Dependency inversion principle
What do the SOLID principles help do?
They are design principles for creating OO code code that is likely to have less problems with rigidity, fragility, immobility and viscosity
Explain the first principle of the SOLID principles:
Single responsibility: a class should only have one responsibility
- why? if it has multiple, changes to one responsibility could impact the other
When detecting a breach of the single responsibility principle, what should you look for:
When a class is handling multiple things:
e.g the word processor class is responsible for saving a document, loading a document and spell checking a document, these should be separated into a SpellChecker class and a fileHandler class
What are the benefits of single responsibility?
- Reduces coupling
- easier to test, understand and maintain code
- improves code reusability
- several developers can work on coding classes simultaneously (due to low coupling)
What is the Open Closed principle:
Classes are 1) open for extension and 2) closed for modification
- why? you should be able to add new functionality to a class without altering it’s existing code
How is the open closed principle achieved?
Through abstraction, abstract classes and interfaces, and dynamic or static polymorphism
(inheritance and overriding methods)
- Use the word final in constructors/method definitions
When detecting a breach of the open closed principle, what should you look for:
class DiscountCalculator:
def calculate_discount(self, customer_type, amount):
if customer_type == “Regular”:
return amount * 0.05
elif customer_type == “Premium”:
return amount * 0.10
elif customer_type == “VIP”:
return amount * 0.20
else:
return 0
Having if, elif, elif and else that all return different values
- if a new customer_type needs to be added another elif statement is added, this violated the OC principle, because the class should just be extended to add another customer type
What are the benefits of the open closed principle?
- ## The current code base is kept stable and bug free but allows it to be used for future code development
What does the final keyword mean?
That a method or class can’t be overridden
What would be the structure for a correct implementation of the open closed principle:
- public interface IcardProvider {
public boolean makePayment(string number)
} - public class cardProviderBase implements IcardProvider { //closed for modification
public final boolean makePayment(string number) {
return number }
}
protected boolean onMakePayment(String number) { //open for extension
return(false)
}
} - public class natwestCardProvider extends CardProviderBase{
protected boolean
onMakePayment(String number) {
return(true);}
}
What are signs that the open closed principle has been breached?
- Adding new if/elif conditions to existing methods to handle new cases.
- Modifying existing classes to add new functionality instead of creating new classes or methods.
What is the Liskov substitution principle:
Classes must be substitutable for their bases classes
- why: subclasses should adhere to the behaviour expected of their parent class