    <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  :: Letters to the Editor</title>
        <link>https://members.accu.org/index.php/articles/1198</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>




<div class="xar-mod-head"><span class="xar-mod-title">Letters to the Editor + CVu Journal Vol 14, #5 - Oct 2002</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/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c184/">Journal Columns</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c186/">LettersEditor</a>
<br />

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

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

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c77/">CVu</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c112/">145</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c186-112/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c186+112/">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;Letters to the Editor</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 08 October 2002 13:15:55 +01:00 or Tue, 08 October 2002 13:15:55 +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="d0e13" id="d0e13"></a>Regarding
Changes to C Vu</h2>
</div>
<p>Under the title &quot;this-&gt;evolve()&quot;, James wrote about the scope
of C Vu. I don't agree that C Vu shall be a magazine for Python or
Java users. Firstly, if this happens, I would feel left out as a
Perl user. No, C Vu cannot cover every language. Secondly, I don't
think the world needs another forum for Python/Java/... If I want
to get in depth with Python or Java, then I would turn to existing
forums where there is enough depth and big audiences.</p>
<p>However I think it would be interesting for C Vu to include
articles on other languages with a C/C++ slant, such as how to use
C/C++ libraries from Python/Java and comparisons of languages in
various aspects. Yes I expect a few articles would favour C++ over
Java just as a Java magazine would favour Java but I don't see any
problems with this.</p>
<p>Sorry for complaining when I am not contributing to C Vu.
However I do enjoy reading C Vu even if there are non-C/C++
features like &quot;Professionalism in Programming&quot; and &quot;A Short History
of Character Sets&quot;.</p>
<p>Best regards</p>
<p>Sven Rosvall</p>
<p><tt class="email">&lt;<a href=
"mailto:Sven_Rosvall@programmingresearch.ie">Sven_Rosvall@programmingresearch.ie</a>&gt;</tt></p>
<p><span class="emphasis"><em>Thank you for taking the time to
express an opinion. I certainly don't see C Vu as being a magazine
for Python or Java users, but I do see that carrying a small amount
of content for languages other than C and C++ is appropriate. I
hope that the readership will make their opinions known and shape
the form that C Vu takes in 2003. - James</em></span></p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e32" id="d0e32"></a>Some
Pitfalls:</h2>
</div>
<p>Dear James,</p>
<p>I received the following collection of traps hidden in typos and
misconceptions from David Caabeiro <tt class="email">&lt;<a href=
"mailto:%3Cdac@globalmente.com%3E">&lt;dac@globalmente.com&gt;</a>&gt;</tt>
(one of our newer members). They are exactly the kind of thing that
I was looking for when I presented the original little puzzle in my
column. I wonder if other readers can be stimulated by this to add
a few more of their own.</p>
<pre class="programlisting">
class foo { 
public: 
static void f() {} 
}; 
void f() {} 

foo:f(); // this calls global f() 

switch(c){ 
  case 0: return 0; 
  case 1: return 1; 
  defualt: return -; // default misspelled 
}; 

string s(); // This is a declaration, not a 
            // construction with no args 

int main(){ 
  int *v = new int(10); // instead  of new int[10] 
  for (int i = 0; i &lt; 10; ++i) v[i] = i; 
} 

char names[] = {
  &quot;David&quot;, 
  &quot;John&quot;, 
  &quot;Peter&quot; // comma forgotten here! 
  &quot;Mary&quot;, 
  &quot;George&quot; 
};  
</pre>
<p>And finally the classic:</p>
<pre class="programlisting">
void foo() {} 
foo; // Forgot() 
</pre>
<p>I hate to think how many hours I have wasted with variants of
that last one. Of course good compilers at a sufficiently high
level of warnings give a diagnostic warning for many of them. That
alone should be a good reason to switch to high warning levels.
Unfortunately that often generates spurious warnings from third
party libraries resulting in us fiddling around with <tt class=
"literal">#pragmas</tt> to hide them.</p>
<p>Francis Glassborow</p>
<p><tt class="email">&lt;<a href=
"mailto:francis.glassborow@ntlworld.com">francis.glassborow@ntlworld.com</a>&gt;</tt></p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e58" id="d0e58"></a>Some
suggestions in response to &quot;A Little String Thing&quot; by Paul
Whitehead (CVu 14.4).</h2>
</div>
<p>James,</p>
<p>1. The code can be shortened by replacing the definition of
<tt class="literal">struct is_space</tt> with a call to <tt class=
"literal">ptr_fun(isspace)</tt> from the header <tt class=
"literal">&lt;functional&gt;</tt></p>
<p>2. The version of <tt class="literal">isspace</tt> Paul uses
does not come from <tt class="literal">&lt;locale&gt;</tt> but from
the C-compatibility library <tt class="literal">&lt;cctype&gt;</tt>
To use the <tt class="literal">&lt;locale&gt;</tt> version you must
pass a locale object as a second argument. This can make the
utility more flexible.</p>
<p>3. I think it's good practice to use <tt class=
"literal">const_iterator</tt> whenever you don't need an writable
iterator. It's safer.</p>
<p>4. I get suspicious when I see things like the following:</p>
<pre class="programlisting">
str = (str_start &lt; str_end) ? std::string(str_start, str_end) : &quot;&quot;;
</pre>
<p>This checks for a &quot;special case&quot;. Special cases are inelegant.
Programs are meant to model the real world, but in my experience
there are not as many special cases in the real world as
programmers think there are. Is the above really a special case? I
don't think so. The need for the test stems from scanning the whole
string backwards. Since it has already been scanned forwards, and a
starting position already found, we only need to scan backwards up
to that position.</p>
<p>So we have:</p>
<pre class="programlisting">
void rem_space(std::string&amp; str) { 
  typedef std::string::const_iterator str_it; 
  const str_it str_start =
     std::find_if(str.begin(), str.end(), std::not1(ptr_fun(isspace)); 
  const str_it str_end = std::find_if(str.rbegin(),
                            std::string::const_reverse_iterator( str_start),
                            std::not1(ptr_fun(isspace)).base(); 
  str = std::string(str_start, str_end); 
}  
</pre>
<p>Personally, I would have preferred this function to return the
result instead of changing the parameter:</p>
<pre class="programlisting">
const std::string trim(const std::string &amp;); 
</pre>
<p>Finally, it can be advantageous to templatize it so that it can
work with <tt class="literal">wchar_t</tt> as well as <tt class=
"literal">char</tt>. I leave that as an exercise.</p>
<p>Klitos Kyriacou</p>
<p><tt class="email">&lt;<a href=
"mailto:klitos@klitos.org">klitos@klitos.org</a>&gt;</tt></p>
<p><span class="emphasis"><em>Paul Whitehead has provided a
response to the message from Klitos, which I reproduce
below:</em></span></p>
<p>James,</p>
<p>I would like to reply, at least in brief, if only as a kind-of
&quot;thank you&quot; for replying to the original article. Firstly I'd like
to say thanks to Klitos Kyriacou for reading the article. Secondly,
I'd like to say thanks for taking the time and effort to reply. It
is appreciated.</p>
<p>Thirdly, I have an admission to make: I wrote the article/code
in January this year (just after the heady days of Christmas/New
Year celebrations :- ). Since then, I've been changing compilers
and platforms so often and have also had a hard-disk failure (yes,
_of course_ I backed-up the hard- drive regularly, just not the
things it turned out I needed ;- that I have to confess I've
&quot;mislaid&quot; the original code; the article too, as it happens, but
then it's been reprinted in CVu so at least I can read my own
article there! Now, I did send a copy of the code to be made
available on the ACCU website but I haven't seen it on there in any
recognisable form as yet, so I'll have to do some of this from
memory - bear with me, if you can.</p>
<p>Points 1 and 4: yes, <tt class="literal">ptr_fun</tt> is good.
Tidies things up a little. However, my own dissatisfaction with the
solution I provided is that I find the whole thing just too verbose
for what seems to be a rather trivial problem. Later on (point 4)
you talk about a &quot;special case&quot; and say that it is inelegant. I
would go further and say that special cases do not exist. They are
simply a degenerate case of a more general rule - a general rule
which just hasn't been found (or even looked for?) yet. When I do
design reviews (and in general they tend to be OO and UML) I
consistently weed out special cases as it shows, at least to my
mind, that the problem is not correctly understood and therefore
the solution is not correct. If this sounds a little extreme, then
at least you can see that I fully support your point (4). I would,
however, like to take the thought processes in your point (4) a lot
further. As I have already mentioned, I find the whole thing too
verbose and I suspect a more radical re-think of the initial
solution - along the lines of your point (4), but more of it - may
be required. Any takers amongst the C Vu readership?</p>
<p>Point 2 re: locale - yes, passing a <tt class=
"literal">locale</tt> to <tt class="literal">isspace</tt> does call
up the locale version and this, as you rightly say, will make the
utility more flexible.</p>
<p>Point 3 re: <tt class="literal">const_iterator</tt> - hmmm,
well, yes, I do tend to use <tt class="literal">const_iterator</tt>
whenever I can - and even &quot;<tt class="literal">const
container_type::const_iterator it = ...</tt>&quot; where possible. I do
this <span class="emphasis"><em>despite</em></span> other people
arguing convincingly that you may as well use the &quot;simple&quot; iterator
in pretty much most cases. Scott Meyer's Effective STL springs to
mind as having this guideline somewhere (but don't quote me, I need
to check!). So using <tt class="literal">const_iterator</tt> is
arguably not such a good thing as it may first appear. However,
being stubbornly const correct (at least I hope I am!) I just can't
bring myself to type &quot;<tt class="literal">iterator</tt>&quot; when
&quot;<tt class="literal">const_iterator</tt>&quot; would work too.</p>
<p>Back to point (4) if I may. I'm not sure about passing back a
const <span class="emphasis"><em>copy</em></span> of the string
object as it doesn't really get you much. A const ref, assuming, of
course, it isn't a ref to a local (i.e. automatic) in the function
or a const pointer (ditto) - that's fine. But a const copy? What
are you going to do with it? If it is going to be used in another
object then there's nothing stopping me doing this:</p>
<pre class="programlisting">
using std::string; 
string mySpaceStr(&quot; abc def &quot;); 
string myStr( trim(mySpaceStr) ); 
</pre>
<p>I could make <tt class="literal">mySpaceStr</tt> a const object
and it still wouldn't change matters as <tt class=
"literal">myStr</tt> would still be non-const, as I wished -
assuming I'm not just being sloppy and really did want a non-const
<tt class="literal">myStr</tt> string object. Copy constructors
(and assignment operators) in general take a const reference as
their argument so by returning a const copy of the object from the
trim function, all you are doing is passing that const object into
a copy constructor in the above case) and then creating the
non-const <tt class="literal">myStr</tt> object from it. A notable
exception to the const argument for copy constructor/assignment
operator is <tt class="literal">std::auto_ptr&lt;&gt;</tt> - and in
that case it _may_ make sense to pass back a <tt class=
"literal">const auto_ptr&lt;&gt;</tt> as a return value, but that's
a different story...</p>
<p>You mention making the function a template function. Yes, this
would increase its flexibility somewhat. I would find the whole
trim() function much tidier if it were to become a method on the
<tt class="literal">std::basic_string&lt;&gt; class</tt> (thereby
incurring the wrath of the multitude who believe <tt class=
"literal">basic_string&lt;&gt;</tt> has too many methods already!).
If you consider the following, we could have:</p>
<p><tt class="literal">1. trim(string&amp;)</tt> - which modifies
the argument, original proposal, or</p>
<p><tt class="literal">2. string trim(const string&amp;)</tt> -
which doesn't modify the argument and returns a new string object
(non-const, as per my comments above) as you suggest in your
comments, or</p>
<p>3. method on the class. Sample usage: <tt class="literal">string
mySpaceStr( &quot; abc def &quot;); mySpaceStr.trim();</tt> After this
operation, <tt class="literal">mySpaceStr</tt> now hopefully
contains <tt class="literal">&quot;abc def&quot;</tt></p>
<p>I find option 3 preferable.</p>
<p>Regards,</p>
<p>Paul Whitehead</p>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
