comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Naming convention to identify functions with side effects
Date: Fri, 3 Oct 2008 14:28:47 +0200
Date: 2008-10-03T14:28:48+02:00	[thread overview]
Message-ID: <ek2wg7tewjyn$.wuz3bh673et9$.dlg@40tude.net> (raw)
In-Reply-To: fd0ce4ptnhuoo78ecu4em6op8nv5hnj338@4ax.com

On Fri, 03 Oct 2008 12:51:33 +0100, Brian Drummond wrote:

> As someone relatively new to Ada, I have to ask, is there need for
> anything more sophisticated than
> 
>    Web_Page.Load ("http://www.irvine.com", Loaded);
>    if Loaded then...
> 
> which avoids side effects in functions, preserves the atomicity of the
> operation and test, but allows the test result to be used later.

Side-effects are bad in any subprograms, be them functions or procedures.
When the effect of the operation Load is solely the state of Web_Page then
you should be able to spell it as:

   Web_Page.Load ("http://www.irvine.com");
   if Web_Page.Loaded then...

> (The web page may indeed change, but "Loaded" refers to the version
> extant during the .Load call)

What is the use of this test if it does not reflect the state of Web_Page?

> Its intent and operation are quite clear, and it's a common paradigm in
> other languages.
> 
> Beyond being a little untidy, is there any reason for avoiding it? 

Yes. In this design the effect of Load is the tuple (Web_Page, Loaded). It
is considerably more difficult for the reader to understand how the effect
is distributed among the tuple members, and when and if their separate
states are consistent to the state of what this tuple is supposed to model. 

According to OO design principles we should bundle such things into one
object, Web_Page for example. Then the operation "test if true" of Boolean
Loaded would become function Loaded of Web_Page.

But even so the code

   Web_Page.Load ("http://www.irvine.com");
   if Web_Page.Loaded then...

looks suspicious to me. Web_Page must fulfill its contract regardless of
outcome of Load. When "not loaded" is a valid state of Web_Page then there
is no obvious reason why anybody would need to test for it. Say the
operation Render should then be able to render a not loaded page. Like
firefox does. Otherwise, if no page may be not loaded, then the constructor
of Web_Page is responsible to initialize a page properly and Load, if any,
should raise an exception rather than silently leaving the page in an
invalid state.

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



  reply	other threads:[~2008-10-03 12:28 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
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 [this message]
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