Objective C Syntax. Black goats make the best sweaters.

I’ve been dabbling with Objective C with dreams of making the next great iPhone app. It is slow starting out. Objective C looks pretty Greek to me, even though I do have some C experience. It’s heavy with the symbol tax and is much lower level than C# or Java; no memory management, header files, pointer declarations. It’s painful, but I’m really starting to like it. It’s almost sick to like it, but there are a few features of the language that are really nice and however impossible, I would like to see them appear in the C# spec.

One feature that has been key in learning the language are the named parameters. Unlike C#, who is getting named parameters, in Objective C they are not optional. You have to include them otherwise it doesn’t know which method to call.

Before we look at an example, I want to stress I am not trained in Objective C, at all. This is only what I’ve collected from the few examples and code I’ve sifted through. Don’t repeat any of this to anyone, except to mock my ignorance if you do discover it. Verbatim regurgitation of this might embarrass yourself. I’m used to it, so I don’t mind.

On to an example.

[NSTimer scheduledTimerWithTimeInterval: 1.0
                                 target:self
                               selector:@selector(onFired:)
                               userInfo:nil
                                repeats:YES];

So in this line of code we make a static call to the type NSTimer. (As a side note, a lot of the core Cocoa classes are prefixed with “NS” as Cocoa is a derivation of NeXTSTEP.). Here we pass five arguments to a method, all obviously named. Compare this to comparable code in C#.

	NSTimer.scheduledTimerWithTimeInterval(1.0,this,onFired,null,true);

It’s obvious to me what the first one is doing with all of its arguments, but in C#, you have no idea. Take a more obvious example, but completely contrived example.

	setLabelStyle(blue,black,10,1,true,false,1.0);

What does that function do? Who knows right. It sets something blue, something black, something to 10, etc. The details of it are obscure. Now look at it in Objective C.

	[self setLabelColor:blue
	         background:black
	           fontSize:10
	        fontSpacing:1
	               bold:true
	            italics:false
	            opacity:1.0 ];

I changed the name of the method only to more closely follow how it would be named in Objective C. The style tends to run the method name into describing the first parameter, since that parameter doesn’t get its own name. You can see how much more explicit that is than in C#. It could be said it is a less dry because you have to repeat those names each time you invoke the method, but its clarity is worth the cost to me.

At first I wondered if since they were named, does that mean they are all optional and order is not important. Wrong! You have to list them and they have to be in that order. Take the same example in .Net 4.0 using named properties.

	setLabelStyle(Color:blue, bold:true, fontSize:10)

We can default the rest instead of overloading the method a bunch of times. Once we get into named properties, I might consider trying to enforce we always use them, especially when calling non-private methods. That might be a hard sell, but I really like them.

So the post was going to be subtitled “Black sheep make the best sweaters.”, but cashmere comes from goats and not sheep.

4 Comments so far

  1. bbum on November 2nd, 2009

    A bit of historical detail that may be illuminating.

    Objective-C doesn’t have named arguments, named parameters, or keyword arguments. As you noted, none of the bits of the method name are optional when calling said method.

    Instead, the method declaration and invocation syntax is referred to as “interleaved arguments”. This notion comes from SmallTalk.

    And, as you have noted, the key advantage to this approach to API design is that it tends to yield much more readable code. Wordy? Sure. Verbose? Absolutely. Long-winded, even? Totally. Comprehensible? YES!

    (Nice post, btw — I enjoy reading posts from folks who are discovering Objective-C.)

  2. mark on November 2nd, 2009

    @bbum Thanks! Objective-C looks really scary, but a lot of the design decisions that made it scary, start to make a lot of sense after I’ve been messing with it more and more. Next on the reading list is “Programming in Objective-C 2.0″ where I find out how terrible my apps are and that they leak like a screen door.

  3. bbum on November 2nd, 2009

    Give the Clang Static Analyzer (or… the “Clang Static Humiliator”, as I called it after the first time I tried it) a try. If you are on Snow Leopard, there should be a “Build and Analyzer” menu option somewhere….

    The analyzer knows a lot — not everything and there are some false positives — about standard coding practices, including memory management.

  4. mark on November 2nd, 2009

    @bblum Thanks! I’ll definitely check that out. I love tooling for static code analysis, especially in pseudo real-time like ReSharper does for C#.

Leave a Reply