Is chaining functions like doX(doY(doZ(data))) poor practice? [closed]












3















I've noticed a lot of my code ends up looking like:



doX(doY(doZ(data)))


Is there any thing wrong with this?










share|improve this 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
















3















I've noticed a lot of my code ends up looking like:



doX(doY(doZ(data)))


Is there any thing wrong with this?










share|improve this 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














3












3








3








I've noticed a lot of my code ends up looking like:



doX(doY(doZ(data)))


Is there any thing wrong with this?










share|improve this question














I've noticed a lot of my code ends up looking like:



doX(doY(doZ(data)))


Is there any thing wrong with this?







javascript






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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














  • 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












2 Answers
2






active

oldest

votes


















2














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





share|improve this answer































    2














    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





    share|improve this answer






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














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





      share|improve this answer




























        2














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





        share|improve this answer


























          2












          2








          2







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





          share|improve this answer













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






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 11:49









          MTCosterMTCoster

          3,66122141




          3,66122141

























              2














              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





              share|improve this answer




























                2














                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





                share|improve this answer


























                  2












                  2








                  2







                  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





                  share|improve this answer













                  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






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 14 '18 at 12:15









                  tomajtomaj

                  706724




                  706724















                      Popular posts from this blog

                      Florida Star v. B. J. F.

                      Danny Elfman

                      Lugert, Oklahoma