comp.lang.ada
 help / color / mirror / Atom feed
* handling null pointers/records to functions
@ 2004-08-21 12:59 quasar
  2004-08-21 13:55 ` Jim Rogers
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: quasar @ 2004-08-21 12:59 UTC (permalink / raw)


Hi , I was wondering what is the best way to handle null records/pointers 
passed to functions with a return type that is generic?

for example in this snippet of code node_ptr_type is a record holding a 
single data value of type Data_Type

function Data_Part (Node_Ptr : Node_Ptr_Type ) return Data_Type is
begin
    if node_ptr /= null then
        return Node_Ptr.Data;
    else
        Put_Line("Node passed to FUNCTION DATA_PART is null");
        {(**)at this point i do not know what to do... do I terminate the 
program, do I return some form of null object}
    end if;
end Data_Part;

What is the most recommended way to handle this? Point (**) has got me 
stuck.

Thanks any help would be great
quasar 





^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: handling null pointers/records to functions
  2004-08-21 12:59 handling null pointers/records to functions quasar
@ 2004-08-21 13:55 ` Jim Rogers
  2004-08-21 17:11   ` Georg Bauhaus
  2004-08-21 19:54 ` Jeffrey Carter
  2004-08-22  4:12 ` Steve
  2 siblings, 1 reply; 5+ messages in thread
From: Jim Rogers @ 2004-08-21 13:55 UTC (permalink / raw)


"quasar" <quasar@nospam.com> wrote in news:41274733@dnews.tpgi.com.au:

> Hi , I was wondering what is the best way to handle null
> records/pointers passed to functions with a return type that is
> generic? 
> 
> for example in this snippet of code node_ptr_type is a record holding
> a single data value of type Data_Type
> 
> function Data_Part (Node_Ptr : Node_Ptr_Type ) return Data_Type is
> begin
>     if node_ptr /= null then
>         return Node_Ptr.Data;
>     else
>         Put_Line("Node passed to FUNCTION DATA_PART is null");
>         {(**)at this point i do not know what to do... do I terminate
>         the 
> program, do I return some form of null object}
>     end if;
> end Data_Part;
> 
> What is the most recommended way to handle this? Point (**) has got me
> stuck.
> 
>

First, it is good to start thinking in Ada and leave some of the C
and C++ terms for those languages. Node_Ptr_Type is an access type,
not a pointer type.

You have probably noticed that the compiler requires you to define
a return value no matter what path is taken through the conditional
expression.

What should you consider as options? It appears that this function
returns a value from a private data type. In this case, passing a
null value to the function is an error. You can handle that error
by raising an exception, which should be handled in the calling
context. The exception clearly and unavoidably identifies an 
erroneous condition to the calling context. 

If the calling context must handle the exception unless you want
this error to terminate the program. Any unhandled exception will
terminate your program.

You ask about returning some kind of null object. I suggest what 
you might consider here is defining some value of the Data member
to indicate no data. This approach avoids exception handling but
is easier to get wrong at the calling context. The calling 
context must compare all return values with the "no data" value
before proceeding. Failure to do so will propogate bad data
throughout the rest of the program.

The use of exceptions is really much more reliable in this case.

Jim Rogers




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: handling null pointers/records to functions
  2004-08-21 13:55 ` Jim Rogers
@ 2004-08-21 17:11   ` Georg Bauhaus
  0 siblings, 0 replies; 5+ messages in thread
From: Georg Bauhaus @ 2004-08-21 17:11 UTC (permalink / raw)


Jim Rogers <jimmaureenrogers@worldnet.att.net> wrote:
:>quasar <quasar@nospam.com> wrote:
 
: You ask about returning some kind of null object. I suggest what 
: you might consider here is defining some value of the Data member
: to indicate no data. This approach avoids exception handling but
: is easier to get wrong at the calling context. The calling 
: context must compare all return values with the "no data" value
: before proceeding. Failure to do so will propogate bad data
: throughout the rest of the program.

Maybe you can also add a default value parameter to the function
that is returned when the Node_Ptr_Type value is null.

function Data_Part
   (Node_Ptr : Node_Ptr_Type; default_value: Data_Type) return Data_Type is
begin
    if node_ptr /= null then
        return Node_Ptr.Data;
    else
        Put_Line("Node passed to FUNCTION DATA_PART is null");
        return default_value;
    end if;
end Data_Part;

: The use of exceptions is really much more reliable in this case.

Have you, quasar, wanted a precondition of node_ptr /= null?
In this case the precondition will not be satisfied by a null
access. Therefore raising an exception seems an adequate solution.

-- Georg



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: handling null pointers/records to functions
  2004-08-21 12:59 handling null pointers/records to functions quasar
  2004-08-21 13:55 ` Jim Rogers
@ 2004-08-21 19:54 ` Jeffrey Carter
  2004-08-22  4:12 ` Steve
  2 siblings, 0 replies; 5+ messages in thread
From: Jeffrey Carter @ 2004-08-21 19:54 UTC (permalink / raw)


quasar wrote:

> Hi , I was wondering what is the best way to handle null records/pointers 
> passed to functions with a return type that is generic?
> 
> for example in this snippet of code node_ptr_type is a record holding a 
> single data value of type Data_Type
> 
> function Data_Part (Node_Ptr : Node_Ptr_Type ) return Data_Type is
> begin
>     if node_ptr /= null then
>         return Node_Ptr.Data;
>     else
>         Put_Line("Node passed to FUNCTION DATA_PART is null");
>         {(**)at this point i do not know what to do... do I terminate the 
> program, do I return some form of null object}
>     end if;
> end Data_Part;
> 
> What is the most recommended way to handle this? Point (**) has got me 
> stuck.

It appears you are dealing with a homework assignment for a generic 
linked data structure, and Data_Type is a generic formal parameter.

Probably the requirements for the data structure indicate that it is 
invalid to attempt to obtain the data from a non-existent node. A null 
value for the parameter would be a non-existent node, so calling this 
function with null is a violation of this precondition, and raising an 
exception is appropriate.

Alternatively, you could use SPARK to prove that the precondition is 
never violated, and the body of the function could simply be

return Node_Ptr.Data;

-- 
Jeff Carter
"Son of a window-dresser."
Monty Python & the Holy Grail
12




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: handling null pointers/records to functions
  2004-08-21 12:59 handling null pointers/records to functions quasar
  2004-08-21 13:55 ` Jim Rogers
  2004-08-21 19:54 ` Jeffrey Carter
@ 2004-08-22  4:12 ` Steve
  2 siblings, 0 replies; 5+ messages in thread
From: Steve @ 2004-08-22  4:12 UTC (permalink / raw)


Of course the answer depends on the behavior you want.
Right off hand, you may find it worthwhile to look into the package
Ada.Exceptions.

With Ada.Exceptions you can associate a message with an exception occurance.
So you you might make the code read something like:

  Null_Ptr_Exception : Exception;

function Data_Part (Node_Ptr : Node_Ptr_Type ) return Data_Type is

begin
  if node_ptr = null then
    Raise_Exception( Null_Ptr_Exception'Identity,
                               "Node passed to FUNCTION DATA_PART is
null" );
  end if;
  return Node_Ptr.Data;
end Data_Part;

Whatever you decide to do, you should document the behavior in the package
spec.

Steve
(The Duck)

"quasar" <quasar@nospam.com> wrote in message
news:41274733@dnews.tpgi.com.au...
> Hi , I was wondering what is the best way to handle null records/pointers
> passed to functions with a return type that is generic?
>
> for example in this snippet of code node_ptr_type is a record holding a
> single data value of type Data_Type
>
> function Data_Part (Node_Ptr : Node_Ptr_Type ) return Data_Type is
> begin
>     if node_ptr /= null then
>         return Node_Ptr.Data;
>     else
>         Put_Line("Node passed to FUNCTION DATA_PART is null");
>         {(**)at this point i do not know what to do... do I terminate the
> program, do I return some form of null object}
>     end if;
> end Data_Part;
>
> What is the most recommended way to handle this? Point (**) has got me
> stuck.
>
> Thanks any help would be great
> quasar
>
>





^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2004-08-22  4:12 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-08-21 12:59 handling null pointers/records to functions quasar
2004-08-21 13:55 ` Jim Rogers
2004-08-21 17:11   ` Georg Bauhaus
2004-08-21 19:54 ` Jeffrey Carter
2004-08-22  4:12 ` Steve

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox