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.
Dependency inversion principle
- High level modules should not depend on low level modules. Both should depend on abstractions
- Abstractions should not depend on details. Details should depend on abstractions
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?
- Encapsulation (of data and methods for working on that data)
- Inheritance, classes inherit properties and methods from other classes
- Polymorphism, a method on a super class can be implemented differently on a sub class
- Abstraction, simplify. Model classes on core characteristics (properties and methods)
De tre viktigaste principerna inom funktionell programmering
- Immutability
- Pure functions
- Functions as first class citizens
What’s a pure function?
A function without side effects. It always produces the same output given the same input.
What does it mean that functions are first class citizens?
That they can be assigned to variables, passed as arguments to and be returned from other functions.
What is recursion?
Defining a function in terms of itself.
recursiveFunction(n) { if ( n > 0 ) // do thing recursiveFunction(n-1) }
What’s a higher-order function?
A function that takes another function as input or produces a function as output. Or both…
What is currying?
Applying a higher-order function to transform a function that takes multiple arguments into a sequence of functions, each taking a single argument.
When to use recursion?
Iterative processing of data structures by breaking a problem down into smaller subproblems.
This pattern is useful for solving problems that can be divided into smaller, similar subproblems.
What’s a signal (in JS)?
Basically an observable. It is a wrapper around a value and returns a getter and a setter. It then keeps track of where the getter is invoked and treats that as a subscription and updates it anytime the setter is called.