Read: Class 02 Classes, Inheritance, Functional P - 401-advanced-javascript-muna/amman-javascript-401d1 GitHub Wiki

Functional Programming

Functional Programming: programming with a distinction between pure code and impure code.

  • Pure code has no side effects. It’s referentially transparent. It means the same results every time you run it.

Pure functions return a value solely based on what was passed into it. The idea behind it is that it will be a pure function if it always returns the same result if the same values are passed into it, and it doesn’t modify values outside of ts scope, that makes it independent from any state in the system. Therefore, a pure function can never mutate data, produce no side effects and can be easily reused.

A “pure” function (no side effects, esoteric state, consistent results)

  • Impure code contains (and often relies on) side effects, so running it twice is different from running it once. An example of a non pure function will be a function that makes an API call or returns an unpredictable result.

An “impure” function (relies on external state, produces a “side effect”)

  • Procedural is about modeling your solution as sequential steps.
  • Object Oriented is about modeling your solution as communicating objects.
  • Functional programming is about modeling your solution as pure functions.

Higher order functions:A higher order function is simply a function that operates on other functions. They can take in a function as input, or return one as output

The main tenets of functional programming

  • Pure Functions
  • Higher Order Functions (Examples: map, filter, reduce)
  • Immutable State

Objects and Inheritance

The same thing with classes (much cleaner!)

function() becomes class {} call() becomes extends

Errors

  • Error messages are super important tools for debugging broken code.
  • Javascript has many built in error messages, but you can also define your own errors in your programs.
  • It is important to not forget that errors will happen in production.
  • Error logs are kept in order to fix bugs in productions.
  • Writing good error messages is critical for finding and fixing bugs in deployed applications

Writing Good Error Messages

A great error message should have the following features

  • a timestamp so that a timeline of the error can be made
  • a message about the problem that occurred
  • a message about the cause of the problem
  • a consistent format (so that it can be parsed and searched)
  • a severity level (low, med high) or (0 - 10)

// creating a smart error

class Bug extends Error {

constructor({problem, cause, level=0, timestamp=new Date().toISOString()}){

`super(__ERROR__ ${problem}: ${cause} (LEVEL ${level}) (TIMESTAMP ${timestamp})`)`

`this.problem = problem`

`this.cause = cause`

`this.level = level`

`this.timestamp = timestamp`

}

}

let error = new Bug({problem: 'cannot create user', cause: 'requires password'})

Error Cheat Sheet

  • Type Reason
  • Error generic error
  • ReferenceError an attempt was made to access a variable that is not defined
  • SyntaxError the javascript is not valid
  • TypeError a provided argument was no the allowable type
  • SystemError a Node.js error that occurs when a system error has occurred

System Error Cheat Sheet

  • EACCESS - an attempt to access a file without the right permissions
  • EADDRINUSE - an attempt to start a server on a PORT that is already in use
  • ECONNREFUSED - a connection was deliberately refused by the target machine
  • ECONNRESET - a connection was forcibly closed by a peer
  • EEXIST - a file exists and the attempted action required that it didn’t
  • EISDIR - an action expected to act on a file but found a directory
  • EMFILE - too many files were open for your operating system to handle
  • ENOENT - an action expected a file, but did not find one
  • ENOTDIR - an action expected a directory, but found something else
  • ENOTEMPTY - an action expected an empty directory, but found one with data in it
  • EPERM - an attempt to do something that you currently don’t have permissions to do
  • EPIPE - an attempt to write data to a connection that had been closed