{"id":202,"date":"2009-01-07T19:36:58","date_gmt":"2009-01-08T00:36:58","guid":{"rendered":"http:\/\/vgable.com\/blog\/2009\/01\/07\/objective-c-10-style-dont-your-enumerators-enumerator\/"},"modified":"2009-10-08T16:35:58","modified_gmt":"2009-10-08T21:35:58","slug":"objective-c-10-style-dont-your-enumerators-enumerator","status":"publish","type":"post","link":"https:\/\/vgable.com\/blog\/2009\/01\/07\/objective-c-10-style-dont-your-enumerators-enumerator\/","title":{"rendered":"Objective-C 1.0 Style: Don&#8217;t Name Your Enumerators &#8220;enumerator&#8221;!"},"content":{"rendered":"<h3>Disclaimer<\/h3>\n<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>\n<h3>Badly Named Problem<\/h3>\n<p>The standard iteration idiom in Objective-C 1.0 is:<br \/>\n<code><br \/>\nNSEnumerator *<em>enumerator<\/em> = [collection objectEnumerator];<br \/>\nid element = nil;<br \/>\nwhile( nil != (element = [<em>enumerator<\/em> nextObject]) ) {<br \/>\n&nbsp;&nbsp;&nbsp;;\/\/do stuff...<br \/>\n}<br \/>\n<\/code><\/p>\n<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>\n<p>This is an especially amateurish practice because &#8230;<\/p>\n<h3>Naming an Enumerator Well is Easy!<\/h3>\n<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>\n<p>For example, to fix:<\/p>\n<pre>NSEnumerator *enumerator = [input objectEnumerator];\nNSString *path = nil;\nwhile (path = [enumerator nextObject])<\/pre>\n<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>\n<p>These rules can be applied without much thought, but will improve the clarity of code.<\/p>\n<h3>Why <code>enumerator<\/code> is Worse Than <code>i<\/code><\/h3>\n<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>\n<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>\n<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>\n<h3>The Right Choice<\/h3>\n<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>\n<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>\n","protected":false},"excerpt":{"rendered":"<p>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 [&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,12,5,4,10,8],"tags":[241,413,288,242],"class_list":["post-202","post","type-post","status-publish","format-standard","hentry","category-bug-bite","category-c","category-cocoa","category-design","category-objective-c","category-programming","category-tips","category-usability","tag-best-practices","tag-names","tag-nsenumerator","tag-programming-style"],"_links":{"self":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/202","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=202"}],"version-history":[{"count":2,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/202\/revisions"}],"predecessor-version":[{"id":431,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/202\/revisions\/431"}],"wp:attachment":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/media?parent=202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/categories?post=202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/tags?post=202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}