Skip to first unread message

Lasse Bergström

unread,
Jan 10, 2015, 7:10:07 AM1/10/15
to clean-code...@googlegroups.com
In the Java Case Study a Gateway is implemented. In that gateway there is a save function. To me save sounds like a command function and not a query one. 

How is it then that it was chosen to return the object it was saved? I can see one reason. In order to connect other things to the newly created and saved entity we need some kind of id. That id is established at the moment when the save function is executed. I see need for that.

It still bothers me that the save function does not feel like a query function. Still it returns something.

Is this an exception the command-query pattern?

Best regards /Lasse

Julien

unread,
Jan 10, 2015, 11:12:10 AM1/10/15
to clean-code...@googlegroups.com
I guess that it is because there is no other way of doing it.
Furthermore, the Principle of Least Astonishment and the SRP are respected.

Temporary coupling like this feels wrong :
gateway.save(...);
id
= gateway.get_id_of_last_registered_object();

Lasse Bergström

unread,
Jan 10, 2015, 4:45:49 PM1/10/15
to clean-code...@googlegroups.com
So it is an exception?

It is not that I am saying that there cannot ever be any exceptions. Perhaps that is one part of being a pragrammatic programmer. To allow yourself to break some patterns.

Do you agree that "save()" feels like a command function?

Could you elaborate on "Furthermore, the Principle of Least Astonishment and the SRP are respected."?

--
The only way to go fast is to go well.
---
You received this message because you are subscribed to the Google Groups "Clean Code Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clean-code-discu...@googlegroups.com.
To post to this group, send email to clean-code...@googlegroups.com.
Visit this group at http://groups.google.com/group/clean-code-discussion.



--
Lasse Bergström
http://nextit.se

Sebastian Gozin

unread,
Jan 12, 2015, 4:32:13 AM1/12/15
to clean-code...@googlegroups.com
Is a command allowed to generate an execution report? In this case one which includes the generated id? And instead of a report we have the persisted instance?


On Saturday, January 10, 2015 at 10:45:49 PM UTC+1, Lasse Bergström wrote:
So it is an exception?

It is not that I am saying that there cannot ever be any exceptions. Perhaps that is one part of being a pragrammatic programmer. To allow yourself to break some patterns.

Do you agree that "save()" feels like a command function?

Could you elaborate on "Furthermore, the Principle of Least Astonishment and the SRP are respected."?
On Sat, Jan 10, 2015 at 5:12 PM, Julien <jh.w3...@gmail.com> wrote:
I guess that it is because there is no other way of doing it.
Furthermore, the Principle of Least Astonishment and the SRP are respected.

Temporary coupling like this feels wrong :
gateway.save(...);
id
= gateway.get_id_of_last_registered_object();

--
The only way to go fast is to go well.
---
You received this message because you are subscribed to the Google Groups "Clean Code Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clean-code-discussion+unsub...@googlegroups.com.

To post to this group, send email to clean-code...@googlegroups.com.
Visit this group at http://groups.google.com/group/clean-code-discussion.

Colin Williams

unread,
Jan 12, 2015, 10:16:40 AM1/12/15
to clean-code...@googlegroups.com
It does seem natural to return something related to the saved object, especially on creation.  

Actually, if you think about "new" in most languages, it violates command query separation in much the same way (constructing the object and returning it).  This is allowed in Command Query Separation because it's considered initialization.  Maybe, instead of thinking of save as a command, we think of it as initialization in some form of persistence.

Lasse Bergström

unread,
Jan 12, 2015, 10:53:06 AM1/12/15
to clean-code...@googlegroups.com
One way of addressing the issue could be to use the natural id instead of a technical id, e.g. database id.

For example when storing a person/user to use username, which should be unique, or social security number. Some natural id which the user/client is providing.
Then when adding things to the entity the client need not to get hold of the technical id but could use the natural id instead.

--
The only way to go fast is to go well.
---
You received this message because you are subscribed to the Google Groups "Clean Code Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clean-code-discu...@googlegroups.com.

To post to this group, send email to clean-code...@googlegroups.com.
Visit this group at http://groups.google.com/group/clean-code-discussion.

Sebastian Gozin

unread,
Jan 12, 2015, 11:53:28 AM1/12/15
to clean-code...@googlegroups.com
Could it be we are really talking about transactions?

Julien pointed out he does not like temporal coupling but it could be acceptable within a transaction as this supposedly removes the temporal coupling despite making several function calls.
Colin's initialization example appears to assume the save command is the transaction.
To unsubscribe from this group and stop receiving emails from it, send an email to clean-code-discussion+unsub...@googlegroups.com.

To post to this group, send email to clean-code...@googlegroups.com.
Visit this group at http://groups.google.com/group/clean-code-discussion.

Caio Fernando Bertoldi Paes de Andrade

unread,
Jan 13, 2015, 12:49:16 PM1/13/15
to clean-code...@googlegroups.com
Exactly, chaining commands like that is the functional style, which avoids explicit transactions, side effects and temporal couplings.
UB and Micah chose that way because they are used to the functional paradigm in Clojure, as they have pointed out in the videos.

Caio

To unsubscribe from this group and stop receiving emails from it, send an email to clean-code-discu...@googlegroups.com.

Dirk Dreyer-Hochstein

unread,
Jan 14, 2015, 7:58:54 AM1/14/15
to clean-code...@googlegroups.com
Hi,

another option for the ID would be to "calculate" it before the save. The book "Implementing Domain-Driven-Design" shows it and its really a nice idea because you can save an object and use the ID already for other connected objects if needed.
To unsubscribe from this group and stop receiving emails from it, send an email to clean-code-discussion+unsub...@googlegroups.com.

To post to this group, send email to clean-code...@googlegroups.com.
Visit this group at http://groups.google.com/group/clean-code-discussion.

Lasse Bergström

unread,
Jan 14, 2015, 8:15:20 AM1/14/15
to clean-code...@googlegroups.com
Well if it is a technical key, would you really generate that in the useCase class?

To unsubscribe from this group and stop receiving emails from it, send an email to clean-code-discu...@googlegroups.com.

To post to this group, send email to clean-code...@googlegroups.com.
Visit this group at http://groups.google.com/group/clean-code-discussion.

Dirk Dreyer-Hochstein

unread,
Jan 14, 2015, 8:20:57 AM1/14/15
to clean-code...@googlegroups.com
Usually not but why shouldn't you use the Repository for generating the next id or an extra service if it is needed?
In his book Vaughn Vernon shows that he often use the repository for this responsibility like:

BookId bookId = bookRepository.nextIdentity();

But that's all a question of responsibility I think. 

Am Mittwoch, 14. Januar 2015 14:15:20 UTC+1 schrieb Lasse Bergström:
Well if it is a technical key, would you really generate that in the useCase class?
On Wed, Jan 14, 2015 at 1:58 PM, Dirk Dreyer-Hochstein <pro...@t-online.de> wrote:
Hi,

another option for the ID would be to "calculate" it before the save. The book "Implementing Domain-Driven-Design" shows it and its really a nice idea because you can save an object and use the ID already for other connected objects if needed.

Am Montag, 12. Januar 2015 16:53:06 UTC+1 schrieb Lasse Bergström:
One way of addressing the issue could be to use the natural id instead of a technical id, e.g. database id.

For example when storing a person/user to use username, which should be unique, or social security number. Some natural id which the user/client is providing.
Then when adding things to the entity the client need not to get hold of the technical id but could use the natural id instead.
On Mon, Jan 12, 2015 at 4:16 PM, Colin Williams <lac...@gmail.com> wrote:
It does seem natural to return something related to the saved object, especially on creation.  

Actually, if you think about "new" in most languages, it violates command query separation in much the same way (constructing the object and returning it).  This is allowed in Command Query Separation because it's considered initialization.  Maybe, instead of thinking of save as a command, we think of it as initialization in some form of persistence.


On Saturday, January 10, 2015 at 7:10:07 AM UTC-5, Lasse Bergström wrote:
In the Java Case Study a Gateway is implemented. In that gateway there is a save function. To me save sounds like a command function and not a query one. 

How is it then that it was chosen to return the object it was saved? I can see one reason. In order to connect other things to the newly created and saved entity we need some kind of id. That id is established at the moment when the save function is executed. I see need for that.

It still bothers me that the save function does not feel like a query function. Still it returns something.

Is this an exception the command-query pattern?

Best regards /Lasse

--
The only way to go fast is to go well.
---
You received this message because you are subscribed to the Google Groups "Clean Code Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clean-code-discussion+unsubscri...@googlegroups.com.
To post to this group, send email to clean-code...@googlegroups.com.
Visit this group at http://groups.google.com/group/clean-code-discussion.



--
Lasse Bergström
http://nextit.se

--
The only way to go fast is to go well.
---
You received this message because you are subscribed to the Google Groups "Clean Code Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clean-code-discussion+unsub...@googlegroups.com.
To post to this group, send email to clean-code...@googlegroups.com.
Visit this group at http://groups.google.com/group/clean-code-discussion.
Reply all
Reply to author
Forward
0 new messages