0

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!

1
  • 1
    I think this is a problem: "I strive to use point free notation when I can." Think of it as one tool in your toolbox. Use it when it makes your code more readable. Skip it when it doesn't. I find const sum = reduce (add, 0) beautiful and elegant, an improvement on the pointed version. But I would absolutely prefer your second example to your first and your third to either of them. Commented Aug 28, 2022 at 13:50

1 Answer 1

2

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).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.