comp.lang.ada
 help / color / mirror / Atom feed
From: Georg Bauhaus <rm.dash-bauhaus@futureapps.de>
Subject: Re: Naming convention to identify functions with side effects
Date: Tue, 30 Sep 2008 17:17:27 +0200
Date: 2008-09-30T17:17:27+02:00	[thread overview]
Message-ID: <48e24307$0$6583$9b4e6d93@newsspool4.arcor-online.net> (raw)
In-Reply-To: <2ed64106-52ef-4909-a0f0-ebb277856e8d@26g2000hsk.googlegroups.com>

Adam Beneschan schrieb:
> On Sep 29, 1:48 pm, Georg Bauhaus <see.reply...@maps.futureapps.de>
> wrote:
> 
>> Again, it might be tempting to pack the two calls in just
>> one in order to hide the locking mechanism, to give the
>> illusion of atomicity. But shouldn't normal Ada
>> concurrency features be employed instead?
> 
> This is a bit too esoteric for me.  I think a much simpler reason for
> the temptation to pack the two calls into just one is that it makes it
> easier to stick the call into more complex Boolean expressions, e.g.
> (assuming URL_Name's type is access string):
> 
>    if URL_Name /= null and then
>       Load_Web_Page (URL_Name.all) then ...
>       [stuff to do if we succeed]
>    else ...
>       [other stuff]
>    end if;

This solution looks like a straight forward one at least in
case of a single additional test in the conditional.
However, as soon as the URL needs a little more inspection,
I need more predicates. Since these are called before Load_Web_Page
is called, Load_Web_Page is "pushed down" the conditional chain,
hiding it and also blurring the distinct sources of error:

   if URL_Name /= null and then
      Valid_Protocol (URL_Name.all) and then
      Host_Allowed (URL_Name.all) and then
      No_Up_Directories (URL_Name.all) and then
      Load_Web_Page (URL_Name.all)
   then ...
      [stuff to do if all test succeed and the page was finally loaded]
   else ...
      [something has failed, maybe loading has failed, do other stuff]
   end if;

So, first, Load_Web_Page, which presumably has the important side
effect of initializing some variable, looses the focus
of attention.

Then, sources of error may need to be distinguished since
the errors are different in character (null pointer, syntax,
and I/O). Load_Web_Page (I/O), being appended to the list
of predicates and the null test, becomes one of many
sources of potential failure in the same conditional.

Finally, the programmer wanting to reduce verbosity by
making Load_Web_Page a Boolean function now causes
verbosity by chaining the many predicates.
This amount by far exceeds the intended reduction in verbosity
and renders the original intent (of saving words) mostly
fruitless.

So the distinct issues become blurred in this single
conditional and the Boolean function Load_Web_Page does
not significantly reduce verbosity.

At this point I remember advice from The Little Schemer
to use little helper functions (that can be nested if
needed). Thus,

   if Is_Acceptable (URL_Name.all) then
      Load_Web_Page (URL_Name.all, Data);
      Construct (Data, Result);
   else
      raise Syntax_Error;
   end if;

(where URL_Name is of subtype "not null String_Ptr")
Stupid obedience to command-query separation like in
Is_Acceptable and Load_Web_Page might pay off in the end.
Not that anyone cares.





  reply	other threads:[~2008-09-30 15:17 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-09-29 18:17 Naming convention to identify functions with side effects Hibou57 (Yannick Duchêne)
2008-09-29 18:45 ` Hibou57 (Yannick Duchêne)
2008-09-29 18:55 ` Adam Beneschan
2008-09-29 19:52   ` Dmitry A. Kazakov
2008-09-29 20:48     ` Georg Bauhaus
2008-09-29 21:10       ` Adam Beneschan
2008-09-30 15:17         ` Georg Bauhaus [this message]
2008-09-30 17:40           ` Ray Blaak
2008-10-02 12:33       ` Ole-Hjalmar Kristensen
2008-10-03  9:18         ` Georg Bauhaus
2008-10-06  7:24           ` Ole-Hjalmar Kristensen
2008-10-03 11:51       ` Brian Drummond
2008-10-03 12:28         ` Dmitry A. Kazakov
2008-10-04 12:30           ` Marco
2008-10-04 13:05             ` (see below)
2008-10-04 15:14               ` Gary Scott
2008-10-11 11:32               ` Marco
2008-10-11 14:57                 ` Dmitry A. Kazakov
2008-10-11 16:05                 ` (see below)
2008-10-11 17:49                   ` Georg Bauhaus
2008-10-11 18:42                     ` (see below)
2008-10-03 13:15         ` Jeffrey Creem
2008-09-29 20:57 ` Georg Bauhaus
2008-09-29 22:22 ` Dr. Adrian Wrigley
replies disabled

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