comp.lang.ada
 help / color / mirror / Atom feed
From: Georg Bauhaus <see.reply.to@maps.futureapps.de>
Subject: Re: Naming convention to identify functions with side effects
Date: Mon, 29 Sep 2008 22:48:20 +0200
Date: 2008-09-29T22:48:20+02:00	[thread overview]
Message-ID: <48e13f14$0$6610$9b4e6d93@newsspool3.arcor-online.net> (raw)
In-Reply-To: <h9civa1o39f6$.79cdhcp3sfms$.dlg@40tude.net>

Dmitry A. Kazakov wrote:
> On Mon, 29 Sep 2008 11:55:49 -0700 (PDT), Adam Beneschan wrote:

>> Several times, I've heard people say (in similar cases) that the name
>> should be something like this:
>>
>>    function Web_Page_Is_Loaded (URL : String) return Boolean;
>>
>> so that it's grammatically correct when you put it in an IF statement:
>>
>>    if Web_Page_Is_Loaded ("http://www.irvine.com") then...
>>
>> This is WRONG, WRONG, WRONG!!!  It may make your IF statement look
>> more grammatically correct, but it OBSCURES what the program is
>> doing.  The above "grammatically correct" statement looks more like a
>> check to see if the page is already loaded; a programmer reading this
>> will not be able to figure out that the function call is the routine
>> that actually does the loading.  This is much better:
>>
>>    if Load_Web_Page ("http://www.irvine.com") then...
> 
> I don't like either.

Me neither.

While a style of "packed programming" might be expected in SETL
or APL, I can't imagine this sort of cleverness to be a preference
when using a deliberately verbose language such as Ada.

A more strict recommendation is perhaps worth noting, named the
Command-query separation (coined by Betrand Meyer). For primitive
functions  of some type T (called queries, like Is_Loaded), it
means the functions should not have abstract side effects.
"Abstract side effects" are side effects that can change the return
value of public functions of type T.  (Functions are the only
means to query the state of an object of type T.)

Some issues become more obvious in a concurrent setting
perhaps,

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

   if Web_Page.Is_Loaded then...

There needs to be a lock of some sort joining the two calls.
Otherwise, the Web_Page object might have changed
between the two calls, a logic error.
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?

If concurrency features of Ada are used instead, the
naming problem is also gone:
the commands (procedures) can be verbs and the queries
(predicate functions) can be named "Is_Something" or
"Has_Something".




  reply	other threads:[~2008-09-29 20:48 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 [this message]
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
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