3
\$\begingroup\$

I would like to know if there are any things I could improve with the following code regarding writing it in a FP style:

server.js:

const express = require('express');
const app = express();
const person = require('./person');

app.get('/create-person/:name', (req, res) => {
    person.addPerson(req.params.name);
    res.sendStatus(201);
});

app.get('/get-person/:name', function (req, res) {
    res.json(person.getPerson(req.params.name));
})

app.listen(3000);

person.js:

// Impure
let personList = [];
// Impure
function set(newPersonList) {
    personList = newPersonList;
    console.log(personList);
}
// Pure
function createPerson(name) {
    return {
        name
    };
}
// Pure
function addPerson(name, createPersonArg = createPerson, personListArg = personList, setArg = set) {
    return setArg([...personListArg, createPersonArg(name)]);
}
// Pure
function getPerson(name, personListArg = personList) {
    return personListArg.find((person) => {
        return name === person.name
    });
}

module.exports = {
    addPerson,
    getPerson
}

I'm using default parameters. I'm also wondering if this is the correct way of using them in FP.

I am aware I am creating a resource with GET. So ignore that, it is for being able to use the browser quickly to create a resource in this example.

\$\endgroup\$
  • 1
    \$\begingroup\$ In regards to your REST API. It is common to send POST instead of GET to create a resource. The HTTP methods (verbs) are meant exactly for this differentiation between operations. Use POST /person to create new person. Use PATCH /person/:name to update existing person. And GET /person/:name to get existing person's data. Or at least that is the usual way... \$\endgroup\$ – slepic Dec 13 '19 at 16:23
  • \$\begingroup\$ @slepic Please add an answer instead of a comment. Refer to the section When shouldn't I comment? on Comment everywhere. \$\endgroup\$ – Sᴀᴍ Onᴇᴌᴀ Dec 13 '19 at 16:29
  • \$\begingroup\$ @SᴀᴍOnᴇᴌᴀ I don't think my comment makes for a decent code review and so I didn't post it as answer. Firstly, it would change the api to become something different then current working code. Secondly, I didnt make any observation about the code itself and I don't think I will... \$\endgroup\$ – slepic Dec 13 '19 at 16:38
  • 1
    \$\begingroup\$ @slepic it technically is an observation about the code, even if it just pertains to the API... "Answers need not cover every issue in every line of the code. Short answers are acceptable, as long as you explain your reasoning. Do not provide suggestions for improvements in a comment, even if your suggestion makes a very short answer." From What goes into an answer \$\endgroup\$ – Sᴀᴍ Onᴇᴌᴀ Dec 13 '19 at 16:47
  • \$\begingroup\$ @slepic I understand that POST is created, my bad for not have mentioning this, but I used GET for simplistic reasons in this instance to create (via web browser request). \$\endgroup\$ – Karl Morrison Dec 16 '19 at 10:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.