My main point is this: programmers sometimes forget to check return codes.
If create() returns nil, then most likely the failure will be noticed very
quickly. I know this from personal experience, as both myself and my
colleague fell into this trap.

Even if you don't agree with this argument, I find using new_record?
aesthetically displeasing. I would much rather check for nil.

It comes down to a matter of semantics. The create() method is supposed to
return a saved object. It it fails in that mission, then it should return
nil. Asking for a saved object and getting an unsaved one is just not
expected behavior.

..tiny..

On Mon, Dec 15, 2008 at 8:24 AM, eltiare <[email protected]> wrote:

>
> You are going to have to write extra code no matter what you do (for
> instance, to catch an exception if you want it raised, or to check if
> it returns nil). Rather than just doing this:
>
> if Foo.create()
> #....
> end
>
> You can do this if you really want to check for it in one line
>
> if Foo.create().new_record?
>  #....
> end
>
> new_record? is only true if the model has not been saved to the
> database yet.
>
> On Dec 13, 7:37 pm, "Tony Mann" <[email protected]> wrote:
> > Dan,
> >
> > If a coder has reason to believe that saving a model will fail, then they
> > could use new() and then save(). Thus create() would be reserved for
> those
> > times when persistence failure is not part of the normal workflow.
> >
> > In its current form, create() does not provide a meaningful shortcut over
> > new and then save, since the return value has to be manually checked. If
> > create() returned nil on failure, then subsequent calls that depend on
> the
> > return value would inevitably fail.
> >
> > That is, this is good:
> >
> >   foo = Foo.new
> >
> >   if foo.save
> >     ...
> >   else
> >   end
> >
> > And this is good:
> >
> >   bar.foo = Foo.create(...)
> >   bar.do_something
> >
> > But this is not good:
> >
> >   bar.foo = Foo.create()
> >
> >   if bar.foo.new_record?
> >     ...
> >   else
> >     ...
> >   end
> >
> > ..tony..
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"DataMapper" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/datamapper?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to