<?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>Vincent Gable's Blog &#187; C++</title>
	<atom:link href="http://vgable.com/blog/category/programming/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://vgable.com/blog</link>
	<description>my weblog.</description>
	<lastBuildDate>Tue, 29 Nov 2011 22:20:23 +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>Never Name a Variable &#8220;Index&#8221;</title>
		<link>http://vgable.com/blog/2010/05/24/never-name-a-variable-index/</link>
		<comments>http://vgable.com/blog/2010/05/24/never-name-a-variable-index/#comments</comments>
		<pubDate>Mon, 24 May 2010 05:31:42 +0000</pubDate>
		<dc:creator>Vincent Gable</dc:creator>
				<category><![CDATA[Bug Bite]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Names]]></category>

		<guid isPermaLink="false">http://vgable.com/blog/?p=603</guid>
		<description><![CDATA[Never name a variable index, especially in C. Instead say what it indexes. For example, if it is used to index an array of Foo objects, call it fooArrayIndex, or currentFooIndex. If the index variable is just used to enumerate over a collection of objects, (eg. for(int i = 0; i &#038;lt arraySize; i++){…} ) [...]]]></description>
			<content:encoded><![CDATA[<p><strong><em>Never</em> name a variable <code>index</code></strong>, especially in C.</p>
<p>Instead <strong>say <em>what</em> it indexes</strong>. For example, if it is used to index an array of <code>Foo</code> objects, call it <code>fooArrayIndex</code>, or <code>currentFooIndex</code>.</p>
<p>If the index variable is just used to enumerate over a collection of objects, (eg. <code>for(int i = 0; i &#038;lt arraySize; i++){…} </code>) then <a href="http://vgable.com/blog/2008/04/07/foreach-for-the-win/">iterate smarter</a>, using a simpler construct that doesn&#8217;t require declaring auxiliary variables. (Eg., in Objective-C use <a href="http://developer.apple.com/mac/library/documentation/cocoa/conceptual/objectivec/articles/ocfastenumeration.html">Fast Enumeration</a>). It&#8217;s not always possible to do this, but it&#8217;s always a good idea to try.<sup>1</sup></p>
<h3>Why <code>index</code> is Especially Bad in C</h3>
<p>The standard <code>strings.h</code> header declares a function named <code>index</code>, that finds the first occurrence of a charicter in a C-string. In practical terms <strong>every C program will have the <code>index</code> function declared everywhere</strong>.</p>
<p>But when a variable is declared with the name <code>index</code> it <a href="http://leepoint.net/notes-java/data/variables/60shadow-variables.html">shadows</a> the function &#8211; meaning the local variable named <code>index</code> takes over the name <code>index</code>, so the function can&#8217;t be called anymore:</p>
<pre>
char * world = index("Hello, World", 'W');
NSLog(@"'%s'", world);
</pre>
<p>Prints &#8220;&#8216;World&#8217;&#8221;, but</p>
<pre>
int index = 0;
char * world = index("Hello, World", 'W');
NSLog(@"'%s'", world);
</pre>
<p>Won&#8217;t compile, because an <code>int</code> isn&#8217;t a function.</p>
<p>Obviously this is a problem for code that uses the <code>index()</code> function — but honestly modern code probably uses a safer, <a href="http://www.mikeash.com/pyblog/friday-qa-2010-02-19-character-encodings.html">unicode-aware</a> string parsing function instead. What&#8217;s given me the most trouble is that <strong>shadowing <code>index</code> makes the compiler give lots of bogus warnings, if you have the useful <code>GCC_WARN_SHADOW</code> <a href="http://vgable.com/blog/2009/12/09/compile-safer/">warning turned on</a></strong>.</p>
<p>There are other good reasons as, specific to Objective-C, which <a href="http://boredzo.org/blog/archives/2009-11-05/the-peril-of-the-index-function">Peter Hosey covers</a>.</p>
<p><sup>1</sup><small>If you <em>really</em> can&#8217;t think of a better name than &#8220;index&#8221;, I prefer the more terse <code>i</code>. It sucks, but at least it&#8217;s shorter. Brevity is a virtue.</small></p>
]]></content:encoded>
			<wfw:commentRss>http://vgable.com/blog/2010/05/24/never-name-a-variable-index/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A C &amp;Puzzler[]</title>
		<link>http://vgable.com/blog/2009/12/25/a-c-puzzler/</link>
		<comments>http://vgable.com/blog/2009/12/25/a-c-puzzler/#comments</comments>
		<pubDate>Fri, 25 Dec 2009 20:35:54 +0000</pubDate>
		<dc:creator>Vincent Gable</dc:creator>
				<category><![CDATA[Announcement]]></category>
		<category><![CDATA[Bug Bite]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Arrays]]></category>
		<category><![CDATA[GCC]]></category>
		<category><![CDATA[Pointers]]></category>

		<guid isPermaLink="false">http://vgable.com/blog/?p=547</guid>
		<description><![CDATA[Here&#8217;s a C-puzzler for you! given this function, void foo(char* s){ printf("s is at: %p\n s is: '%s'\n", s, s); } and that char s[] = "Joy!"; foo(s); prints out s is at: 0xbffff46b s is: &#8216;Joy!&#8217; what will this next line print? foo(&#038;s); //WHAT WILL THIS DO? Pick all that apply: Print &#8220;Joy!&#8221; Print [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a C-puzzler for you!</p>
<p>given this function,</p>
<pre>void foo(char* s){
	printf("s is at: %p\n s is: '%s'\n", s, s);
}</pre>
<p>and that</p>
<pre>
char s[] = "Joy!";
foo(s);
</pre>
<p>prints out</p>
<blockquote><p>
s is at: <code>0xbffff46b</code><br />
s is: &#8216;Joy!&#8217;
</p></blockquote>
<p>what will this next line print?</p>
<pre><strong>foo(&#038;s); //WHAT WILL THIS DO?</strong>
</pre>
<p>Pick all that apply:</p>
<ol>
<li>Print &#8220;Joy!&#8221;</li>
<li>Print garbage</li>
<li>Print the same address for <code>s</code></li>
<li>Print the a different address for <code>s</code></li>
<li>Crash</li>
<li>Go into an Infinite loop</li>
</ol>
<h3>Answer</h3>
<p><small>Answer: one and three</small></p>
<p>Yeah, it&#8217;s not what I expected either, especially since:</p>
<pre>@encode(__typeof__(s)) = [5c]
@encode(__typeof__(&#038;s)) = ^[5c]</pre>
<p>In fact, all of these are equvalent (modulo type warnings):</p>
<pre>
foo(s);
foo(&#038;s[0]);
foo(&#038;(*s));
foo(&#038;s);
</pre>
<p><a href="http://www.reddit.com/r/programming/comments/i6k5/jeez_after_all_these_years_a_c_array_vs_pointer">Explanation</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://vgable.com/blog/2009/12/25/a-c-puzzler/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Compile Safer</title>
		<link>http://vgable.com/blog/2009/12/09/compile-safer/</link>
		<comments>http://vgable.com/blog/2009/12/09/compile-safer/#comments</comments>
		<pubDate>Wed, 09 Dec 2009 11:13:18 +0000</pubDate>
		<dc:creator>Vincent Gable</dc:creator>
				<category><![CDATA[Bug Bite]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[MacOSX]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Compilers]]></category>
		<category><![CDATA[GCC]]></category>
		<category><![CDATA[Warnings]]></category>
		<category><![CDATA[Xcode]]></category>

		<guid isPermaLink="false">http://vgable.com/blog/?p=526</guid>
		<description><![CDATA[Peter Hosey explains what warnings he uses and why. It&#8217;s good, but long. Fortunately, you can just grab a script, and enable those warnings in your Xcode projects. Warnings = Errors If I could force just one compiler flag on everyone who&#8217;s code I use, it would be TREAT_WARNINGS_AS_ERRORS. As a rule, things don&#8217;t get [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://boredzo.org/blog/archives/2009-11-07/warnings">Peter Hosey explains what warnings he uses and why</a>. It&#8217;s good, but long. Fortunately, you can <a href="http://rentzsch.tumblr.com/post/237349423/hoseyifyxcodewarnings-scpt">just grab a script</a>, and enable those warnings in your Xcode projects.</p>
<h3>Warnings = Errors</h3>
<p>If I could force just one compiler flag on everyone who&#8217;s code I use, it would be <code>TREAT_WARNINGS_AS_ERRORS</code>. As a rule, things don&#8217;t get improved if they aren&#8217;t broken. (How many times have you said &#8220;I&#8217;ll come back and fix this code later&#8221;? Yeah.)  Warnings fester and grow on each other, until they cause a real breakage. It&#8217;s an inescapable evil of building software with finite resources.</p>
<p>If a warning isn&#8217;t worth stopping the build over &#8212; it&#8217;s not worth checking for in the first place.</p>
<h3>Use the Latest Tools</h3>
<p>Specifically, if you aren&#8217;t using <a href="http://www.amazon.com/gp/product/B001AMHWP8?ie=UTF8&#038;tag=vincgabl-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=B001AMHWP8">Snow Leopard</a> and Xcode 3.2 to build your Objective-C code, <strong>you are crazy</strong>. Trust me, <a href="http://developer.apple.com/mac/library/featuredarticles/StaticAnalysis/index.html">painless static analysis</a> is worth upgrading for. It catches maddening memory leaks, not just trivial type errors, like adding an <code>int</code> to an <code>NSArray</code>, that you would catch immediately.</p>
]]></content:encoded>
			<wfw:commentRss>http://vgable.com/blog/2009/12/09/compile-safer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript Nailed &#124;&#124;</title>
		<link>http://vgable.com/blog/2009/10/20/javascript-nailed/</link>
		<comments>http://vgable.com/blog/2009/10/20/javascript-nailed/#comments</comments>
		<pubDate>Tue, 20 Oct 2009 19:59:30 +0000</pubDate>
		<dc:creator>Vincent Gable</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Usability]]></category>
		<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Look At My Stupid Factorial Function]]></category>
		<category><![CDATA[Programming Language Design]]></category>

		<guid isPermaLink="false">http://vgable.com/blog/?p=468</guid>
		<description><![CDATA[One thing about JavaScript I really like is that its &#124;&#124;, the Logical Or operator, is really a more general &#8216;Eval Until True&#8216; operation. (If you have a better name for this operation, please leave a comment!) It&#8217;s the same kind of or operator used in Lisp. And I believe it&#8217;s the best choice for [...]]]></description>
			<content:encoded><![CDATA[<p>One thing about JavaScript I really like is that its <code>||</code>, the <code>Logical Or</code> operator, is really a more general &#8216;<code>Eval Until True</code>&#8216; operation. (<strong>If you have a better name for this operation, please leave a comment!)</strong> It&#8217;s the same kind of <code>or</code> operator <a href="http://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node75.html">used in Lisp</a>. And I believe it&#8217;s the best choice for a language to use.</p>
<p>In C/C++, <code>a || b</code> is equivalent to,</p>
<pre>
  if a evaluates to a non-zero value:
    return true;
  if b evaluates to a non-zero value:
    return true;
  otherwise:
    return false;
</pre>
<p>Note that if <code>a</code> can be converted to <code>true</code>, then <code>b</code> is <em>not</em> evaluated. Importantly, <strong>in C/C++ <code>||</code> always returns a <code>bool</code></strong>.</p>
<p>But <strong>the JavaScript <code>||</code> returns the <em>value</em> of the first variable that can be converted to <code>true</code></strong>, or the last variable if both variables can&#8217;t be interpreted as <code>true</code>,</p>
<pre>
  if a evaluates to a non-zero value:
    return a;
  otherwise:
    return b;
</pre>
<h3>Concise</h3>
<p>JavaScript&#8217;s <code>||</code> is some sweet syntactic sugar.</p>
<p>We can write,</p>
<pre>return playerName || "Player 1";</pre>
<p>instead of,</p>
<pre>return playerName ? playerName : "Player 1";</pre>
<p>And simplify <code>assert</code>-like code in a perl-esq way,</p>
<pre>x || throw "x was unexpectedly null!";</pre>
<p>It&#8217;s interesting that a more concise definition of <code>||</code> allows more concise code, even though intuitively we&#8217;d expect a more complex <code>||</code> to &#8220;do more work for us&#8221;.</p>
<h3>General</h3>
<p>Defining <code>||</code> to return values, not <code>true</code>/<code>false</code>, is much more useful for functional programming.</p>
<p>The short-circuit-evaluation is powerful enough to replace <code>if</code>-statements. For example, the familiar factorial function,</p>
<pre>function factorial(n){
	if(n == 0) return 1;
	return n*factorial(n-1);
}</pre>
<p>can be written in JavaScript using <code>&#038;&#038;</code> and <code>||</code> expressions,</p>
<pre>function factorial2(n){ return n * (n &#038;&#038; factorial2(n-1)) || 1;}</pre>
<p>Yes, I know this isn&#8217;t the clearest way to write a factorial, and it would still be an expression if it used <code>?:</code>, but hopefully this gives you a sense of what short-circuiting operations can do.</p>
<p>Unlike <code>?:</code>, the two-argument <code>||</code> intuitively generalizes to <em>n</em> arguments, equivalent to <code>a1 || a2 || ... || a<em>n</em></code>. This makes it even more useful for dealing with abstractions.</p>
<p>Logical operators that return values, instead of simply booleans, are more expressive and powerful, although at first they may not seem useful &#8212; especially coming from a language without them.</p>
]]></content:encoded>
			<wfw:commentRss>http://vgable.com/blog/2009/10/20/javascript-nailed/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>sizeof() Style</title>
		<link>http://vgable.com/blog/2009/10/19/sizeof-style/</link>
		<comments>http://vgable.com/blog/2009/10/19/sizeof-style/#comments</comments>
		<pubDate>Mon, 19 Oct 2009 17:23:26 +0000</pubDate>
		<dc:creator>Vincent Gable</dc:creator>
				<category><![CDATA[Bug Bite]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Programming Style]]></category>
		<category><![CDATA[sizeof]]></category>

		<guid isPermaLink="false">http://vgable.com/blog/?p=460</guid>
		<description><![CDATA[Never say sizeof(sometype) when you can say sizeof(a_variable). The latter works even if the type of a_variable changes, and it is much more obvious what the size is supposed to represent.]]></description>
			<content:encoded><![CDATA[<p>Never say <code>sizeof(sometype)</code> when you can say <code>sizeof(a_variable)</code>.  The latter works even if the type of <code>a_variable</code> changes, and it is much more obvious what the size is supposed to represent.</p>
]]></content:encoded>
			<wfw:commentRss>http://vgable.com/blog/2009/10/19/sizeof-style/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Don&#8217;t Check malloc()</title>
		<link>http://vgable.com/blog/2009/10/12/dont-check-malloc/</link>
		<comments>http://vgable.com/blog/2009/10/12/dont-check-malloc/#comments</comments>
		<pubDate>Tue, 13 Oct 2009 01:25:21 +0000</pubDate>
		<dc:creator>Vincent Gable</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[MacOSX]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Quotes]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[malloc]]></category>
		<category><![CDATA[Mike Ash]]></category>

		<guid isPermaLink="false">http://vgable.com/blog/?p=434</guid>
		<description><![CDATA[There&#8217;s no point in trying to recover from a malloc failure on OS X, because by the time you detect the failure and try to recover, your process is likely to already be doomed. There&#8217;s no need to do your own logging, because malloc itself does a good job of that. And finally there&#8217;s no [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>There&#8217;s no point in trying to recover from a <code>malloc</code> failure on OS X, because by the time you detect the failure and try to recover, your process is likely to already be doomed. There&#8217;s no need to do your own logging, because <code>malloc</code> itself does a good job of that. And finally there&#8217;s no real need to even explicitly abort, because any <code>malloc</code> failure is virtually guaranteed to result in an instantaneous crash with a good stack trace.</p></blockquote>
<p>&#8211;<a href="http://www.mikeash.com/?page=pyblog/friday-qa-2009-10-09-defensive-programming.html">Mike Ash</a></p>
<p>This is <em>excellent advice</em>. Peppering your code with <code>if</code> statements harms readability and simplicity.</p>
<p>It&#8217;s still a good idea to check large (many MB) <code>malloc</code>s, but I can&#8217;t imagine recovering gracefully from a situation where 32 byte memory allocations are failing on a modern desktop.</p>
]]></content:encoded>
			<wfw:commentRss>http://vgable.com/blog/2009/10/12/dont-check-malloc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Never Start An Integer With 0</title>
		<link>http://vgable.com/blog/2009/09/11/never-start-an-integer-with-0/</link>
		<comments>http://vgable.com/blog/2009/09/11/never-start-an-integer-with-0/#comments</comments>
		<pubDate>Fri, 11 Sep 2009 12:14:12 +0000</pubDate>
		<dc:creator>Vincent Gable</dc:creator>
				<category><![CDATA[Bug Bite]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[Octal]]></category>
		<category><![CDATA[Programming Language Design]]></category>
		<category><![CDATA[Programming Style]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Smalltalk]]></category>

		<guid isPermaLink="false">http://vgable.com/blog/?p=389</guid>
		<description><![CDATA[When programming, never start an integer with 0. Most programming languages treat a decimal number that starts with 0 as octal (base-8). So x = 013; does not set x to 13. Instead x is 11, because 013 is interpreted as 138 not 1310. Languages with this quirk include: C, C++, Objective-C, Java, JavaScript, Perl, [...]]]></description>
			<content:encoded><![CDATA[<p>When programming, <strong>never start an integer with 0</strong>. <em>Most</em> programming languages treat a decimal number that starts with 0 as octal (base-8). So <code>x = 013;</code> does <em>not</em> set <code>x</code> to 13. Instead <code>x</code> is 11, because <code>013</code> is interpreted as 13<sub>8</sub> not 13<sub>10</sub>.</p>
<p>Languages with this quirk include: <a href="http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/BitOp/hexoctal.html">C, C++, Objective-C</a>, <a href="http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html">Java</a>, <a href="http://www.amazon.com/gp/product/0596101996?ie=UTF8&#038;tag=vincgabl-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0596101996">JavaScript</a>, <a href="http://perl.active-venture.com/pod/perlfaq4-datanumber.html">Perl</a>, <a href="http://www.ibm.com/developerworks/opensource/library/os-python1/">Python 3.0</a>, and <a href="http://docs.huihoo.com/ruby/ruby-man-1.4/syntax.html#numeric">Ruby</a>. If you add up the &#8220;<a href="http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html">market share</a>&#8221; of these languages, it comes out to above 50%, which is why I say <em>most</em> languages.</p>
<h3>&#8220;But I use {Smalltalk, Haskell, Lisp, etc.}&#8221;</h3>
<p>I&#8217;m jealous that you get to use such a nice language. However, it&#8217;s bad programming hygiene to pick up habits that are <em>dangerous</em> in common languages.</p>
<p>Now, I assume you wouldn&#8217;t write 7 as <code>007</code>, unless the leading zero(s) carried some extra meaning. There are cases where this clarity outweighs &#8220;cleanliness&#8221; (unless the code meant to be ported to a C-like language).</p>
<p>But you should at least be aware of this inter-lingual gotcha.</p>
]]></content:encoded>
			<wfw:commentRss>http://vgable.com/blog/2009/09/11/never-start-an-integer-with-0/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ignoring Just One Deprecated Warning</title>
		<link>http://vgable.com/blog/2009/06/15/ignoring-just-one-deprecated-warning/</link>
		<comments>http://vgable.com/blog/2009/06/15/ignoring-just-one-deprecated-warning/#comments</comments>
		<pubDate>Tue, 16 Jun 2009 02:38:11 +0000</pubDate>
		<dc:creator>Vincent Gable</dc:creator>
				<category><![CDATA[Bug Bite]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Compilers]]></category>
		<category><![CDATA[GCC]]></category>
		<category><![CDATA[iPhone OS 3.0]]></category>
		<category><![CDATA[Project Management]]></category>
		<category><![CDATA[Prometheus Development]]></category>
		<category><![CDATA[Xcode]]></category>

		<guid isPermaLink="false">http://vgable.com/blog/2009/06/15/ignoring-just-one-deprecated-warning/</guid>
		<description><![CDATA[Switching projects over to iPhone OS 3.0 means discovering that functions I&#8217;m using are deprecated. Occasionally there isn&#8217;t a totally straightforward replacement, and the best thing to do is to file a bug/TODO/note for myself, and ignore the warning until a later version, when major refactoring will be possible. But bitter experience has taught me [...]]]></description>
			<content:encoded><![CDATA[<p>Switching projects over to iPhone OS 3.0 means discovering that functions I&#8217;m using are deprecated. Occasionally there isn&#8217;t a totally straightforward replacement, and the best thing to do is to <em>file a bug/TODO/note for myself</em>, and ignore the warning until a later version, when major refactoring will be possible. But bitter experience has taught me to have Xcode treat warnings as errors<sup>1</sup>, so it&#8217;s necessary to trick the compiler into ignoring the warning for things to build.</p>
<h3><code>-Wno-deprecated</code></h3>
<p>The <code><a href="http://developer.apple.com/documentation/DeveloperTools/gcc-4.0.1/gcc/Warning-Options.html#Warning-Options">-Wno-deprecated</a></code> flag tells GCC to suppress warnings about deprecated code. But <a href="http://developer.apple.com/tools/xcode/xcodebuildsettings.html">adding it to an Xcode project</a> means you won&#8217;t get useful warnings about <em>other</em> depreciated code.</p>
<p>You could file a bug telling yourself to turn that warning back on after the deprecated functionality has been updated. That should work just fine. But it feels like bad project hygiene to me.</p>
<h3>Casting and Protocols</h3>
<p>Type casting is a dangerous old-C technique that&#8217;s <em>earned</em> its infamy. But it&#8217;s undeniably fitting to use a <a href="http://www.acm.org/crossroads/xrds3-1/ovp3-1.html">deprecated</a> language feature to get deprecated code to build. The basic idea is to declare a protocol that includes the method you want to suppress warnings for,</p>
<pre>
@protocol DeprecatedHack
- (void) myDeprecatedMethod;
@end
</pre>
<p>then just <em>cast</em> your objects so the compiler thinks they implement the protocol,</p>
<pre>
[foo myDeprecatedMethod]; //warnings
[(id&lt;DeprecatedHack&gt;>)foo myDeprecatedMethod]; //no warnings
</pre>
<p>Although having to declare a protocol is somewhat heavyweight, it leaves a nice artifact <em>in the code</em> reminding you to replace deprecated functionality.</p>
<h3>Protocols Not Required</h3>
<p>Sometimes just casting to <code>id</code> is enough. This happens if another object has a non-deprecated method with the same name. </p>
<p><sup>1</sup><small>For experimental or prototyping projects I let warnings slide. But in the main project I always treat warnings as errors. Ignoring them in production code has never worked &#8212; warnings fester and grow on each other.</p>
<p>Because Objective-C is so dynamic, there are many errors that the compiler can warn you about, but can&#8217;t be <em>totally sure</em> are errors. For example, methods can be added to a class at runtime, so if you call <code>-someMethodThatDoesNotExistAnywhere</code>, the compiler will warn you that something is up, but won&#8217;t stop the build, because the necessary code <em>could </em>magically appear at runtime. Of course, 99% of the time, it&#8217;s me accidentally using <code>count</code> when I meant <code>length</code>, etc. What I&#8217;m really trying to say here is that treating warnings as errors is an even better idea in Objective-C.</p>
<p></small></p>
]]></content:encoded>
			<wfw:commentRss>http://vgable.com/blog/2009/06/15/ignoring-just-one-deprecated-warning/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Objective-C 1.0 Style: Don&#8217;t Name Your Enumerators &#8220;enumerator&#8221;!</title>
		<link>http://vgable.com/blog/2009/01/07/objective-c-10-style-dont-your-enumerators-enumerator/</link>
		<comments>http://vgable.com/blog/2009/01/07/objective-c-10-style-dont-your-enumerators-enumerator/#comments</comments>
		<pubDate>Thu, 08 Jan 2009 00:36:58 +0000</pubDate>
		<dc:creator>Vincent Gable</dc:creator>
				<category><![CDATA[Bug Bite]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Usability]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Names]]></category>
		<category><![CDATA[NSEnumerator]]></category>
		<category><![CDATA[Programming Style]]></category>

		<guid isPermaLink="false">http://vgable.com/blog/2009/01/07/objective-c-10-style-dont-your-enumerators-enumerator/</guid>
		<description><![CDATA[Disclaimer There is a better way to iterate over collections in Objective-C 1.0. You really should use it. It&#8217;s easier to write, easier to read, less prone to bugs, faster, and makes what I&#8217;m going to rant about here a non-issue, because you won&#8217;t have any NSEnumerator variables in your code. Badly Named Problem The [...]]]></description>
			<content:encoded><![CDATA[<h3>Disclaimer</h3>
<p><a href="http://vgable.com/blog/2008/12/28/fast-enumeration-in-objective-c-10/">There is a better way to iterate over collections in Objective-C 1.0</a>.  You really should use it.  It&#8217;s easier to write, easier to read, less prone to bugs, faster, and makes what I&#8217;m going to rant about here a non-issue, because you won&#8217;t have any <code>NSEnumerator</code> variables in your code.  </p>
<h3>Badly Named Problem</h3>
<p>The standard iteration idiom in Objective-C 1.0 is:<br />
<code><br />
NSEnumerator *<em>enumerator</em> = [collection objectEnumerator];<br />
id element = nil;<br />
while( nil != (element = [<em>enumerator</em> nextObject]) ) {<br />
&nbsp;&nbsp;&nbsp;;//do stuff...<br />
}<br />
</code></p>
<p>Unfortunately, <strong>I see otherwise steller programmers name their <code>NSEnumerator</code> variables &#8220;enumerator&#8221;.</strong>  That&#8217;s always wrong, because <strong>it does not tell you <em>what</em> the enumerator enumerates</strong>.  We already know that <code>enumerator</code> enumerates things, because it&#8217;s type is <code>NSEnumerator</code>, unless it&#8217;s name tells us more then that it&#8217;s hardly better then no name at all.</p>
<p>This is an especially amateurish practice because &#8230;</p>
<h3>Naming an Enumerator Well is Easy!</h3>
<p>Call it, <strong>the plural of the <code><em>element</em></code> variable</strong>.  And if that won&#8217;t work you can <em>always</em> fall back on calling it <strong><code><em>collection</em>Enumerator</code></strong>.</p>
<p>For example, to fix:</p>
<pre>NSEnumerator *enumerator = [input objectEnumerator];
NSString *path = nil;
while (path = [enumerator nextObject])</pre>
<p>We should name <code>enumerator</code> <code><strong>paths</strong></code> or <code><strong>inputEnumerator</strong></code>.  You might find &#8220;paths&#8221; to be too close to &#8220;path&#8221; in which case <strong>let the &#8220;plural form&#8221; of <code><em>element</em></code> be <code>every<em>Element</em></code></strong>, giving <code><strong>everyPath</strong></code>.</p>
<p>These rules can be applied without much thought, but will improve the clarity of code.</p>
<h3>Why <code>enumerator</code> is Worse Than <code>i</code></h3>
<p>Firstly, the practice of naming an the index in a <code>for</code>-loop <code>i</code> is not good.  You can avoid it by renaming <code>i</code> to <code><em>thingThatIsIndexed</em>Index</code>.</p>
<p>But at least, <code>for(int i = 0; i &lt; collection.size(); i++)</code>, is <em>concise</em>; therefore better than a equally-poorly-named <code>NSEnumerator</code>.</p>
<p>Also, there is something to be said for the idiom you can just use <code>collection[i]</code> over declaring an extra <code>element</code> variable.</p>
<h3>The Right Choice</h3>
<p>Everyone agrees informative names are good, yet poorly named enumerators are everywhere (just browse Apple&#8217;s sample code!)  Poorly named enumerators persist because nobody really uses an enumerator per se, they are just part of an iteration idiom.  (So stop declaring them and <a href="http://vgable.com/blog/2008/12/28/fast-enumeration-in-objective-c-10/">iterate smarter</a>).  When was the last time you saw code that did anything with an <code>enumerator</code> besides <code>[enumerator nextObject]</code> in a <code>while</code> loop?</p>
<p>But <em>bad habits matter</em>.  Don&#8217;t pick up the habit of naming something poorly when it&#8217;s easy to do the right thing.</p>
]]></content:encoded>
			<wfw:commentRss>http://vgable.com/blog/2009/01/07/objective-c-10-style-dont-your-enumerators-enumerator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How To Multi</title>
		<link>http://vgable.com/blog/2008/12/22/how-to-multi/</link>
		<comments>http://vgable.com/blog/2008/12/22/how-to-multi/#comments</comments>
		<pubDate>Mon, 22 Dec 2008 21:54:46 +0000</pubDate>
		<dc:creator>Vincent Gable</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[MacOSX]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Quotes]]></category>
		<category><![CDATA[Distributed Computing]]></category>
		<category><![CDATA[Futurism]]></category>
		<category><![CDATA[Multi-Core]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Parallelization]]></category>
		<category><![CDATA[Threads]]></category>

		<guid isPermaLink="false">http://vgable.com/blog/2008/12/22/how-to-multi/</guid>
		<description><![CDATA[Avoid distributed computing unless your code is going to be run by a single client with a lot of available hardware. Being able to snarf up CPU cycles from idle hardware sitting around in the user&#8217;s house sounds cool but just doesn&#8217;t pay off most of the time. Avoid GPGPU on the Mac until Snow [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p> <strong>Avoid distributed computing unless your code is going to be run by a single client with a lot of available hardware.</strong> Being able to snarf up CPU cycles from idle hardware sitting around in the user&#8217;s house sounds cool but just doesn&#8217;t pay off most of the time.</p>
<p><strong>Avoid GPGPU on the Mac until Snow Leopard ships</strong> unless you have a really good application for it. OpenCL will make GPGPU a lot more practical and flexible, so trying to shoehorn your computationally expensive code into GLSL or CoreImage today just doesn&#8217;t seem worth it.</p>
<p><strong>Using multiple processes is a good idea if the subprograms are already written. &#8230; If you&#8217;re writing your code from scratch, I don&#8217;t recommend it</strong> unless you have another good reason to write subprocesses, as it&#8217;s difficult and the reward just isn&#8217;t there.</p>
<p><strong>For multithreading, concentrate on message passing and operations.</strong> Multithreading is never easy, but these help greatly to make it simpler and less error prone.</p>
<p><strong>Good OO design will also help a lot here.</strong> It&#8217;s vastly easier to multithread an app which has already been decomposed into simple objects with well-defined interfaces and loose coupling between them.</p></blockquote>
<p>&#8211;<a href="http://www.mikeash.com/?page=pyblog/friday-qa-2008-12-19.html">Mike Ash</a> (emphasis mine, line-breaks added).  <a href="http://www.mikeash.com/?page=pyblog/friday-qa-2008-12-19.html">The article has more detail and is very much worth reading</a>.</p>
<p>One point that this advice really drives home for me is that <strong>you need to focus on making good code first, and defer micro-optimizations</strong>.  If taking the time to clean up some code makes it easier to parallelize, then you <em>are</em> optimizing your code by refactoring it, even if at a micro-level you might be making some of it slower by, say, not caching something that takes O(1) time to compute.</p>
<p>Apple does not sell a Mac that&#8217;s not multi-core, and even the iPhone has a CPU <em>and</em> a GPU. There&#8217;s no question that optimization means parallelization.  And all signs point to computers getting more parallel in the future.  Any optimization that hurts parallelization is probably a mistake.</p>
]]></content:encoded>
			<wfw:commentRss>http://vgable.com/blog/2008/12/22/how-to-multi/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

