I’ve been spending the past few weeks learning my way around the Cocoa framework. I used to develop software for the Macintosh, a long time ago (I started on a Macintosh Plus, which I upgraded to a whole 1MB of RAM, woo hoo!). Once I moved into financial market systems in the mid-1990’s, I stopped working with the machines. However, after having finished up what I could with BIDS Trading, and deciding I needed a change, I have returned to the Apple world, and have started to work on some ideas for the Macintosh and the iPhone.
After having spent the last 11 years using Java, and the past 4 working exclusively in Java, it has been “interesting” to return to a C-based language, specifically the Objective-C that is used for Mac and iPhone development. What I wanted to cover here was to revisit some of the issues that I remember from C and C++ that also apply to Objective-C. This is not me complaining about them. What I’m doing is reminding myself what I have to watch out for, and perhaps helping others at the same time.
An element of Objective-C that I’m not a huge fan of right now is the extra “typing” you have to do for routine work. For example, you have to declare the method signature in the header file, and then implement that method (including re-typing the method signature) in the implementation file. Yes, you can copy and paste (and I do that a lot), but only having to type the signature once would be nicer. If you want to change a method signature, you have to do it twice, once in the header and once in the implementation. This can lead to mistakes if the two don’t match. Unfortunately, because Objective-C is ultimately layered over top of C, this type of thing can’t be avoided.
Another thing to be aware of is something innocuous like assignments inside the “if” and “while” condition statement: don’t do it. I like that Java wouldn’t let you, and generally speaking I avoided it in my own C and C++ code. Sure, it makes the code visually tighter, but it doesn’t improve the code’s performance, makes it harder for others to read, and makes for mistakes. Keep the conditional part of the “if” or “while” a condition. Leave assignment to occur in the rest of the code.
Another issue that you have to pay a little more attention to is memory management. On the Mac, it is possible to use a garbage collector, but that facility doesn’t exist on the iPhone. The retain/release mechanism is reasonably easy to work with, but it takes discipline to remember to do it at all, and some thought to make sure you do it right. Release an object too often, and now you have a pointer to undefined memory. Miss a release statement and now you have a memory leak.
Referencing methods and data members is also something that can be a bit confusing at first. There are times where you use the “” approach like “[self variable:value]”, and there are times when you can use the “dot” notation, like “self.variable = value”. I’ve tended to use the “dot” notation for properties for the most part, and leave the “” notation for more complex method signatures.
Smaller gripes aside, overall I’ve found Objective-C reasonably nice to work with. There are parts of it, like the dynamic method dispatching and the way method calls “read”, that I quite like. Being able to augment a class and have your features available without having to create new subclasses is very cool. I’m looking forward to working with it for the next while.