Is chaining functions like doX(doY(doZ(data))) poor practice? [closed]
I've noticed a lot of my code ends up looking like:
doX(doY(doZ(data)))
Is there any thing wrong with this?
javascript
closed as primarily opinion-based by Adriani6, Laxmikant Dange, Lelio Faieta, cнŝdk, JJJ Nov 14 '18 at 13:49
Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I've noticed a lot of my code ends up looking like:
doX(doY(doZ(data)))
Is there any thing wrong with this?
javascript
closed as primarily opinion-based by Adriani6, Laxmikant Dange, Lelio Faieta, cнŝdk, JJJ Nov 14 '18 at 13:49
Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.
1
this style is very common in functional programming (which javascript supports) and is meaningfull
– Nikos M.
Nov 14 '18 at 12:10
What is the alternative you are comparing it to?
– Thilo
Nov 14 '18 at 12:17
add a comment |
I've noticed a lot of my code ends up looking like:
doX(doY(doZ(data)))
Is there any thing wrong with this?
javascript
I've noticed a lot of my code ends up looking like:
doX(doY(doZ(data)))
Is there any thing wrong with this?
javascript
javascript
asked Nov 14 '18 at 11:29
RedHorseRedHorse
283
283
closed as primarily opinion-based by Adriani6, Laxmikant Dange, Lelio Faieta, cнŝdk, JJJ Nov 14 '18 at 13:49
Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as primarily opinion-based by Adriani6, Laxmikant Dange, Lelio Faieta, cнŝdk, JJJ Nov 14 '18 at 13:49
Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.
1
this style is very common in functional programming (which javascript supports) and is meaningfull
– Nikos M.
Nov 14 '18 at 12:10
What is the alternative you are comparing it to?
– Thilo
Nov 14 '18 at 12:17
add a comment |
1
this style is very common in functional programming (which javascript supports) and is meaningfull
– Nikos M.
Nov 14 '18 at 12:10
What is the alternative you are comparing it to?
– Thilo
Nov 14 '18 at 12:17
1
1
this style is very common in functional programming (which javascript supports) and is meaningfull
– Nikos M.
Nov 14 '18 at 12:10
this style is very common in functional programming (which javascript supports) and is meaningfull
– Nikos M.
Nov 14 '18 at 12:10
What is the alternative you are comparing it to?
– Thilo
Nov 14 '18 at 12:17
What is the alternative you are comparing it to?
– Thilo
Nov 14 '18 at 12:17
add a comment |
2 Answers
2
active
oldest
votes
I’d consider this good practice - it encourages Curly’s law (every function should do one thing), allowing you to reuse parts of the function chain later in different circumstances.
If you find the chains get too long and start feeling cumbersome, just break somewhere logical and assign a variable with a meaningful name:
const almostResult = doY(doZ(data));
return doX(almostResult);
add a comment |
There is nothing wrong with it, as long as your code is readable and easy to work with.
The functional approach
If you want to code in a more functional style, I recommend you use compose
form lodash or ramda. It takes the result from one function and pipes it into the next one (starting on the right hand side).
import { compose } from 'ramda';
...
const doAll = compose(doX, doY, doZ);
const result = doAll(data);
I recommend Professor Frisby's Mostly adequate guide to Functional Programming, for why this is a cool way to program (See chapter 5 for compose).
Ecmascript pipes is coming (maybe)
In the not to distant future we will hopefully get the pipe operator (|>
), which will give the language itself a very elegant way to combine functions.
const result = data
|> doZ
|> doY
|> doX
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I’d consider this good practice - it encourages Curly’s law (every function should do one thing), allowing you to reuse parts of the function chain later in different circumstances.
If you find the chains get too long and start feeling cumbersome, just break somewhere logical and assign a variable with a meaningful name:
const almostResult = doY(doZ(data));
return doX(almostResult);
add a comment |
I’d consider this good practice - it encourages Curly’s law (every function should do one thing), allowing you to reuse parts of the function chain later in different circumstances.
If you find the chains get too long and start feeling cumbersome, just break somewhere logical and assign a variable with a meaningful name:
const almostResult = doY(doZ(data));
return doX(almostResult);
add a comment |
I’d consider this good practice - it encourages Curly’s law (every function should do one thing), allowing you to reuse parts of the function chain later in different circumstances.
If you find the chains get too long and start feeling cumbersome, just break somewhere logical and assign a variable with a meaningful name:
const almostResult = doY(doZ(data));
return doX(almostResult);
I’d consider this good practice - it encourages Curly’s law (every function should do one thing), allowing you to reuse parts of the function chain later in different circumstances.
If you find the chains get too long and start feeling cumbersome, just break somewhere logical and assign a variable with a meaningful name:
const almostResult = doY(doZ(data));
return doX(almostResult);
answered Nov 14 '18 at 11:49
MTCosterMTCoster
3,66122141
3,66122141
add a comment |
add a comment |
There is nothing wrong with it, as long as your code is readable and easy to work with.
The functional approach
If you want to code in a more functional style, I recommend you use compose
form lodash or ramda. It takes the result from one function and pipes it into the next one (starting on the right hand side).
import { compose } from 'ramda';
...
const doAll = compose(doX, doY, doZ);
const result = doAll(data);
I recommend Professor Frisby's Mostly adequate guide to Functional Programming, for why this is a cool way to program (See chapter 5 for compose).
Ecmascript pipes is coming (maybe)
In the not to distant future we will hopefully get the pipe operator (|>
), which will give the language itself a very elegant way to combine functions.
const result = data
|> doZ
|> doY
|> doX
add a comment |
There is nothing wrong with it, as long as your code is readable and easy to work with.
The functional approach
If you want to code in a more functional style, I recommend you use compose
form lodash or ramda. It takes the result from one function and pipes it into the next one (starting on the right hand side).
import { compose } from 'ramda';
...
const doAll = compose(doX, doY, doZ);
const result = doAll(data);
I recommend Professor Frisby's Mostly adequate guide to Functional Programming, for why this is a cool way to program (See chapter 5 for compose).
Ecmascript pipes is coming (maybe)
In the not to distant future we will hopefully get the pipe operator (|>
), which will give the language itself a very elegant way to combine functions.
const result = data
|> doZ
|> doY
|> doX
add a comment |
There is nothing wrong with it, as long as your code is readable and easy to work with.
The functional approach
If you want to code in a more functional style, I recommend you use compose
form lodash or ramda. It takes the result from one function and pipes it into the next one (starting on the right hand side).
import { compose } from 'ramda';
...
const doAll = compose(doX, doY, doZ);
const result = doAll(data);
I recommend Professor Frisby's Mostly adequate guide to Functional Programming, for why this is a cool way to program (See chapter 5 for compose).
Ecmascript pipes is coming (maybe)
In the not to distant future we will hopefully get the pipe operator (|>
), which will give the language itself a very elegant way to combine functions.
const result = data
|> doZ
|> doY
|> doX
There is nothing wrong with it, as long as your code is readable and easy to work with.
The functional approach
If you want to code in a more functional style, I recommend you use compose
form lodash or ramda. It takes the result from one function and pipes it into the next one (starting on the right hand side).
import { compose } from 'ramda';
...
const doAll = compose(doX, doY, doZ);
const result = doAll(data);
I recommend Professor Frisby's Mostly adequate guide to Functional Programming, for why this is a cool way to program (See chapter 5 for compose).
Ecmascript pipes is coming (maybe)
In the not to distant future we will hopefully get the pipe operator (|>
), which will give the language itself a very elegant way to combine functions.
const result = data
|> doZ
|> doY
|> doX
answered Nov 14 '18 at 12:15
tomajtomaj
706724
706724
add a comment |
add a comment |
1
this style is very common in functional programming (which javascript supports) and is meaningfull
– Nikos M.
Nov 14 '18 at 12:10
What is the alternative you are comparing it to?
– Thilo
Nov 14 '18 at 12:17