{"id":371,"date":"2008-08-14T09:33:24","date_gmt":"2008-08-14T16:33:24","guid":{"rendered":"http:\/\/www.corbinstreehouse.com\/blog\/index.php\/2008\/08\/xcode-code-completion-and-your-code\/"},"modified":"2018-10-31T10:15:17","modified_gmt":"2018-10-31T17:15:17","slug":"xcode-code-completion-and-your-code","status":"publish","type":"post","link":"https:\/\/www.corbinstreehouse.com\/blog\/2008\/08\/xcode-code-completion-and-your-code\/","title":{"rendered":"Xcode code completion and your code"},"content":{"rendered":"<p id=\"top\" \/>\n<p>How can you become a faster Cocoa programmer? One way is to adequately name your variables, enums and classes.<\/p>\n<p>Let&#8217;s start with enums and take an example from something new to NSTableView in Leopard. This is copied from NSTableView.h with the comments stripped out for clarity.<\/p>\n<p><span style=\"color: #AA0D91; font-family: Monaco; font-size: 10px;\">enum <span style=\"color: #000000\">{<\/span><\/span><\/p>\n<p style=\"font: 10.0px Monaco; color: #007400\"><span style=\"color: #2E0D6E;\">NSTableViewSelectionHighlightStyleRegular <span style=\"color: #000000\">=<\/span> <span style=\"color: #1c00cf\">0<\/span><span style=\"color: #000000\">,<\/span><\/span><\/p>\n<p style=\"font: 10.0px Monaco; min-height: 14.0px\"><span style=\"color: #2E0D6E;\">NSTableViewSelectionHighlightStyleSourceList <span style=\"color: #000000\">=<\/span> <span style=\"color: #1c00cf\">1<\/span><span style=\"color: #000000\">,<\/span><\/span><\/p>\n<p style=\"font: 10.0px Monaco\">};<\/p>\n<p style=\"font: 10.0px Monaco\"><span style=\"color: #aa0d91\">typedef<\/span> NSInteger NSTableViewSelectionHighlightStyle;<\/p>\n<p style=\"font: 10.0px Monaco; color: #007400\"><span style=\"color: #5C2699;\"><span style=\"color: #000000\">&#8211; (<\/span>NSTableViewSelectionHighlightStyle<span style=\"color: #000000\">)selectionHighlightStyle;<\/span><\/span><\/p>\n<p style=\"font: 10.0px Monaco\">&#8211; (<span style=\"color: #aa0d91\">void<\/span>)setSelectionHighlightStyle:(<span style=\"color: #5c2699\">NSTableViewSelectionHighlightStyle<\/span>)selectionHighlightStyle;<\/p>\n<p>\nThere are several things to notice here, some of which are important to you. The most important thing (in my opinion) is the common prefix. Notice that the enum values fully contain the enum type name. Why? The answer is code completion, which you should be using. It is much easier to remember one key portion of the name than to remember all values. In this case, the key thing to remember is &#8220;selection&#8221;.<br \/>\nAs a programmer working with NSTableView you <strong>know<\/strong> you want to change the selection highlight style, but you don&#8217;t remember the option for the specific style you want. You <strong>know<\/strong> the Cocoa convention is setFoo, so you type:<\/p>\n<pre class=\"code\">\r\n[tableView set\r\n<\/pre>\n<p>And hit escape (or whatever key combo invokes code completion for you. For me, I remapped the key to ctrl-space, since I was used to Delphi and Visual Studio. But, I also use escape).<br \/>\nYou see this result:<\/p>\n<p><a href=\"http:\/\/www.corbinstreehouse.com\/blog\/wp-content\/uploads\/2008\/08\/tableviewset-codecomplete.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.corbinstreehouse.com\/blog\/wp-content\/uploads\/2008\/08\/tableviewset-codecomplete-tm.jpg\" width=\"500\" height=\"209\" alt=\"TableViewSet_CodeComplete.png\" \/><\/a><\/p>\n<p>and start typing &#8220;sel&#8221; to see the result you want:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.corbinstreehouse.com\/blog\/wp-content\/uploads\/2008\/08\/tableviewsetselresult.jpg\" width=\"653\" height=\"82\" alt=\"TableViewSetSelResult.png\" \/><br \/>\nWhich inserts this template:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.corbinstreehouse.com\/blog\/wp-content\/uploads\/2008\/08\/tableviewsettemplate.jpg\" width=\"611\" height=\"25\" alt=\"TableViewSetTemplate.png\" \/><br \/>\nNow, I&#8217;m surprised, but most people don&#8217;t realize that they can type ctrl-\/ (or maybe alt-\/ depending on your key bindings) to select the placeholder and type over it. Memorize that keystroke, and use it.<br \/>\nNow, the common prefix name comes in <strong>really<\/strong> handy with code completion &#8212; just start typing in the type that the placeholder tells you and you&#8217;ll see what options you have:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.corbinstreehouse.com\/blog\/wp-content\/uploads\/2008\/08\/tableviewsetoptions.jpg\" width=\"587\" height=\"110\" alt=\"TableViewSetOptions.png\" \/><br \/>\nIn essence, you only have to remember &#8220;sel&#8221;, and from there you can derive exactly what option you want using code completion. Less memorization, and faster programming.<br \/>\nUnfortunately, a lot of Cocoa came along before code completion, and doesn&#8217;t follow this convention. But if you look at a new UI framework (ala: UIKit for the iPhone), you&#8217;ll find this pattern throughout it. It makes programming very fast with fewer trips to the header to find out what you need.<\/p>\n<p>The bottom line: use a common prefix, wherever you have a list of options. Also note that the NSTableViewSelectionHighlightStyle has the prefix NSTableView, since it <strong>only<\/strong> applies to NSTableView. But, the property name is &#8220;selectionHighlightStyle&#8221;, since it doesn&#8217;t make sense to replicate the type name there.<\/p>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How can you become a faster Cocoa programmer? One way is to adequately name your variables, enums and classes. Let&#8217;s start with enums and take an example from something new to NSTableView in Leopard. This&#8230; <a class=\"read-more\" href=\"https:\/\/www.corbinstreehouse.com\/blog\/2008\/08\/xcode-code-completion-and-your-code\/\">[read more]<\/a><\/p>\n","protected":false},"author":1,"featured_media":366,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4,6,86],"tags":[63,130,72],"class_list":["post-371","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-apple","category-cocoa","category-coding","tag-cocoa","tag-objc","tag-programming"],"jetpack_featured_media_url":"https:\/\/www.corbinstreehouse.com\/blog\/wp-content\/uploads\/2008\/08\/tableviewsetoptions.jpg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/posts\/371","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/comments?post=371"}],"version-history":[{"count":1,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/posts\/371\/revisions"}],"predecessor-version":[{"id":5176,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/posts\/371\/revisions\/5176"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/media\/366"}],"wp:attachment":[{"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/media?parent=371"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/categories?post=371"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/tags?post=371"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}