design a method returning a value or changing some data but not both









up vote
0
down vote

favorite












I vaguely remember reading somewhere a while ago that either a method/function should return a value without modifying the state or should process some data changing a state but not returning data. It is beyond simple getters and setters.



I cannot figure where I read that, the rationale and if this was a sound principle. It may be simpler to test for sure but is there any other reason? Is there is there a related design principle or pattern? Any clue or link appreciated.



Thanks
Olivier










share|improve this question

























    up vote
    0
    down vote

    favorite












    I vaguely remember reading somewhere a while ago that either a method/function should return a value without modifying the state or should process some data changing a state but not returning data. It is beyond simple getters and setters.



    I cannot figure where I read that, the rationale and if this was a sound principle. It may be simpler to test for sure but is there any other reason? Is there is there a related design principle or pattern? Any clue or link appreciated.



    Thanks
    Olivier










    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I vaguely remember reading somewhere a while ago that either a method/function should return a value without modifying the state or should process some data changing a state but not returning data. It is beyond simple getters and setters.



      I cannot figure where I read that, the rationale and if this was a sound principle. It may be simpler to test for sure but is there any other reason? Is there is there a related design principle or pattern? Any clue or link appreciated.



      Thanks
      Olivier










      share|improve this question













      I vaguely remember reading somewhere a while ago that either a method/function should return a value without modifying the state or should process some data changing a state but not returning data. It is beyond simple getters and setters.



      I cannot figure where I read that, the rationale and if this was a sound principle. It may be simpler to test for sure but is there any other reason? Is there is there a related design principle or pattern? Any clue or link appreciated.



      Thanks
      Olivier







      design-patterns






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 3:39









      Olivier D

      1089




      1089






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          That principle is called Command–query separation.



          From wikipedia:




          Command–query separation (CQS) is a principle of imperative computer programming. It was devised by Bertrand Meyer as part of his pioneering work on the Eiffel programming language.



          It states that every method should either be a command that performs an action, or a query that returns data to the caller, but not both. In other words, Asking a question should not change the answer. More formally, methods should return a value only if they are referentially transparent and hence possess no side effects.




          Personally, I think this principle is very helpful. It is based on a common observation that: when we see a function that returns some data, we tend to think that it does not change any state of data. In other words, that function does not have any side-effect. In constrast, functions with side-effects are considered "risky" and therefore should have a clear indication (returning void type). You see, C# has the concept of Properties where you change state of a property by using the assignment symbol which is a stronger indication than setter methods.



          To conclude, the CQS principle helps our code to be easier to read and reason since managing how state changes is crucial in programming.



          You can find more useful links in the wikipedia article, and this and this.






          share|improve this answer




















          • Good answer. I'd argue that trying to apply this principle to all of your code would make it far more tedious to write and maintain, however. For instance: straight-forward methods like Queue.Dequeue(); would need to be broken into two commands: one to remove the next item from the queue (data structure modification) and one to retrieve the result.
            – Ryan Pierce Williams
            Nov 10 at 9:39







          • 1




            You might be interesting in how Functional Programming handles your case: returning both the element and new queue/stack: fsharpforfunandprofit.com/posts/stack-based-calculator (section Popping the stack).
            – Nghia Bui
            Nov 10 at 9:46










          • Functional Programming is fun, though I've had limited opportunity to use full-on functional programming out in the field. You could certainly adhere to the pattern by creating a new stack like that - though then I'd question why you would want to take the performance hit of creating another stack every time you push/pop just to adhere to the pattern? Nice idea though :)
            – Ryan Pierce Williams
            Nov 10 at 9:53







          • 1




            If you read the F# implementation in the link, you will see that it does exactly what you proposed. Anw, in general, if you find a particular part in your program is curical with performance, then implement it with mutability style if it can boost the performance. We should not be dogmatic with immutabilty or any principle, but rather find a trade-off depending on our situation.
            – Nghia Bui
            Nov 10 at 10:02






          • 1




            "But how to keep all references to the stack the same while still adhering to the pattern? " That's an interesting and big topic which cannot be discussed in comments, you can ask another question tagged with function-programming.
            – Nghia Bui
            Nov 10 at 10:06










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235806%2fdesign-a-method-returning-a-value-or-changing-some-data-but-not-both%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          That principle is called Command–query separation.



          From wikipedia:




          Command–query separation (CQS) is a principle of imperative computer programming. It was devised by Bertrand Meyer as part of his pioneering work on the Eiffel programming language.



          It states that every method should either be a command that performs an action, or a query that returns data to the caller, but not both. In other words, Asking a question should not change the answer. More formally, methods should return a value only if they are referentially transparent and hence possess no side effects.




          Personally, I think this principle is very helpful. It is based on a common observation that: when we see a function that returns some data, we tend to think that it does not change any state of data. In other words, that function does not have any side-effect. In constrast, functions with side-effects are considered "risky" and therefore should have a clear indication (returning void type). You see, C# has the concept of Properties where you change state of a property by using the assignment symbol which is a stronger indication than setter methods.



          To conclude, the CQS principle helps our code to be easier to read and reason since managing how state changes is crucial in programming.



          You can find more useful links in the wikipedia article, and this and this.






          share|improve this answer




















          • Good answer. I'd argue that trying to apply this principle to all of your code would make it far more tedious to write and maintain, however. For instance: straight-forward methods like Queue.Dequeue(); would need to be broken into two commands: one to remove the next item from the queue (data structure modification) and one to retrieve the result.
            – Ryan Pierce Williams
            Nov 10 at 9:39







          • 1




            You might be interesting in how Functional Programming handles your case: returning both the element and new queue/stack: fsharpforfunandprofit.com/posts/stack-based-calculator (section Popping the stack).
            – Nghia Bui
            Nov 10 at 9:46










          • Functional Programming is fun, though I've had limited opportunity to use full-on functional programming out in the field. You could certainly adhere to the pattern by creating a new stack like that - though then I'd question why you would want to take the performance hit of creating another stack every time you push/pop just to adhere to the pattern? Nice idea though :)
            – Ryan Pierce Williams
            Nov 10 at 9:53







          • 1




            If you read the F# implementation in the link, you will see that it does exactly what you proposed. Anw, in general, if you find a particular part in your program is curical with performance, then implement it with mutability style if it can boost the performance. We should not be dogmatic with immutabilty or any principle, but rather find a trade-off depending on our situation.
            – Nghia Bui
            Nov 10 at 10:02






          • 1




            "But how to keep all references to the stack the same while still adhering to the pattern? " That's an interesting and big topic which cannot be discussed in comments, you can ask another question tagged with function-programming.
            – Nghia Bui
            Nov 10 at 10:06














          up vote
          1
          down vote



          accepted










          That principle is called Command–query separation.



          From wikipedia:




          Command–query separation (CQS) is a principle of imperative computer programming. It was devised by Bertrand Meyer as part of his pioneering work on the Eiffel programming language.



          It states that every method should either be a command that performs an action, or a query that returns data to the caller, but not both. In other words, Asking a question should not change the answer. More formally, methods should return a value only if they are referentially transparent and hence possess no side effects.




          Personally, I think this principle is very helpful. It is based on a common observation that: when we see a function that returns some data, we tend to think that it does not change any state of data. In other words, that function does not have any side-effect. In constrast, functions with side-effects are considered "risky" and therefore should have a clear indication (returning void type). You see, C# has the concept of Properties where you change state of a property by using the assignment symbol which is a stronger indication than setter methods.



          To conclude, the CQS principle helps our code to be easier to read and reason since managing how state changes is crucial in programming.



          You can find more useful links in the wikipedia article, and this and this.






          share|improve this answer




















          • Good answer. I'd argue that trying to apply this principle to all of your code would make it far more tedious to write and maintain, however. For instance: straight-forward methods like Queue.Dequeue(); would need to be broken into two commands: one to remove the next item from the queue (data structure modification) and one to retrieve the result.
            – Ryan Pierce Williams
            Nov 10 at 9:39







          • 1




            You might be interesting in how Functional Programming handles your case: returning both the element and new queue/stack: fsharpforfunandprofit.com/posts/stack-based-calculator (section Popping the stack).
            – Nghia Bui
            Nov 10 at 9:46










          • Functional Programming is fun, though I've had limited opportunity to use full-on functional programming out in the field. You could certainly adhere to the pattern by creating a new stack like that - though then I'd question why you would want to take the performance hit of creating another stack every time you push/pop just to adhere to the pattern? Nice idea though :)
            – Ryan Pierce Williams
            Nov 10 at 9:53







          • 1




            If you read the F# implementation in the link, you will see that it does exactly what you proposed. Anw, in general, if you find a particular part in your program is curical with performance, then implement it with mutability style if it can boost the performance. We should not be dogmatic with immutabilty or any principle, but rather find a trade-off depending on our situation.
            – Nghia Bui
            Nov 10 at 10:02






          • 1




            "But how to keep all references to the stack the same while still adhering to the pattern? " That's an interesting and big topic which cannot be discussed in comments, you can ask another question tagged with function-programming.
            – Nghia Bui
            Nov 10 at 10:06












          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          That principle is called Command–query separation.



          From wikipedia:




          Command–query separation (CQS) is a principle of imperative computer programming. It was devised by Bertrand Meyer as part of his pioneering work on the Eiffel programming language.



          It states that every method should either be a command that performs an action, or a query that returns data to the caller, but not both. In other words, Asking a question should not change the answer. More formally, methods should return a value only if they are referentially transparent and hence possess no side effects.




          Personally, I think this principle is very helpful. It is based on a common observation that: when we see a function that returns some data, we tend to think that it does not change any state of data. In other words, that function does not have any side-effect. In constrast, functions with side-effects are considered "risky" and therefore should have a clear indication (returning void type). You see, C# has the concept of Properties where you change state of a property by using the assignment symbol which is a stronger indication than setter methods.



          To conclude, the CQS principle helps our code to be easier to read and reason since managing how state changes is crucial in programming.



          You can find more useful links in the wikipedia article, and this and this.






          share|improve this answer












          That principle is called Command–query separation.



          From wikipedia:




          Command–query separation (CQS) is a principle of imperative computer programming. It was devised by Bertrand Meyer as part of his pioneering work on the Eiffel programming language.



          It states that every method should either be a command that performs an action, or a query that returns data to the caller, but not both. In other words, Asking a question should not change the answer. More formally, methods should return a value only if they are referentially transparent and hence possess no side effects.




          Personally, I think this principle is very helpful. It is based on a common observation that: when we see a function that returns some data, we tend to think that it does not change any state of data. In other words, that function does not have any side-effect. In constrast, functions with side-effects are considered "risky" and therefore should have a clear indication (returning void type). You see, C# has the concept of Properties where you change state of a property by using the assignment symbol which is a stronger indication than setter methods.



          To conclude, the CQS principle helps our code to be easier to read and reason since managing how state changes is crucial in programming.



          You can find more useful links in the wikipedia article, and this and this.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 9:30









          Nghia Bui

          1,443812




          1,443812











          • Good answer. I'd argue that trying to apply this principle to all of your code would make it far more tedious to write and maintain, however. For instance: straight-forward methods like Queue.Dequeue(); would need to be broken into two commands: one to remove the next item from the queue (data structure modification) and one to retrieve the result.
            – Ryan Pierce Williams
            Nov 10 at 9:39







          • 1




            You might be interesting in how Functional Programming handles your case: returning both the element and new queue/stack: fsharpforfunandprofit.com/posts/stack-based-calculator (section Popping the stack).
            – Nghia Bui
            Nov 10 at 9:46










          • Functional Programming is fun, though I've had limited opportunity to use full-on functional programming out in the field. You could certainly adhere to the pattern by creating a new stack like that - though then I'd question why you would want to take the performance hit of creating another stack every time you push/pop just to adhere to the pattern? Nice idea though :)
            – Ryan Pierce Williams
            Nov 10 at 9:53







          • 1




            If you read the F# implementation in the link, you will see that it does exactly what you proposed. Anw, in general, if you find a particular part in your program is curical with performance, then implement it with mutability style if it can boost the performance. We should not be dogmatic with immutabilty or any principle, but rather find a trade-off depending on our situation.
            – Nghia Bui
            Nov 10 at 10:02






          • 1




            "But how to keep all references to the stack the same while still adhering to the pattern? " That's an interesting and big topic which cannot be discussed in comments, you can ask another question tagged with function-programming.
            – Nghia Bui
            Nov 10 at 10:06
















          • Good answer. I'd argue that trying to apply this principle to all of your code would make it far more tedious to write and maintain, however. For instance: straight-forward methods like Queue.Dequeue(); would need to be broken into two commands: one to remove the next item from the queue (data structure modification) and one to retrieve the result.
            – Ryan Pierce Williams
            Nov 10 at 9:39







          • 1




            You might be interesting in how Functional Programming handles your case: returning both the element and new queue/stack: fsharpforfunandprofit.com/posts/stack-based-calculator (section Popping the stack).
            – Nghia Bui
            Nov 10 at 9:46










          • Functional Programming is fun, though I've had limited opportunity to use full-on functional programming out in the field. You could certainly adhere to the pattern by creating a new stack like that - though then I'd question why you would want to take the performance hit of creating another stack every time you push/pop just to adhere to the pattern? Nice idea though :)
            – Ryan Pierce Williams
            Nov 10 at 9:53







          • 1




            If you read the F# implementation in the link, you will see that it does exactly what you proposed. Anw, in general, if you find a particular part in your program is curical with performance, then implement it with mutability style if it can boost the performance. We should not be dogmatic with immutabilty or any principle, but rather find a trade-off depending on our situation.
            – Nghia Bui
            Nov 10 at 10:02






          • 1




            "But how to keep all references to the stack the same while still adhering to the pattern? " That's an interesting and big topic which cannot be discussed in comments, you can ask another question tagged with function-programming.
            – Nghia Bui
            Nov 10 at 10:06















          Good answer. I'd argue that trying to apply this principle to all of your code would make it far more tedious to write and maintain, however. For instance: straight-forward methods like Queue.Dequeue(); would need to be broken into two commands: one to remove the next item from the queue (data structure modification) and one to retrieve the result.
          – Ryan Pierce Williams
          Nov 10 at 9:39





          Good answer. I'd argue that trying to apply this principle to all of your code would make it far more tedious to write and maintain, however. For instance: straight-forward methods like Queue.Dequeue(); would need to be broken into two commands: one to remove the next item from the queue (data structure modification) and one to retrieve the result.
          – Ryan Pierce Williams
          Nov 10 at 9:39





          1




          1




          You might be interesting in how Functional Programming handles your case: returning both the element and new queue/stack: fsharpforfunandprofit.com/posts/stack-based-calculator (section Popping the stack).
          – Nghia Bui
          Nov 10 at 9:46




          You might be interesting in how Functional Programming handles your case: returning both the element and new queue/stack: fsharpforfunandprofit.com/posts/stack-based-calculator (section Popping the stack).
          – Nghia Bui
          Nov 10 at 9:46












          Functional Programming is fun, though I've had limited opportunity to use full-on functional programming out in the field. You could certainly adhere to the pattern by creating a new stack like that - though then I'd question why you would want to take the performance hit of creating another stack every time you push/pop just to adhere to the pattern? Nice idea though :)
          – Ryan Pierce Williams
          Nov 10 at 9:53





          Functional Programming is fun, though I've had limited opportunity to use full-on functional programming out in the field. You could certainly adhere to the pattern by creating a new stack like that - though then I'd question why you would want to take the performance hit of creating another stack every time you push/pop just to adhere to the pattern? Nice idea though :)
          – Ryan Pierce Williams
          Nov 10 at 9:53





          1




          1




          If you read the F# implementation in the link, you will see that it does exactly what you proposed. Anw, in general, if you find a particular part in your program is curical with performance, then implement it with mutability style if it can boost the performance. We should not be dogmatic with immutabilty or any principle, but rather find a trade-off depending on our situation.
          – Nghia Bui
          Nov 10 at 10:02




          If you read the F# implementation in the link, you will see that it does exactly what you proposed. Anw, in general, if you find a particular part in your program is curical with performance, then implement it with mutability style if it can boost the performance. We should not be dogmatic with immutabilty or any principle, but rather find a trade-off depending on our situation.
          – Nghia Bui
          Nov 10 at 10:02




          1




          1




          "But how to keep all references to the stack the same while still adhering to the pattern? " That's an interesting and big topic which cannot be discussed in comments, you can ask another question tagged with function-programming.
          – Nghia Bui
          Nov 10 at 10:06




          "But how to keep all references to the stack the same while still adhering to the pattern? " That's an interesting and big topic which cannot be discussed in comments, you can ask another question tagged with function-programming.
          – Nghia Bui
          Nov 10 at 10:06

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235806%2fdesign-a-method-returning-a-value-or-changing-some-data-but-not-both%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo