<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Rants from Vas &#187; C</title>
	<atom:link href="http://www.vastheman.com/blog/category/technolgy/development/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.vastheman.com/blog</link>
	<description>The Anthony Mundine of geeks!</description>
	<lastBuildDate>Tue, 24 Jan 2012 10:16:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>And?</title>
		<link>http://www.vastheman.com/blog/2010/02/04/and/</link>
		<comments>http://www.vastheman.com/blog/2010/02/04/and/#comments</comments>
		<pubDate>Thu, 04 Feb 2010 11:12:00 +0000</pubDate>
		<dc:creator>vastheman</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Technolgy]]></category>

		<guid isPermaLink="false">http://www.vastheman.com/blog/?p=137</guid>
		<description><![CDATA[C++ defines a bunch of aliases for operators. These are kind of cool, and they can make code more readable at times – for example you can write things like: if ((dest bitor netmask) == bcdest and protocol == udp) But in typical C++ fashion, they chose to specify it in a completely brain-dead way. [...]]]></description>
			<content:encoded><![CDATA[<p>C++ defines a bunch of aliases for operators.  These are kind of cool, and they can make code more readable at times – for example you can write things like:</p>
<pre>if ((dest bitor netmask) == bcdest and protocol == udp)</pre>
<p>But in typical C++ fashion, they chose to specify it in a completely brain-dead way.  The names don’t alias the operators they’re named for, but their actual <em>punctuation representations</em>.  That means this is valid code:</p>
<pre>Address parse(const std::string bitand repr);</pre>
<p>The ability to do this doesn’t really help anyone, except lazy compiler vendors who want to implement the aliases as predefined macros.  But it gives us all one more WTF, and another tool in our arsenal for writing obfuscated code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vastheman.com/blog/2010/02/04/and/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Generated Copy Constructors Considered Evil</title>
		<link>http://www.vastheman.com/blog/2009/04/04/constructors/</link>
		<comments>http://www.vastheman.com/blog/2009/04/04/constructors/#comments</comments>
		<pubDate>Sat, 04 Apr 2009 03:40:30 +0000</pubDate>
		<dc:creator>vastheman</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Technolgy]]></category>

		<guid isPermaLink="false">http://www.vastheman.com/blog/?p=85</guid>
		<description><![CDATA[Sometimes I really hate C++. Not just dislike it, but really, really hate it. This week, one of the most horrible language “features” got me again: the generated copy constructor. I understand why they exist — they’re necessary to allow C structures to be passed by value no extra effort. However, their behaviour causes a [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes I really hate C++.  Not just dislike it, but really, really hate it.  This week, one of the most horrible language “features” got me again: the generated copy constructor.  I understand why they exist — they’re necessary to allow C structures to be passed by value no extra effort.  However, their behaviour causes a world of pain that should never have been inflicted on developers.</p>
<p><span id="more-85"></span></p>
<p>I have a template class — let’s call it <tt>Foo</tt>.  It used to have a couple of non-trivial constructors and assignment operators:</p>
<pre>template &lt;typename T&gt;
class Foo
{
public:
    Foo(T* = 0);
    Foo(const Foo&lt;T&gt;&amp;);
    ~Foo() throw();

    Foo&lt;T&gt;&amp; operator=(T*);
    Foo&lt;T&gt;&amp; operator=(const Foo&lt;T&gt;&amp;);

    ...
};</pre>
<p>This all works fine — user-defined constructors and assignment operators are used in all cases.  But one day, I realise that I can simplify some code by making the constructors and assignment operators more general:</p>
<pre>template &lt;typename T&gt;
class Foo
{
public:
    Foo(T* = 0);
    template &lt;typename U&gt;
    Foo(const Foo&lt;U&gt;&amp;);
    ~Foo() throw();

    Foo&lt;T&gt;&amp; operator=(T*);
    template &lt;typename U&gt;
    Foo&lt;T&gt;&amp; operator=(const Foo&lt;U&gt;&amp;);

    ...
};</pre>
<p>Instead just being able to construct or assign from the same class, you should be able to construct or assign from any instantiation of the template.  But this caused things to break all over the place.  Can you see why?  The compiler will now generate a copy constructor and assignment operator.  To stop the compiler from generating them, you need not just a constructor/operator <em>general enough</em> to accept an instance of the same class, but a constructor/operator that takes an instance of <em>exactly the same</em> class.  To make it work, I need to do this:</p>
<pre>template &lt;typename T&gt;
class Foo
{
public:
    Foo(T* = 0);
    Foo(const Foo&lt;T&gt;&amp;);
    template &lt;typename U&gt;
    Foo(const Foo&lt;U&gt;&amp;);
    ~Foo() throw();

    Foo&lt;T&gt;&amp; operator=(T*);
    Foo&lt;T&gt;&amp; operator=(const Foo&lt;T&gt;&amp;);
    template &lt;typename U&gt;
    Foo&lt;T&gt;&amp; operator=(const Foo&lt;U&gt;&amp;);

    ...
};</pre>
<p>There’s another case where this can easily trip you up.  Consider this:</p>
<pre>class Fish
{
    ...
};

class Salmon : public Fish
{
public:
    Salmon(const Fish&amp;);

    ...
};</pre>
<p>Counter-intuitively, a generated copy constructor will be used to construct <tt>Salmon</tt> from other instances of <tt>Salmon</tt> derived classes; the user-defined constructor will only be used to construct <tt>Salmon</tt> from instances of <tt>Fish</tt> and other derived classes thereof.</p>
<p>The current counter-intuitive behaviour makes it too easy to end up with broken code.  The issues could have been avoided in a number of ways:</p>
<ul>
<li>No generated copy constructors/assignment operators</li>
<li>Suppress generated copy constructors/assignment operators in the presence of user-defined copy constructors/assignment that are general enough to accept an instance of the same type (or to think of it another way, give the generated copy constructor/assignment operator lower precedence than all user-defined constructors/assignment operators)
</li>
<li>Suppress generated copy constructors/assignment operators<br />
in the presence of <em>any</em> user-defined constructors/assignment operators</li>
</ul>
<p>While I’m excited about some of the new features in C++0x, I can’t help but dread that some of them will be implemented in equally brain-dead ways.  Move semantics is one that comes to mind immediately.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vastheman.com/blog/2009/04/04/constructors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>On Spaghetti</title>
		<link>http://www.vastheman.com/blog/2008/07/27/spaghetti/</link>
		<comments>http://www.vastheman.com/blog/2008/07/27/spaghetti/#comments</comments>
		<pubDate>Sun, 27 Jul 2008 13:42:35 +0000</pubDate>
		<dc:creator>vastheman</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Technolgy]]></category>

		<guid isPermaLink="false">http://www.vastheman.com/blog/?p=51</guid>
		<description><![CDATA[Most programming languages have flow control features of some kind. Yeah, I know there are some languages that lack them, for example early programmable shader languages, some macro languages, and I think some programmable calculators just run a program straight through from beginning to end. But by and large, programming languages provide ways to jump [...]]]></description>
			<content:encoded><![CDATA[<p>Most programming languages have flow control features of some kind.  Yeah, I know there are some languages that lack them, for example early programmable shader languages, some macro languages, and I think some programmable calculators just run a program straight through from beginning to end.  But by and large, programming languages provide ways to jump around within the code and write decision-making logic.</p>
<p>Fairly early on, people realised that the only things you really need for flow control are a way to make a comparison, and a way to conditionally jump to another point in the program based on the result of a comparison.  On top of these primitives, you can build flow structures that are as complex as you like.  If you look at the native machine code that computers run, you can see that this has really been taken to heart: most CPUs provide a way to store the result of a comparison and one or more conditional jump instructions.  Early programming languages like BASIC and Fortran had flow control based entirely on these primitives, too.  If you learned to program on an 8-bit personal computer, you’ll no doubt remember writing statements like “<b>IF </b><i>condition</i><b> THEN GOTO </b><i>line</i>” all the time.</p>
<p>But in 1968, this form of flow control was about to get a major setback (at least in high-level languages), because Edsger Dijkstra had written what was to become a highly influential letter entitled “A Case Against the Goto Statement”.  You probably don’t know it by this name, though, because it was published in CACM under the title “ Go To Statement Considered Harmful” (Niklaus Wirth, a CACM editor at the time, changed the title for publication).  This letter criticised the <i>goto</i> statement and the form of flow control associated with it, instead advocating structured programming.</p>
<p><span id="more-51"></span></p>
<p>Most modern high-level programming languages are designed with structured programming in mind: simple statements can be grouped into compound statements (with <b>begin</b> and <b>end</b> delimiters in Pascal, or with curly braces in C or Java), and flow control is based around these compound statements.  For example, and <b>if</b> statement can be used to conditionally skip over a compound statement.  Essentially, compound statements are the basic building block from which you build your structured code.  Poor <b>goto</b> has survived to varying degrees: it’s present but rarely used in C and Pascal, and is a reserved word with no function in Java, for example.</p>
<p>The crusade against the <b>goto</b> statement has continued unabated.  Most programming lecturers will advise against its use, or neglect to mention its existence.  Programs that make use of it are referred to as “spaghetti code” because flow control can conceivable jump from any given point to any other given point.  This can make code difficult to understand, debug or modify.  However, in spite of this, I think Dijkstra’s message is being largely ignored.</p>
<p>You see, the beauty of structured code, and part of what makes it easy to understand (and consequently easy to debug and modify), is that code blocks only have a single entry point and a single exit point – program flow enters the block at the beginning, continues through it linearly, and leaves it at the end.  All flow control statements operate on entire blocks – an <b>if</b> statement skips an entire block if the condition is not met.  The <b>goto</b> statement obviously violates this principle, as program flow can be made to jump to an arbitrary point.  And that’s why Dijkstra criticised it: because it causes program flow to deviate from the program’s structure.  However, despite the ongoing crusade against <b>goto</b>, several other flow control structures that effectively do the same thing are being encouraged.  These include loop control statements (<b>continue</b> and <b>break</b>), C-style return statements and exceptions.  Let’s have a look at each of them.</p>
<p>First up, let’s think about loops.  A loop will have some kind of condition that must be maintained in order for it to run, and a block of code that runs while the condition is maintained.  Now this block, like any other block of code in a structured program, will have a natural entry point at the top and an exit point at the bottom.  The loop itself has an exit point after the loop condition is evaluated.  But when you add a <b>continue</b> statement, you’re adding <em>another exit point</em> to the loop body.  You can no longer say that the loop body will be entered at the top and left at the bottom.  In fact, <b>continue</b> may as well just be shorthand for doing a <b>goto</b> that jumps to the end of the loop body.  A <b>break</b> statement is slightly worse: it’s like a <b>goto</b> that jumps to a point just outside the loop – it’s not only adding an additional exit point to the loop body, but adding an exit point to the loop itself!</p>
<p>C-style <b>return</b> statements are similar: they add exit points to functions (effectively a <b>goto</b> that jumps to the end of the function body).  I do realise that there isn’t really much you can do about them, though – there isn’t any other way to return a value from a function.  The best you can really do is to only ever place one <b>return</b> statement in a function, and to place it at the end of the body.  (Pascal lets you return a value by assigning to the name of the function, so there’s no excuse there.)</p>
<p>Now neither of these are really any better or worse than <b>goto</b> statements.  They’re just like shorthand <b>goto</b> statements where the destination is implied.  If you use them, fair enough – just be aware of the consequences, and think twice before you criticise <b>goto</b> again, because your code is starting to look like spaghetti, too.</p>
<p>But exceptions are the worst of all.  Exceptions are like a <b>goto</b> where you <em>don’t know the destination!</em>  Think about it: throwing an exception could jump to somewhere in the same function, or somewhere up the call stack.  You just don’t know where it will land  (The only thing they can’t do that a <b>goto</b> can is to jump backwards within a code block.)  Use of exceptions means you don’t know whether a function call will return, or jump somewhere else.  Worst of all, in C++ simply unwinding an object could cause an exception to be thrown, which will most likely lead to a memory leak.</p>
<p>To deal with exceptions properly, you need to write ugly code.  Languages like Java help you out a bit with <b>finally</b> blocks.  But you still have to remember to wrap anything that needs cleaning up in a <b>try</b> block and to place the clean-up code in the corresponding <b>finally</b> block – the language can’t make you code properly.  So now you’re code is littered with <b>try</b>…<b>finally</b> constructs.</p>
<p>Of course, the C++ way has to be the ugliest: <abbr title="Resource Allocation is Initialisation">RAII</abbr>.  If you need to clean something up, you need to make a small class with the thing that needs to be cleaned up in the constructor, and the clean-up code in the destructor, and remember to be very careful to ensure you won’t throw an exception from the destructor, because then you’re really screwed.  Now create an instance of this class and make sure it’s unwound at the point where you need the clean-up to occur.  Now your code is littered with these small classes that are only really there in case an exception is thrown.  You also can’t see the program flow properly, because it will be jumping to all these little destructors.  And you want to hope you don’t need to try stepping through it in a debugger, because that’s an absolute nightmare.</p>
<p>There’s one place that exceptions are even more evil, if that’s possible: Objective-C++.  There is no proper way to deal with exceptions in Objective-C++ because C++ frames will not be properly unwound when an Objective-C exception is thrown.  (Objective-C exceptions are generally only thrown in truly exceptional circumstances, so it isn’t such a big problem in practice, but that’s beside the point.)</p>
<p>So why don’t we bring back <b>goto</b>?  We’re doing all the things that make it harmful – we’re just kidding ourselves by refusing to call it what it is.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vastheman.com/blog/2008/07/27/spaghetti/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

