Five Things that Suck About Objective-C and Cocoa

April 5, 2007 at 11:59 pm 3 comments

Things have been quiet here in this blog. Too quiet. As such, I’m keeping the name-five-things-you-hate-about-a-language-you-like ball rolling, having seen it rolled with zest and vigor by brian d foy, Titus, Jacob Kaplan-Moss and Vincent.

Without further ado:

Five Things that Suck About Objective-C/Cocoa:

  1. Syntax for NSString literals. For the uninitiated, in Objective-C code enclosing "insomnia" in simple double-quotes creates a C-style char[] string; if you wish to use the far more powerful and versatile Objective-C NSString class, you must add an @ (making @"insomnia"). Backwards-compatibility with C is a good and useful thing, but why require more keystrokes to do the most commonly-used thing? I myself last weekend puzzled over a wonderfully non-specific “invalid reciever” error for a long time before realizing that I forgot an @ when sending strings to an arrayWithObjects: method. Aside from that, why use the @-sign as a prefix for NSStrings when it’s used in a plethora of other places, such as @interface, @implementation, @end and @selector? Though 90% of my @-key-presses in Textmate are prefixes to NSStrings, I can’t have a keypress of @ automatically expand to @”” – there are too many other things to do with the poor little @-sign. The oft-neglected | (pipe or vertical bar, I’ve heard both) character is far less disruptive to the flow of typing.
  2. reallyLongAndCamelCasedMethodNamesGetAnnoying. I refer specifically to the lovely NSWorkspace method openURLs: withAppBundleIdentifier: options: additionalEventParamDescriptor: launchIdentifiers:
    And ObjC method names can be concise yet informative – take for example NSString’s compare: options: range: locale:. I must admit, this complaint is not entirely valid, especially considering Textmate/XCode’s fancy code completion.
  3. No operator overloading. Come on, guys – why reject this crucial part of Smalltalk heritage? I, for one, am sick of writing objectAtIndex and objectForKey: as compared to Python’s []. Though Smalltalk allows one to define new operators, I’d be perfectly happy to settle for a few overloadable operators (string concatenation is desperately needed).
  4. Mysterious helper methods. I didn’t know of the existence of NSHomeDirectory(), NSTemporaryDirectory(), or NSClassFromString() until very recently. True, this is my fault, but I think that F-Script’s idea of storing all of these methods in a singleton System object is excellent, and much more in line with Objective-C’s Smalltalk heritage. (Actually, I have a half-finished ObjC class that makes NSBeep() and all those other miscellaneous C functions into class methods; if there’s any interest, I’ll finish and release it. I suppose that makes this complaint invalid. Oh well.)
  5. File management is a mess. Essential code is scattered throughout NSWorkspace (in all its brain-dead glory), NSFileManager, NSFileHandle, NSPipe, NSDirectoryEnumerator, and NSData – few things are as infuriating as hunting down the correct class that does exactly what I want. (Actually, no. Finding a better solution after thirty minutes of hacking around some perceived inadequacy is worse.

So there you have it. To be honest, it took me quite a while to write this, mainly because Objective-C is such a great language and Cocoa is such a great set of libraries. I suppose that the imperfections in a consistently useful and friendly toolkits stand out, and in retrospect I sort of feel guilty for my picky attitude. After all, it could be much, much worse.

Entry filed under: apple, cocoa, code, rant. Tags: .

This Is Worrying Cocoa Snippet: Detonate your Cursor

3 Comments

  • 1. Scott Stevenson  |  April 6, 2007 at 6:23 am

    1. I don’t think there’s any sane way to make NSStrings the default rather than a C string without breaking a lot of important code from the entire world. The reason @ is used is that it’s a standard way to Objective-C to offer something to gcc. All of the @ things are for the compiler.

    2. I think that mostly comes from your experience with Python. It might be a cultural thing, but when reading other’s people’s code, I’ll take more detailed message names over terse ones every time.

    3. The lack of operators overloading is, again, designed to simplify the sytax so that you have an easier time understanding code you didn’t write. I don’t feel as strongly about this as item 2, though.

    4. I agree.

    5. All of the classes you mention have very different responsibilities, but maybe that’s not clear from what you’ve been using them for. NSPipe, for example, is very much correct in being separate from the others. Same with NSData. What do you not like about NSWorkspace?

  • 2. Alex Gordon  |  April 10, 2007 at 6:00 pm

    1. I’m writing quite a large app ATM and I haven’t used standard C string constants (“blah”) at all. However to replace the @”” syntax with “” would be a big mistake. One on the main features of ObjC is that it is a strict superset of C (any C program is a valid ObjC program). If “” was used as an NSString constant then that would be broken.

    However, I agree about the @ sign for ObjC keywords. It’s just annoying.

    2. I use codesense so this isn’t a big problem for me. But, yeah, it they get insanely long and stupid sometimes.

    3. Yes, this is my biggest gripe with Objective-C (besides GC, but that’s sorted now)

    4. Yeah, it took me ages to find NSSelectorFromString() (again). This is easy to fix with a couple of classes and catagories though.

    5. I think the biggest problem here is that NSTask is just too low level. I don’t want to mess with file handles and pipes just to call a shell command and get it’s output as a string.

    I see no problem with NSData and NSFileManager though. They do what they say on the tin.

  • 3. Jon  |  December 7, 2008 at 6:23 pm

    I could not agree more. I have been using Objective C only recently and having had experience with a LOT of programming and scripting languages, Objective C is by far the most annoying to work with.

    I just can not understand why people find it fascinating. It is old and obsolete and frustrating to work with.


About Me



I'm Patrick Thomson. This was a blog about computer programming and computer science that I wrote in high school and college. I have since disavowed many of the views expressed on this site, but I'm keeping it around out of fondness.

If you like this, you might want to check out my Twitter or Tumblr, both of which are occasionally about code.

Blog Stats

  • 656,770 hits

%d bloggers like this: