Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

141
Visualizações
Easy-to-read way of function composition in Javascript

I'm a huge fan of functional programming. I strive to use point free notation when I can. However, I often don't understand when point free notation is appropriate or when it is overkill. I typical delema that I run into is when I have a three or fewer functions that I would love to use a compose(...) statement for to avoid creating variables. However, I often feel like the code ends up looking uglier when I use compose(...) for such few functions compared to using intermediate variables.

Here's an example of my dilemma:

// Prefering point free notation
(address, signer, execute) =>
    (atr, sequenceId) =>
        compose(
            setTxHash(atr, sequenceId),
            execute(address, signer),
            findTx,           
        )(atr, sequenceId);
// Preferring temporary variables
(address, signer, execute) =>
    (atr, sequenceId) => {
        const step1 = findTx(atr, sequenceId);
        const step2 = execute(address, signer)(step1);
        const step3 = setTxHash(atr, sequenceId)(step2);
        return step3;
    }
// function composition without using compose(...) method
(address, signer, execute) => (atr, sequenceId) => 
    setTxHash(atr, sequenceId)(
        execute(address, signer)(
            findTx(atr, sequenceId)
        )
    )

I know asking "do any of these look better than others?" may be too subjective but are there any rules of thumb I can use or any good resources that talk about this and can offer a helpful perspective?

Thanks in advance!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you need to introduce the points (variables with names) anyway because you're passing them to multiple functions, using compose is overkill. The third version of your function is the cleanest in that regard. Also, names are always useful if they are descriptive, since that aids the understandability of the code.

Only if you could write

(execute) => compose(
    setTxHash,
    execute,
    findTx,           
)

the usage of compose gains you anything (in particular, conciseness).

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda