Skip to content

Instantly share code, notes, and snippets.

@Kikobeats
Last active July 10, 2023 16:21
Show Gist options
  • Save Kikobeats/c8c0dc43b6148c80f702fbcab3d2c419 to your computer and use it in GitHub Desktop.
Save Kikobeats/c8c0dc43b6148c80f702fbcab3d2c419 to your computer and use it in GitHub Desktop.
ES6 Pure & self documented Functions.
/**
* ES6 Pure & self documented Functions.
*
* Why Pure?
*
* A pure function is a function where the return value is only determined
* by its input values, without observable side effects.
*
* Why Self-documented?
*
* Extract all the object path in the first line with the destructuring, so
* you know all the input that the function receive.
*
* If you have less than 4 variables, you can declare it like:
* function readFile (filepath, filename, permissions) {}
*
* Otwerwhise use object approach.
*
* for Async functions, pass callback after opts
* function readFile (opts, cb) {}
*
*/
function readFile (opts) {
const { filepath, filename, permissions } = opts
console.log(`reading file '${filename}' at ${filepath} with ${permissions} mode`)
}
readFile({filepath: '~/HOME', filename: 'test.txt', permissions: '644'})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment