Re: [testdrivendevelopment] "Find or Create" functions: a discussion

"Steve Gordon" <[email protected]> Sun, 24 Nov 2019 07:00:50 -0700
Newsgroups gmane.comp.programming.test-driven-development
Message-ID <CAF7qo6Vvm5Ki1L8O1HgWBVSBDymYpY1xHDHrorUBuKCAxVzYfQ@mail.gmail.com>
--000000000000c799500598181394
Content-Type: text/plain; charset="UTF-8"

Pragmatically, in the vast majority of applications much less data is
required for finding an entry than creating it.  Creating an entry with so
much missing data can create data integrity problems.

So, given that findOrCreate() should need all the data that Create() would
need (not just identifying information), I would find it cleaner to just
allow Create to return the object whether it was created or already existed
(and signal that the entry already existed if the client code cares).

On Sun, Nov 24, 2019 at 1:55 AM J. B. Rainsberger <[email protected]>
wrote:

> Hi, folks. An old issue came back to the surface this month in
> consultation with clients and I'd like your opinion. It regards the old
> "find or create" pattern. It seems to violate Command/Query Separation (as
> I understand it), but it seems handy and harmless, so I'd like to find out
> more about what you folks think about it. Benign? Problematic?
>
> I imagine using this with the Repository pattern. Let's say we register a
> patient in a medical environment and so we need a UI that reduces as much
> as possible the number of steps. We don't want to force the user to look up
> a patient just to discover that the hospital has no record of them, so we
> allow the user to enter some basic identifying information. This
> information suffices to either find an existing patient or create a new one
> if our database doesn't know that patient. The result is something like
>
> Patient registeredPatient =
> patientRepository.findOrCreate(patientIdentifyingInformation);
>
> The identifying information might have basics like name, date of birth, it
> doesn't matter. We can guarantee that registeredPatient now represents an
> Entity in our system, either because we found someone that matched the
> identifying information or because we created one.
>
> This appears to violate CQS, but it seems like a good thing to have. Some
> individuals struggle with this, because they don't know whether this is an
> area where CQS "doesn't matter" or an area where CQS is trying to teach
> them something and they can't see what they're meant to learn. I haven't
> thought about this in depth in years, so I feel the same way right now.
> Drawbacks? Alternatives?
>
> I was also thinking about how to design this, and it seems to me like a
> special case of getOrAbsent(), so that I could implement the generic
> findOrCreate() algorithm with something like
>
>
> repository.find(identifyingInformation).orElse(T::createFromIdentifyingInformation)
>
> where find() returns Maybe TIdentifyingInformation and T has a named
> constructor for creating a T from a TIdentifyingInformation. I'm assuming
> here that TIdentifyingInformation is enough to provide all the mandatory
> properties of T.
>
> With this design, I don't need a single findOrCreate() function any more,
> because the pieces find() and orElse(T::create) communicate the idea well
> enough.
>
> Thoughts? I'm happy to see the discussion meander. Is this a
> totally-solved issue and there's one clear good way to proceed? or is it
> more a matter of context or preference?
> --
> J. B. (Joe) Rainsberger :: https://tdd.training :: http://www.jbrains.ca
> :: http://www.thecodewhisperer.com
>
> 
>
>

-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.

View/Reply Online (#35744): https://groups.io/g/testdrivendevelopment/message/35744
Mute This Topic: https://groups.io/mt/61872506/3268755
Group Owner: [email protected]
Unsubscribe: https://groups.io/g/testdrivendevelopment/leave/6156322/744556386/xyzzy  [[email protected]]
-=-=-=-=-=-=-=-=-=-=-=-


--000000000000c799500598181394
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Pragmatically, in the vast majority=C2=A0of applications m=
uch less data is required for finding an entry than creating it.=C2=A0 Crea=
ting an entry with so much missing data can create data integrity problems.=
<div><br></div><div>So, given that findOrCreate() should need all the data =
that Create() would need (not just identifying information), I would find i=
t cleaner to just allow Create to return the object whether it was created =
or already existed (and signal that the entry already existed if the client=
 code cares).=C2=A0=C2=A0</div></div><br><div class=3D"gmail_quote"><div di=
r=3D"ltr" class=3D"gmail_attr">On Sun, Nov 24, 2019 at 1:55 AM J. B. Rainsb=
erger &lt;<a href=3D"mailto:[email protected]">[email protected]</a>&=
gt; wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0=
px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div =
dir=3D"ltr"><div>Hi, folks. An old issue came back to the surface this mont=
h in consultation with clients and I&#39;d like your opinion. It regards th=
e old &quot;find or create&quot; pattern. It seems to violate Command/Query=
 Separation (as I understand it), but it seems handy and harmless, so I&#39=
;d like to find out more about what you folks think about it. Benign? Probl=
ematic?</div><div><br></div><div>I imagine using this with the Repository p=
attern. Let&#39;s say we register a patient in a medical environment and so=
 we need a UI that reduces as much as possible the number of steps. We don&=
#39;t want to force the user to look up a patient just to discover that the=
 hospital has no record of them, so we allow the user to enter some basic i=
dentifying information. This information suffices to either find an existin=
g patient or create a new one if our database doesn&#39;t know that patient=
. The result is something like</div><div><br></div><div>Patient registeredP=
atient =3D patientRepository.findOrCreate(patientIdentifyingInformation);</=
div><div><br></div><div>The identifying information might have basics like =
name, date of birth, it doesn&#39;t matter. We can guarantee that registere=
dPatient now represents an Entity in our system, either because we found so=
meone that matched the identifying information or because we created one.</=
div><div><br></div><div>This appears to violate CQS, but it seems like a go=
od thing to have. Some individuals struggle with this, because they don&#39=
;t know whether this is an area where CQS &quot;doesn&#39;t matter&quot; or=
 an area where CQS is trying to teach them something and they can&#39;t see=
 what they&#39;re meant to learn. I haven&#39;t thought about this in depth=
 in years, so I feel the same way right now. Drawbacks? Alternatives?</div>=
<div><br></div><div>I was also thinking about how to design this, and it se=
ems to me like a special case of getOrAbsent(), so that I could implement t=
he generic findOrCreate() algorithm with something like</div><div><br></div=
><div>repository.find(identifyingInformation).orElse(T::createFromIdentifyi=
ngInformation)</div><div><br></div><div>where find() returns Maybe TIdentif=
yingInformation and T has a named constructor for creating a T from a TIden=
tifyingInformation. I&#39;m assuming here that TIdentifyingInformation is e=
nough to provide all the mandatory properties of T.</div><div><br></div><di=
v>With this design, I don&#39;t need a single findOrCreate() function any m=
ore, because the pieces find() and orElse(T::create) communicate the idea w=
ell enough.</div><div><br></div><div>Thoughts? I&#39;m happy to see the dis=
cussion meander. Is this a totally-solved issue and there&#39;s one clear g=
ood way to proceed? or is it more a matter of context or preference?<br>-- =
<br><div dir=3D"ltr"><div dir=3D"ltr"><div>J. B. (Joe) Rainsberger :: <a hr=
ef=3D"https://tdd.training" target=3D"_blank">https://tdd.training</a> :: <=
span><a href=3D"http://www.jbrains.ca" target=3D"_blank">http://www.jbrains=
.ca</a> :: <a href=3D"http://www.thecodewhisperer.com" target=3D"_blank">ht=
tp://www.thecodewhisperer.com</a></span></div><div><br></div></div></div></=
div></div>



<p></p><p></p></blockquote></div>

<div width=3D"1" style=3D"color:white;clear:both">_._,_._,_</div>
<hr>
Groups.io Links:<p>

You receive all messages sent to this group.


<p>

<a target=3D"_blank" href=3D"https://groups.io/g/testdrivendevelopment/mes=
sage/35744">View/Reply Online (#35744)</a> |


  <a target=3D"_blank" href=3D"mailto:[email protected]?subj=
ect=3DRe:%20Re%3A%20%5Btestdrivendevelopment%5D%20%22Find%20or%20Create%22%=
20functions%3A%20a%20discussion">Reply To Group</a>
  
    | <a target=3D"_blank" href=3D"mailto:[email protected]?subject=3DP=
rivate:%20Re:%20Re%3A%20%5Btestdrivendevelopment%5D%20%22Find%20or%20Create=
%22%20functions%3A%20a%20discussion">Reply To Sender</a>
  


|


  
    <a target=3D"_blank" href=3D"https://groups.io/mt/61872506/3268755">Mu=
te This Topic</a>
  

| <a href=3D"https://groups.io/g/testdrivendevelopment/post">New Topic</a>=
<br>



<br>

<a href=3D"https://groups.io/g/testdrivendevelopment/editsub/3268755">Your=
 Subscription</a> |
<a href=3D"mailto:[email protected]">Contact Group Own=
er</a> |

<a href=3D"https://groups.io/g/testdrivendevelopment/leave/6156322/7445563=
86/xyzzy">Unsubscribe</a>

 [[email protected]]<br>
<div width=3D"1" style=3D"color:white;clear:both">_._,_._,_</div>


--000000000000c799500598181394--