Xcode code completion and your code

Apple, Cocoa, Coding

How can you become a faster Cocoa programmer? One way is to adequately name your variables, enums and classes.

Let’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.

enum {

NSTableViewSelectionHighlightStyleRegular = 0,

NSTableViewSelectionHighlightStyleSourceList = 1,

};

typedef NSInteger NSTableViewSelectionHighlightStyle;

– (NSTableViewSelectionHighlightStyle)selectionHighlightStyle;

– (void)setSelectionHighlightStyle:(NSTableViewSelectionHighlightStyle)selectionHighlightStyle;

There 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 “selection”.
As a programmer working with NSTableView you know you want to change the selection highlight style, but you don’t remember the option for the specific style you want. You know the Cocoa convention is setFoo, so you type:

[tableView set

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).
You see this result:

TableViewSet_CodeComplete.png

and start typing “sel” to see the result you want:

TableViewSetSelResult.png
Which inserts this template:

TableViewSetTemplate.png
Now, I’m surprised, but most people don’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.
Now, the common prefix name comes in really handy with code completion — just start typing in the type that the placeholder tells you and you’ll see what options you have:

TableViewSetOptions.png
In essence, you only have to remember “sel”, and from there you can derive exactly what option you want using code completion. Less memorization, and faster programming.
Unfortunately, a lot of Cocoa came along before code completion, and doesn’t follow this convention. But if you look at a new UI framework (ala: UIKit for the iPhone), you’ll find this pattern throughout it. It makes programming very fast with fewer trips to the header to find out what you need.

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 only applies to NSTableView. But, the property name is “selectionHighlightStyle”, since it doesn’t make sense to replicate the type name there.



Subscribe
Notify of
guest

4 Comments
Inline Feedbacks
View all comments
Stephane

One thing that annoys me with code completion in Xcode (3.x since this is the first release where code completion is working quite well and fast) is that it has no respect for lower or uppercase.

This ends up with suggestions that are not correct.

I tend to believe the fact that code completion has not been working (like syntax coloring for 10 years) until Xcode 3.x explains why the pattern you describe has not been adopted earlier.

Mark Dalrymple

Even then, the completeness of the available completion symbols is random. Sometimes it’ll just not suggest the one symbol you want. (rdar://6001580). Ending up on the wrong side of Completion Roulette is a big reason why I turned it off.

corbin

Stephane — you are right, it is case insensitive. Hopefully you don’t have code that varies by case-only in some way, which could be an easy way to make coding errors (akin to variable names that differ by a single letter). I actually rely on the fact that the completion is case insensitive. Feel free to log a bug on bugreporter.apple.com — maybe there is already an advanced option to make it respect.

Mark — There are some issues still present — thank you for logging bugs! I also turn off the auto suggestion feature of code completion. I don’t like it popping up at wrong times. Instead, i rely heavily on the manual invoking of code completion when I want to have it finish what I’m typing.

Andy

I apologize for replying to such an old post, but I’m pretty new to Xcode.
Is there a way to improve the auto complete results? Import a more complete dictionary or index maybe?
For instance, if I am looking for an array count and type [self.myArray it comes up with no completions found. if I leave the self off ([myArray ), it’ll return a whole list, including count. And if I try point notation (ie myArray.count) it never returns any results after the period.

I work in Visual Studio too, and compared to VS’ Auto Complete, Code Sense makes me cry.

It would just be a lot easier and faster to learn Objective C if I didn’t have to memorize the API before I could start.

Subscribe to new posts:

You'll get an email whenever a I publish a new post to my blog and nothing more. -- Corbin

As an Amazon Associate I earn from qualifying purchases.

(c) 2008-2024 Corbin Dunn

Privacy Policy

Subscribe to RSS feeds for entries.

78 queries. 0.144 seconds.

Log in