Giter VIP home page Giter VIP logo

guide-to-writing-a-function's Introduction

Guide to Writing a Function

  • It should return something.
  • It should be pure.
  • It should do one thing.

It should return something

In JavaScript a function always returns but you should be explicit about what you want it to return.

function add (x, y) {
  x + y
}

What does this return? How would you fix this function?

It should be pure

A pure function returns the same thing everytime it is called with the same arguments

function add (x, y) {
  return x + y + Math.random()
}

Can you see why this might be frustrating?

function getUsers () {
  return Users.getAll()
}

Can you see why this might be frustrating?

Also I/O is impure. If you are changing the world with your function you can't be so sure that your program will run as intended.

function log (x) {
  console.log(x)
  return x
}

How might this ruin your life?

var lastItem = readLastStdOut(log(printItems(items)))

Holy strawman!

The point is you want to isolate all the gross parts of your program so you know where the bugs will be.

It should do one thing

function add (x, y) {
  Users.find(1).update({
    name: 'Sally'
  }) 

  return x + y
}

You should be cringing.

function add (x, y) {
  console.log(x, '+', y, '=', x + y)
  return x + y
}

These are two seperate things. Don't conflate purposes.

Don't be so serious

But don't be a wimp either. These rules are almost always applicable. If you think you need to break them you are probably wrong (aka: need to write a new function). But break them if you must, it's okay!

guide-to-writing-a-function's People

Stargazers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.