coderholic

Thoughts on Objective-C

Back in January I set myself the challenge of learning 12 new programming languages in 12 months. It's been a busy year so far though. I got married, had a baby daughter, and quit my job to work full time on my startup . As a result I've only written about one new language so far, clojure. I've not given up on the challenge though! Here's the second new language I've learnt this year: Objective-C.

Objective-C wasn't included in my original list of 12 programming languages, but I learnt it out of necessity when developing the Geomium iPhone application, and I'm now a huge fan! Below I describe parts of the language that initially tripped me up, the parts I'm not keen on, and the parts that I really like.

What caught me out

I first got stuck in to Objective-C by reading through the official documentation from Apple, which is fantastic. There were a few things that caught me out at the start though:

Class vs. Instance methods

When I first saw the + and - prefix on method declarations I thought it was a really neat way of specifying private or public methods. Not so. A + actually specifies that the method is a class method, equivalent to the static keyword in many other object oriented languages. Private methods don't exist in Objective-C.

self.property vs. property

Objective-C has some syntactic sugar to allow you to access properties as self.property rather than the usual [self property]. self.property and property aren't equivalent though - the former will call the property method, and the latter will access the property directly, which can sometimes lead to memory leaks, and did for me on a few occasions before I realised the difference!

Strings

I still get caught on this one from time to time. Objective-C strings must be defined as @"string" rather than "string". You won't get a complier error with the second type, but your app will likely crash!

What I don't like

Most of my issues with the language are to do with the lack of syntactic sugar. String concatenation, for example, is just painful:

NSString* concat = [NSString stringWithFormat:@"%@%@", @"first-string", @"second-string"];

Creating a dictionary is even worse (there are some slightly easier ways than this, but still):

NSArray* items = [NSArray arrayWithObjects: @"joined", @"hometown", @"likeToMeet", @"website", @"description", @"interests", nil];
NSArray* descriptions = [NSArray arrayWithObjects: @"Joined", @"Hometown", @"Like to meet", @"Website", @"Description", @"Interests", nil];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:descriptions forKeys:items];

What I love

So there are a few syntactic issues that I think could be improved with the language, but there are some features that I absolutely love!

Both static and dynamic typing

I really like the fact that you can pick and choose between static and dynamic typing. If you want to quickly try something you can do it all dynamically. Once you've decided on a suitable interface you can define all of your static types and benefit from compiler checks. Support for dynamic typing also lets you do some really cool stuff, like Key-Value coding on your classes.

Categories

Objective-C categories allow you to extend existing classes, just like monkey patching in Python or Ruby. It's a really powerful feature.

Method names

Objective-C methods don't have a named first argument, but require a name for all of the other arguments. It also takes all of the method argument names into account when determining which method to call. At first I thought this was a bit strange (and it seems I'm not alone), but this actually turns out to be really useful. You never need to lookup a method declaration to work out what argument X is supposed to be. It's right there in the call!

The fact that it takes the argument names into account allows you to group related methods together by name, and this is used to great effect by Apple with many of their delegate methods. For example, all of the UITableViewDelegate methods are simple called tableView, and then have additional arguments that determine the different behaviour.

Boolean constants

Most languages go for a variation on true and false. Objective-C has instead gone for YES and NO - a small touch, but I love it! :)

What about you?

There seems to be a great community of Objective-C programmers out there. When I've run into problems I've either found the answer in existing forum posts, or quickly got a response on StackOverflow. So to all of the Objective-C programmers out there, what parts of the language do you like, and which do you dislike? Let me know on twitter or in the comments below.

Posted on 13 Oct 2010
If you enjoyed reading this post you might want to follow @coderholic on twitter or browse though the full blog archive.