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


Groups > comp.programming > #5799 > unrolled thread

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

Started byVictor Porton <porton@narod.ru>
First post2015-09-04 17:15 +0300
Last post2015-09-04 20:38 +0200
Articles 6 — 2 participants

Back to article view | Back to comp.programming


Contents

  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

#5799 — ORM: when to raise an exception: on object creation or on use?

FromVictor Porton <porton@narod.ru>
Date2015-09-04 17:15 +0300
SubjectORM: when to raise an exception: on object creation or on use?
Message-ID<msc92o$peq$1@speranza.aioe.org>
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)?

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

[toc] | [next] | [standalone]


#5800

From"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Date2015-09-04 17:51 +0200
Message-ID<12w5k0sn9jcje$.fgxuw9zqjtxy$.dlg@40tude.net>
In reply to#5799
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...

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

[toc] | [prev] | [next] | [standalone]


#5801

FromVictor Porton <porton@narod.ru>
Date2015-09-04 19:22 +0300
Message-ID<mscgfu$csi$1@speranza.aioe.org>
In reply to#5800
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?

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

[toc] | [prev] | [next] | [standalone]


#5802

From"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Date2015-09-04 18:54 +0200
Message-ID<1j4uwq4hldmem.fpg8od1n2fc1.dlg@40tude.net>
In reply to#5801
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.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

[toc] | [prev] | [next] | [standalone]


#5804

FromVictor Porton <porton@narod.ru>
Date2015-09-04 21:17 +0300
Message-ID<mscn6r$vu1$1@speranza.aioe.org>
In reply to#5802
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

[toc] | [prev] | [next] | [standalone]


#5805

From"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Date2015-09-04 20:38 +0200
Message-ID<13ch56170j86m.1icy94695z21c.dlg@40tude.net>
In reply to#5804
On Fri, 04 Sep 2015 21:17:01 +0300, Victor Porton wrote:

> 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".

There is no contradiction. Yes applies to the program functionality. If an
object that refers to a row is non-functional when the row does not exist.
Thus you must check that.

> 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.

Even if the class is abstract there is a functionality it defines. In
particular its interface all implementations (descendants, members of the
class, derived types, name them as you wish) must implement. If existence
of the row is a part of that functionality shared by all descendants and
one all clients will rely on then you must check it.

Regarding referential objects there exist designs which allow illegal/null
references and those that do not. The later are preferable, but not always
possible.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

[toc] | [prev] | [standalone]


Back to top | Article view | comp.programming


csiph-web