Paradigms And Patterns Flashcards
Liskov substitution principle
Base class and subclass should be interchangeable. For example Square shouldn’t be a sub class to Rectangle even if it is mathematically. They should both be sub classes to Shape.
Use ___ and ___ variable names
Meaningful
Pronounceable
Use the same ___ for the same type of ___
Vocabulary
Variable
Instead of short circuiting and conditionals?
Use default parameters
How many function parameters?
Ideally two or fewer
Three benefits of making sure functions do one thing only?
Easier to
1. compose
2. test
3. reason about
How to set default objects?
Use Object.assign:
const menuConfig = { title: "Order", // User did not include 'body' key buttonText: "Send", cancellable: true }; function createMenu(config) { let finalConfig = Object.assign( { title: "Foo", body: "Bar", buttonText: "Baz", cancellable: true }, config ); return finalConfig // config now equals: {title: "Order", body: "Bar", buttonText: "Send", cancellable: true} // ... } createMenu(menuConfig);
Instead of using flags as function parameters?
Split out your functions if they are following different code paths based on a boolean.
function createFile(name) { fs.create(name); } function createTempFile(name) { createFile(`./temp/${name}`); }
How to avoid pitfalls of side effects?
- Use clear structure when sharing state between objects
- Use immutable data structures
- Centralize where side effects can occur
Three reasons to use getters and setters
- Easy to add validation to
set
- Easy to add logging and error handling
- Encapsulates internal representation
How to set up your objects for method chaining?
Make every method return this
The 3 cases when to use inheritance over composition?
- Relationship is ‘is-a’ (and not ‘has-a’)
- You can reuse code from base class
- You want to make global changes to derived classes
Single responsibility principle
There should never be more than one reason for a class to change.
Open/closed principle
Software entities should be open for extension but closed for modification.
Interface segregation principle
Clients should not be forced to depend upon interfaces that they do not use.
Split functionality. Make things optional.