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?
Three reasons to use getters and setters
setHow to set up your objects for method chaining?
Make every method return this
The 3 cases when to use inheritance over composition?
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.
Dependency inversion principle
Three things to do with thrown errors?
try {
functionThatMightThrow();
} catch (error) {
// One option (more noisy than console.log):
console.error(error);
// Another option:
notifyUserOfError(error);
// Another option:
reportErrorToService(error);
// OR do all three!
}What’s a functor?
A data structure that implements map
Common JS functors?
Array, stream, tree
What’s a monad?
A functor that implements flatMap()
What’s a stream?
A series of promises. A bunch of objects that will arrive at some point. Or not.
4 main principles of OOP?
De tre viktigaste principerna inom funktionell programmering
What’s a pure function?
A function without side effects. It always produces the same output given the same input.