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
design-patterns
add a comment |
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
design-patterns
add a comment |
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
design-patterns
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
design-patterns
asked Nov 10 at 3:39
Olivier D
1089
1089
add a comment |
add a comment |
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.
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 (sectionPopping 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 withfunction-programming
.
– Nghia Bui
Nov 10 at 10:06
|
show 2 more comments
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.
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 (sectionPopping 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 withfunction-programming
.
– Nghia Bui
Nov 10 at 10:06
|
show 2 more comments
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.
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 (sectionPopping 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 withfunction-programming
.
– Nghia Bui
Nov 10 at 10:06
|
show 2 more comments
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.
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.
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 (sectionPopping 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 withfunction-programming
.
– Nghia Bui
Nov 10 at 10:06
|
show 2 more comments
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 (sectionPopping 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 withfunction-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
|
show 2 more comments
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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