Paradigms And Patterns Flashcards

1
Q

Liskov substitution principle

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Use ___ and ___ variable names

A

Meaningful
Pronounceable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Use the same ___ for the same type of ___

A

Vocabulary
Variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Instead of short circuiting and conditionals?

A

Use default parameters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How many function parameters?

A

Ideally two or fewer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Three benefits of making sure functions do one thing only?

A

Easier to
1. compose
2. test
3. reason about

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to set default objects?

A

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);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Instead of using flags as function parameters?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to avoid pitfalls of side effects?

A
  1. Use clear structure when sharing state between objects
  2. Use immutable data structures
  3. Centralize where side effects can occur
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Three reasons to use getters and setters

A
  1. Easy to add validation to set
  2. Easy to add logging and error handling
  3. Encapsulates internal representation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to set up your objects for method chaining?

A

Make every method return this

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

The 3 cases when to use inheritance over composition?

A
  1. Relationship is ‘is-a’ (and not ‘has-a’)
  2. You can reuse code from base class
  3. You want to make global changes to derived classes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Single responsibility principle

A

There should never be more than one reason for a class to change.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Open/closed principle

A

Software entities should be open for extension but closed for modification.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Interface segregation principle

A

Clients should not be forced to depend upon interfaces that they do not use.
Split functionality. Make things optional.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Dependency inversion principle

A
  1. High level modules should not depend on low level modules. Both should depend on abstractions
  2. Abstractions should not depend on details. Details should depend on abstractions
17
Q

Three things to do with thrown errors?

A
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!
}
18
Q

What’s a functor?

A

A data structure that implements map

19
Q

Common JS functors?

A

Array, stream, tree

20
Q

What’s a monad?

A

A functor that implements flatMap()

21
Q

What’s a stream?

A

A series of promises. A bunch of objects that will arrive at some point. Or not.

22
Q

4 main principles of OOP?

A
  1. Encapsulation (of data and methods for working on that data)
  2. Inheritance, classes inherit properties and methods from other classes
  3. Polymorphism, a method on a super class can be implemented differently on a sub class
  4. Abstraction, simplify. Model classes on core characteristics (properties and methods)
23
Q

De tre viktigaste principerna inom funktionell programmering

A
  1. Immutability
  2. Pure functions
  3. Functions as first class citizens
24
Q

What’s a pure function?

A

A function without side effects. It always produces the same output given the same input.

25
Q

What does it mean that functions are first class citizens?

A

That they can be assigned to variables, passed as arguments to and be returned from other functions.

26
Q

What is recursion?

A

Defining a function in terms of itself.

recursiveFunction(n) {
if ( n > 0 )
  // do thing
  recursiveFunction(n-1)
}
27
Q

What’s a higher-order function?

A

A function that takes another function as input or produces a function as output. Or both…

28
Q

What is currying?

A

Applying a higher-order function to transform a function that takes multiple arguments into a sequence of functions, each taking a single argument.

29
Q

When to use recursion?

A

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.

30
Q

What’s a signal (in JS)?

A

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.