    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Intuitive Multi-Paradigm Design:</title>
        <link>https://members.accu.org/index.php/journals/483</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>


        <h2>Journal Articles</h2>


<div class="xar-mod-head"><span class="xar-mod-title">Overload Journal #38 - Jul 2000 + Programming Topics</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c78/">Overload</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c166/">38</a>
                    (6)
<br />

                                            <a href="https://members.accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c65/">Programming</a>
                    (877)
<br />

                                            <a href="https://members.accu.org/index.php/journals/c166-65/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/journals/c166+65/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Intuitive Multi-Paradigm Design:</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 26 July 2000 17:50:58 +01:00 or Wed, 26 July 2000 17:50:58 +01:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e20" id="d0e20"></a></h2>
<h3>Do I use a member function or a non-member
function?</h3>
</div>
<p><span class="emphasis"><em>This question was posed in an article
[1] by Scott Meyers. How you answer it depends on your
perspective.</em></span></p>
<p>The best approach adopts a problem based perspective: What are
you trying to achieve?</p>
<p>However, as a thought experiment, I'll take an implementation
perspective. At first, I thought we were mixing two paradigms,
Object Orientation (OO.) and Procedural (algorithms).</p>
<p>Different designers will produce different designs for the same
problem. A key factor is the paradigm mix they prefer. Rephrasing
the question makes it easier to answer:</p>
<p><span class="bold"><b>&quot;What are the design implications of
member functions and non-member functions?&quot;</b></span></p>
<p>The answer, ultimately depends more on your personal design
style mix than the original problem. Here are two extreme
examples:</p>
<p>If you are heavily into OO then you'll be using member functions
everywhere. Even your algorithms will be wrapped up in classes and
are likely to be passed around as objects. The original question
becomes &quot;which class do I make this a member function of?&quot;</p>
<p>If you're heavily into procedural programming, there'll be
plenty of highly-cohesive, weakly coupled functions manipulating a
small set of built in types. The original question never gets
asked.</p>
<p>Actually you are somewhere between those two artificial
extremes. Everyone knows the C++ language supports multiple
paradigms - procedural, modular, object-based, object-oriented and
more. And language shapes thought. So, at some intuitive level,
we're all doing multi-paradigm design and implementation. There
comes a point where it is worth looking at what we're doing more
formally. Which is a perfect excuse to introduce some formal
terminology&hellip;</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e42" id="d0e42"></a>Multi-Paradigm
Design</h2>
</div>
<p>A <span class="bold"><b>paradigm</b></span> is just a matter of
perspective - a way of grouping similar things together and noting
their differences. Some points of views are similar and overlap
(object-based, object-oriented). As far as intuition is concerned,
the differences do not matter.</p>
<p>When weaving paradigms together, I find it a helpful crutch to
have artificially rigid definitions of the paradigms I'm using. The
paradigms have to overlap in some way so they can be stitched
together. Personally I think the different paradigms of C++ can be
bundled together as families of paradigms (algorithms, user defined
types, declarative).</p>
<p>When designing something, we make a careful study of the
situation/business (domain analysis). Sometimes the business is so
broad that its best split up into smaller areas. I prefer to split
the business focus into <span class=
"bold"><b>sub-domains</b></span> (e.g. forecasting, risk-analysis)
and the technology into <span class=
"bold"><b>sub-systems</b></span> (e.g. databases, GUIs).</p>
<p>When we split something up, we group things together into
bunches (<span class="bold"><b>families</b></span>) based on what
they have in common (<span class="bold"><b>commonality
analysis</b></span>) and how the family members differ
(<span class="bold"><b>variability analysis</b></span>).</p>
<p><span class="emphasis"><em>Table 2 (next page)</em></span>
describes an informal, intuitive approach to Multi-Paradigm design.
At the end of this intuition-driven approach, there may be some
functions that could be put in classes or could be stand-alone
functions. This uncertainty shows that we will discover something
later.</p>
<p>Once the topic/domain has been split into sub-domains and
sub-systems, the choice of paradigm may be obvious to
intuition.</p>
<p>While paradigm choice is important in selecting the tools we
will use, it only brings us nearer to the answer of our question.
Some email discussion with Kevlin Henney pointed out that
&quot;questions &hellip; where responsibility for behaviour is allocated
.. the forces at play on this include extensibility, primitiveness,
dependencies, and so on&hellip;you might want to look at
&hellip;the Interface Principle&quot;.</p>
<p><span class="bold"><b>&quot;Does the function's behaviour belong
inside the class?&quot;</b></span></p>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e82" id="d0e82"></a>Applying design
principles.</h3>
</div>
<p>How about considering the Open Closed Principle (OCP)? That is,
what we implement should be open for extension and closed to
change.</p>
<p>Applying the OCP to the function, should the function extendable
(applicable to other types)? If so then we should consider making
it a template function.</p>
<p>According to the Interface Principle (IP) [4], regardless of
whether the function is implemented as a member function or a
freestanding function, the function is part of the class'
interface. I will refer to the member functions as the class method
interface.</p>
<p>So, taking the OCP and IP together, if we add a member function
to the class we change it, if we write a freestanding function that
mentions the class, we extend the class - without changing the
class definition or using virtual functions! According to the OCP,
we should provide things that are open to extension and closed to
change.</p>
<p>For the class to be closed to change, we need to provide member
functions that provide meaningful services so that non-member
functions do not have to access private data.</p>
<p>We need to look at the function and the class regarding the
principles of weak coupling and high cohesion (and consider
granularity for good measure). A coupling issue arises if the
function relies on private data. We then need to make a decision
based on cohesion to either (1) make the function a member function
or (2) write the function as a non-member function and write some
additional member functions so that the private data is no longer
an obstacle.</p>
<p>Polymorphism requirements don't affect our decision. If
polymorphism is needed, the function should either be a virtual
member function or a freestanding function that accepts a base
class pointer/reference as a parameter.</p>
<p>Remember, though, whatever choice you make, it should take the
application into account.</p>
<div class="table"><a name="d0e102" id="d0e102"></a>
<table summary=
"Design implications of member functions and non-member functions"
border="1" cellspacing="0">
&lt;colgroup&gt;
&lt;col&gt;
&lt;col&gt;&lt;/colgroup&gt;
&lt;tbody&gt;
<tr>
<td align="left"><span class="emphasis"><em>Member function
implies</em></span></td>
<td align="left"><span class="emphasis"><em>Non-member function
implies</em></span></td>
</tr>
<tr>
<td align="left" valign="top">
<div class="literallayout">
<p>Function is part of the class method interface.<br>
<br>
Function is an essential part of the class and must <br>
not be separated from it (i.e. needs access to <br>
private/protected data).<br>
<br>
(if virtual) - the function is a part of the class' <br>
runtime polymorphism.<br>
<br>
Responsibility for the function's behaviour lies with <br>
the class.<br>
<br>
Dependencies - the function prototype may require <br>
additional headers to be included by the class' user.</p>
</div>
</td>
<td align="left" valign="top">
<div class="literallayout">
<p>Function is part of the class interface.<br>
<br>
Function is an algorithm.<br>
<br>
Function prototype declares which classes/types it can <br>
be applied to.<br>
<br>
Algorithm requirements implied by the function body.<br>
<br>
(if template) - function is sufficiently generic that it <br>
can be applied to a set of types/classes which implement <br>
a set of facilities for the template function (note - <br>
these facilities are implied by the function <br>
implementation).<br>
<br>
Can avoid additional dependencies - function prototype <br>
can be stored in a different header to the class <br>
definition.</p>
</div>
</td>
</tr>
&lt;/tbody&gt;
</table>
<p class="title c3">Table 1. Design implications of member
functions and non-member functions</p>
</div>
<p>Ambiguity implies the function is one of:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>A future member function of</p>
</li>
<li>
<p>A class that does not exist yet.</p>
</li>
<li>
<p>A class we do not know enough about to decide.</p>
</li>
<li>
<p>A future algorithm that may get applied to different types one
day using</p>
</li>
<li>
<p>Runtime polymorphism (inheritance class hierarchy)</p>
</li>
<li>
<p>Compile time polymorphism (template function).</p>
</li>
</ul>
</div>
<p>Examples of mixing both:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>A template that takes base class pointers as parameters.</p>
</li>
<li>
<p>A class hierarchy in which the member functions are implemented
using different, smaller function templates.</p>
</li>
</ul>
</div>
<div class="table"><a name="d0e151" id="d0e151"></a>
<table summary="A development approach." border="1" cellspacing=
"0">
&lt;colgroup&gt;
&lt;col&gt;&lt;/colgroup&gt;
&lt;tbody&gt;
<tr>
<td align="left"><span class="emphasis"><em>Think about the
problem.</em></span>
<div class="literallayout">
<p>Break the original problem (application domain) into sub-domains
&amp; sub-systems. <br>
Identify significant processes (algorithms) on smaller post-it
notes. <br>
Identify classes - families grouped by common structure and
behaviour, family members distinguished by variability in <br>
  behaviour and/or additional structure and behaviour.<br>
Entities (classes, objects), their responsibilities, behaviour and
collaborations.<br>
Relationships, Collaborations and Interfaces between objects. <br>
Operations performed by objects (methods - member functions).<br>
Identify algorithms - families grouped together by common name
&amp; purpose (overloading and/or template functions), family <br>
  members distinguished by variability in implementation, function
signature (overloading), types (template functions).<br>
Flow of information / objects.<br>
Operations performed on objects (algorithms - non member
functions).<br>
Identify groups of objects (using database normalisation
techniques).<br>
Assign processes to CRC cards (as member functions), keep others to
one side (non-member functions).<br>
Assign algorithms to post it notes.<br>
Play with the CRC cards and other post it notes.<br>
Implement your entities as classes, aiming for a &quot;thin method
interface&quot;.<br>
Implement your algorithms as functions that manipulate objects via
their member functions.</p>
</div>
</td>
</tr>
&lt;/tbody&gt;
</table>
<p class="title c3">Table 2. A development approach.</p>
</div>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e162" id=
"d0e162"></a>Acknowledgements</h2>
</div>
<p>Thanks to Kevlin Henney and Phil Bass for their comments.</p>
<div class="bibliography">
<div class="titlepage">
<h2><a name="d0e167" id="d0e167"></a>References &amp;
Further Reading.</h2>
</div>
<div class="bibliomixed"><a name="Meyers" id="Meyers"></a>
<p class="bibliomixed">[Meyers] <span class="citetitle"><i class=
"citetitle">How Non-Member Functions Improve
Encapsulation</i></span> (Scott Meyers, via <span class=
"bibliomisc"><a href="http://www.aristeia.com" target=
"_top">www.aristeia.com</a></span>)</p>
</div>
<div class="bibliomixed"><a name="Stroustrup" id="Stroustrup"></a>
<p class="bibliomixed">[Stroustrup] <span class=
"citetitle"><i class="citetitle">The C++ Programming Language 3rd
edition</i></span>. (Bjarne Stroustrup). Chapter 2: Overview of
paradigms and Part IV: Design Using C++.</p>
</div>
<div class="bibliomixed"><a name="Coplien" id="Coplien"></a>
<p class="bibliomixed">[Coplien] <span class="citetitle"><i class=
"citetitle">Multi-Paradigm Design for C++</i></span> (James O.
Coplien). Especially chapters on Solution domain analysis,
Commonality, Variability.</p>
</div>
<div class="bibliomixed"><a name="Sutter" id="Sutter"></a>
<p class="bibliomixed">[Sutter] <span class="citetitle"><i class=
"citetitle">Exceptional C++</i></span> (Herb Sutter)</p>
</div>
<div class="bibliomixed">
<p class="bibliomixed">Combining O.O. Design and Generic
Programming ( Klaus Kreft &amp; Angelika Langer, <span class=
"citetitle"><i class="citetitle">C++ Report</i></span> March 1997,
home.camelot.de/langer/Articles/OOPvsGP/Introduction.htm)</p>
</div>
<div class="bibliomixed">
<p class="bibliomixed"><span class="citetitle"><i class=
"citetitle">C++ Primer 3e</i></span> (Lippman &amp; Lajoie)
chapters on Function templates, Overloaded functions.</p>
</div>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
