comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: Naming convention to identify functions with side effects
Date: Mon, 29 Sep 2008 11:55:49 -0700 (PDT)
Date: 2008-09-29T11:55:49-07:00	[thread overview]
Message-ID: <29ac62e1-78da-4048-ad95-8f88a29f7d31@z6g2000pre.googlegroups.com> (raw)
In-Reply-To: 5654ee5f-aa9f-4fff-87e0-45854b850f26@y38g2000hsy.googlegroups.com

On Sep 29, 11:17 am, Hibou57 (Yannick Duchêne)
<yannick_duch...@yahoo.fr> wrote:
> Hello every body out there,
>
> Do some one know a commonly used or at least a good, naming convention
> to identify functions with side effects ?

I don't.  But I'll take advantage of this to discuss one thing that I
think is a REALLY BAD IDEA.

To me, "functions with side effects" can encompass at least three
different types of functions, and perhaps the answer to your question
should be different for all three:

(1) Functions that really are (mathematically) functions, and that
will return the same value each time you call them with the same
parameters, but they may also save some state information in order to
help make the next execution run faster.

(2) Functions whose purpose is to return a value, but that can return
different values each time they're called.  An example would be a
random number generator function that returns a different value each
time it's called, or a function whose purpose is to return the "next"
word from a file, so that successive calls to the function return
successive values from the file.

(3) "Functions" whose purpose is to perform some task, and that also
return a value but only as a secondary purpose or a result indicator.
This is sometimes a little harder to do in Ada than other languages,
because functions can't have OUT or IN OUT parameters, but it's still
possible.  A common usage I've seen is to have a function that, say,
opens a file and returns a Boolean to indicate whether the operation
was successful.  I think you can make a case that you shouldn't write
this as a function; nevertheless, it's commonly done (yes, I do it
sometimes).

It's this last that is connected to one of my pet peeves.  Say you've
decided to write a routine to load a particular web page, and you've
made it a Boolean function that returns True if it's successful:

   function Load_Web_Page (URL : String) return Boolean;

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

because now the reader knows that this call is actually loading the
web page, even if it violates a rule of English grammar.

So while I don't have a good answer to your question, I *can* tell you
that if anyone says that a Boolean function should *always* have a
name that is an adjective, or a past participle, or a predicate,
rather than just a verb, don't listen to them.

Thank you for patiently sitting through my rant.

                                  -- Adam




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