{"id":603,"date":"2010-05-24T00:31:42","date_gmt":"2010-05-24T05:31:42","guid":{"rendered":"http:\/\/vgable.com\/blog\/?p=603"},"modified":"2010-05-24T00:31:45","modified_gmt":"2010-05-24T05:31:45","slug":"never-name-a-variable-index","status":"publish","type":"post","link":"https:\/\/vgable.com\/blog\/2010\/05\/24\/never-name-a-variable-index\/","title":{"rendered":"Never Name a Variable &#8220;Index&#8221;"},"content":{"rendered":"<p><strong><em>Never<\/em> name a variable <code>index<\/code><\/strong>, especially in C.<\/p>\n<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>\n<p>If the index variable is just used to enumerate over a collection of objects, (eg. <code>for(int i = 0; i &lt arraySize; i++){\u2026} <\/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>\n<h3>Why <code>index<\/code> is Especially Bad in C<\/h3>\n<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>\n<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\u00a0&#8212; 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>\n<pre>\nchar * world = index(\"Hello, World\", 'W');\nNSLog(@\"'%s'\", world);\n<\/pre>\n<p>Prints &#8220;&#8216;World'&#8221;, but<\/p>\n<pre>\nint index = 0;\nchar * world = index(\"Hello, World\", 'W');\nNSLog(@\"'%s'\", world);\n<\/pre>\n<p>Won&#8217;t compile, because an <code>int<\/code> isn&#8217;t a function.<\/p>\n<p>Obviously this is a problem for code that uses the <code>index()<\/code> function \u2014 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>\n<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>\n<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>\n","protected":false},"excerpt":{"rendered":"<p>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++){\u2026} ) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,7,6,5,4],"tags":[613,413],"class_list":["post-603","post","type-post","status-publish","format-standard","hentry","category-bug-bite","category-c","category-cocoa","category-objective-c","category-programming","tag-c","tag-names"],"_links":{"self":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/603","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/comments?post=603"}],"version-history":[{"count":1,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/603\/revisions"}],"predecessor-version":[{"id":604,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/603\/revisions\/604"}],"wp:attachment":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/media?parent=603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/categories?post=603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/tags?post=603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}