Topic: pure and clean
Author: sj@aracnet.com (Scott Johnson)
Date: 1996/08/27 Raw View
In another thread, someone mentioned the ideas of declaring "pure" and
"clean" functions--where a pure function is one which depends only on its
parameters, a clean function is one with no side-effects other than
modification of its parameters (or return value.) (Functions could be
both pure and clean, or one, or neither.)
Ignoring pure functions, let's examine the concept of
cleanliness--I can think of two reasonable definitions. The first, and
less stringent, is that any side effects a function has shall be expressed
through its parameter list. To satisfy this requirement, a clean function
need only have the following requirements (I think.)
No writes to any static class members, no writes to any static local
variables, no writes to any global variables (ewww!), no writes to
anything pointed to by any of the above, and no calls to any
non-clean functions. Clean nonmember functions (and clean static member
functions) may only modify their parameters and return value; clean
nonstatic member functions may also modify the nonstatic class members.
In this notion of cleanliness, it matters not if pointers or references to
the global scope, the heap, etc. are passed in--as long as they are passed
in as parameters (or as nonstatic class data members), all is well.
A more stringent notion of cleanliness, one which is useful if parallel
code is going to be written, requires further restrictions be placed on
the parameters (and nonstatic data members, in the case of nonstatic
member functions). In a BRIEF nutshell, no pointer or reference which
points to any static or heap data shall be passed to such a function
(unless it is const.) In other words, the compiler should attempt to
enforce the restriction that global items be left alone, including the
case of pointers or references to global items being passed as parameters
(or as members of parameters, or members of members of parameters, ad
naseum....). Or sumthin like that.
Which level of cleanliness is meant when a "clean" function is discussed?
And has anybody produced a formal specification for the restrictions which
must be placed on a clean function to support the notion of cleanliness?
(And purity, for pure functions.)
Geez, I love dabbling in discussions on language extensions... :)
Scott
--
/--------------------------------------------------------------------------\
|Scott Johnson -- Professional (sometimes) SW Engineer and all-purpose Geek|
|I don't speak for nobody but myself, which everyone else is thankful for |
\--------------------------------------------------------------------------/
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]