Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming > #5804

Re: ORM: when to raise an exception: on object creation or on use?

From Victor Porton <porton@narod.ru>
Newsgroups comp.programming
Subject Re: ORM: when to raise an exception: on object creation or on use?
Date 2015-09-04 21:17 +0300
Organization Aioe.org NNTP Server
Message-ID <mscn6r$vu1$1@speranza.aioe.org> (permalink)
References <msc92o$peq$1@speranza.aioe.org> <12w5k0sn9jcje$.fgxuw9zqjtxy$.dlg@40tude.net> <mscgfu$csi$1@speranza.aioe.org> <1j4uwq4hldmem.fpg8od1n2fc1.dlg@40tude.net>

Show all headers | View raw


Dmitry A. Kazakov wrote:

> On Fri, 04 Sep 2015 19:22:23 +0300, Victor Porton wrote:
> 
>> Dmitry A. Kazakov wrote:
>> 
>>> On Fri, 04 Sep 2015 17:15:53 +0300, Victor Porton wrote:
>>> 
>>>> Should an ORM implementation query the DB as soon as an active record
>>>> is created in order to check that the queried row exists in DB, or is
>>>> it OK to throw an exception later when attempting to access a field
>>>> (for an nonexistent row)?
>>> 
>>> The rule is simple, only valid objects shall exist. If an object is to
>>> reference only an existing row, then you cannot create one that does not
>>> do that.
>>> 
>>> Another SW design rule is that an error has to be detected as early as
>>> possible. If an invalid reference is an error then...
>> 
>> Got your answer.
>> 
>> I am going to follow your advice, taking into account however that this
>> way an otherwise unnecessary query to the DB is done, what makes the code
>> less efficient in terms of speed and server load.
>> 
>> Dmitry, do you confirm that the exception should be thrown as soon as
>> possible, even if this requires querying of otherwise unnecessary DB
>> fields?
> 
> No, you need not check for consistency of the DB, unless it must be
> consistent before you do anything. There is an infinite number of things
> one could check but never does. The rule of early detection applies only
> to the program logic.
> 
> There is an old saying, never test for bugs you are not going to fix. It
> sounds cynical but it is true. If your program can do its job without
> checking then checking is wasting time and, moreover, a bug, since this
> may prevent your program from working.

It looks like for me that you contradict your self: You first answered my 
question "no" then "yes".

What is your final advice for me? (no (there is need to check row existence 
on object creation) or yes (there is no need to check row existence on 
object creation)

Don't ask for a particular situation. It is yet not quite known, I am just 
writing some abstract foundational classes for our company, which may be 
used in different situations.

-- 
Victor Porton - http://portonvictor.org

Back to comp.programming | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

ORM: when to raise an exception: on object creation or on use? Victor Porton <porton@narod.ru> - 2015-09-04 17:15 +0300
  Re: ORM: when to raise an exception: on object creation or on use? "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> - 2015-09-04 17:51 +0200
    Re: ORM: when to raise an exception: on object creation or on use? Victor Porton <porton@narod.ru> - 2015-09-04 19:22 +0300
      Re: ORM: when to raise an exception: on object creation or on use? "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> - 2015-09-04 18:54 +0200
        Re: ORM: when to raise an exception: on object creation or on use? Victor Porton <porton@narod.ru> - 2015-09-04 21:17 +0300
          Re: ORM: when to raise an exception: on object creation or on use? "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> - 2015-09-04 20:38 +0200

csiph-web