<?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>Stack Overflow &#187; variable naming</title>
	<atom:link href="http://embeddedgurus.com/stack-overflow/tag/variable-naming/feed/" rel="self" type="application/rss+xml" />
	<link>http://embeddedgurus.com/stack-overflow</link>
	<description>Thoughts on embedded systems by Nigel Jones</description>
	<lastBuildDate>Wed, 01 Feb 2012 20:38:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Eye, Aye I!</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/11/eye-aye-i/</link>
		<comments>http://embeddedgurus.com/stack-overflow/2009/11/eye-aye-i/#comments</comments>
		<pubDate>Fri, 06 Nov 2009 15:25:00 +0000</pubDate>
		<dc:creator>Nigel Jones</dc:creator>
				<category><![CDATA[Coding Standards]]></category>
		<category><![CDATA[General C issues]]></category>
		<category><![CDATA[variable naming]]></category>

		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/11/06/eye-aye-i/</guid>
		<description><![CDATA[Today&#8217;s post should probably be called &#8216;Thoughts on non-descriptive variable names&#8217;, but once in a while I have to let my creative side out! Anyway, the motivation for today&#8217;s post, is actually Michael Barr&#8217;s latest blog posting concerning analysis of the source code for a breathalyzer. Since I do expert witness work, as well as [...]]]></description>
			<content:encoded><![CDATA[<p>Today&#8217;s post should probably be called &#8216;Thoughts on non-descriptive variable names&#8217;, but once in a while I have to let my creative side out!</p>
<p>Anyway, the motivation for today&#8217;s post, is actually Michael Barr&#8217;s latest blog <a href="http://embeddedgurus.com/barr-code/2009/11/breathalyzer-source-code-analysis/">posting </a>concerning analysis of the source code for a breathalyzer. Since I do expert witness work, as well as develop products I was keen to see what the experts in this case had to say. One snippet from the expert for the plaintiffs caught my eye. In <a href="http://www.dwi.com/new-jersey/new-jersey/code-review">appendix B of their report, </a>Draeger made the following statement concerning general code issues:</p>
<blockquote><p>Non descriptive variable names – i, j, dummy and temp</p></blockquote>
<p>This touched upon something where I seem to be at odds with the conventional wisdom. I&#8217;ll illustrate what I mean. Consider initializing an array to zero (I&#8217;ll ignore that we could use a library function for this). I would code it like this:</p>
<pre>uint8_t buffer[BUFSIZE];
uint8_t i;</pre>
<pre>for (i = 0; i &lt; BUFSIZE; i++)
{
 buffer[i] = 0;
}</pre>
<p>This code would be rejected by many coding standards (and apparently would offend Draeger), as the loop variable &#8216;i&#8217; is not descriptive. To be &#8216;correct&#8217;, I should instead code it like this</p>
<pre>uint8_t buffer[BUFSIZE];
uint8_t buffer_index;</pre>
<pre>for (buffer_index = 0; buffer_index &lt;BUFSIZE; buffer_index++)
{
 buffer[buffer_index] = 0;
}</pre>
<p>So for me, the question is, does the second approach buy me anything &#8211; or indeed cost me anything? Well clearly, this is a matter of opinion. However I&#8217;d make the following observations:</p>
<ol>
<li>I think my code is clear, concise and easily understood by even the most unskilled programmer</li>
<li>Is the variable name &#8216;buffer_index&#8217; clearer &#8211; yes but only to a native English speaker. It&#8217;s my experience that there are a lot of non-native English speakers in the industry.</li>
<li>Personally, I find the use of similar words in close proximity (buffer[buffer_index]) to be a bit harder to read, and very easy to mis-read if there are other variables around prefixed with buffer.</li>
</ol>
<p>I&#8217;d also make the observation that many coding standards require variable names to be at least 3 characters long, and as a result I&#8217;ve seen code that looks like this:</p>
<pre>uint8_t buffer[BUFSIZE];
uint8_t iii;</pre>
<pre>for (iii = 0; iii &lt; BUFSIZE; iii++)
{
 buffer[iii] = 0;
}</pre>
<p>Clearly in this case, the person is addressing the letter of the standard (if you&#8217;ll pardon the pun), but not the spirit. Where the standard requires the variable names to be meaningful, I&#8217;ve also seen this done:</p>
<pre>uint8_t buffer[BUFSIZE];
uint8_t idx;</pre>
<pre>for (idx = 0; idx &lt; BUFSIZE; idx++)
{
 buffer[idx] = 0;
}</pre>
<p>This code meets the letter of the standard, and arguably the spirit. Is it really any more understandable than my original code? I don&#8217;t think so &#8211; but I&#8217;ll be interested to get your comments.</p>
<p><a href="http://embeddedgurus.com/stack-overflow/">Home</a></p>
]]></content:encoded>
			<wfw:commentRss>http://embeddedgurus.com/stack-overflow/2009/11/eye-aye-i/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

