Objective C and C++ verbosities compared

In my previous post I mentioned Steve Job’s choice of Objective C over C++ for development, based on his assertion that he wanted “to eliminate 80% of the code you have to write for you app”. To which Chris commented that in this older post, a piece of C++ code that I had converted to Objective C was actually twice as long as the original.

I probably should have gone into a bit more details in that initial remark on Objective C vs. C++, but that would have been rather off topic. Chris’s comment calls for more discussion, though.

The quick reply :

the C++ code I converted uses a simple STL-based data structure. The Objective C version uses Core Data.

There’s no question that, for simple operations, Core Data is more verbose than the STL equivalent. In the latter case you’re iterating over a simple container, in the former you’re actually querying a DB (an SQLite db, btw – yet another example of a well-reused piece of technology).

So, even though both codes do the same thing conceptually, the underlying technology is completely different. Yes, the Objc version is much longer, however… in the C++ case, there’s the whole definition of the STL data structure which is not shown in the example, and that you have to write. In the case of Core Data, well, there isn’t. You simply design the data model with Xcode’s Core Data builder :-). So the complete number of lines of code is actually smaller in the ObjC case.

The longer reply :

Core Data didn’t exist yet in at the time Jobs chose Objective C as the base language for NeXTStep, and Objective C does tend to be more verbose in its APIs than C++. The best way to demonstrate this is through a basic example of an array of ints :


// declare an array of ints
std::vector arrayOfInts;

// add one element

// get the element’s value
int i = arrayOfInts[0];

Objective C

// no array of ints, only arrays of NSObject* so :
NSMutableArray* arrayOfInts = [NSMutableArray array];

// add one element
[arrayOfInts addObject:[NSNumber numberWithInt:1]];

int i = [[arrayOfInts objectAtIndex:0] intValue];

If that reminds you of Java, you’re right. And thankfully, autoboxing is being added in Objective C (better late than never).

Edit Jan. 12th, 2013 : the above code would now be written as follows :

// no array of ints, only arrays of NSObject* so :
NSMutableArray* arrayOfInts = [NSMutableArray array];

// add one element
[arrayOfInts addObject:@1];

int i = arrayOfInts[0].intValue;

// or, even simpler :

NSArray* arrayOfInts = @[ @1 ];

int i = arrayOfInts[0].intValue;

end Edit

So why choose Objective C over C++ at the time ?

Because Objective C, as verbose are its basic APIs, is actually closer to Python than to C++. The object model is much more elaborate than in C++ (in which you have nothing else but virtual methods) : classes are first class objects, an object can be asked if it handles a method or not, you can add methods to a class at runtime and without having to derive it, and the language’s position toward type safety is much more relaxed – it’s essentially duck typing. You don’t have to declare a method in the interface to implement it, which is very convenient for internal methods. You do not have to declare a method that you override either, again simply implementing your classes’s own version is enough.

@interface MyClass : NSObject

// nothing


@implementation MyClass

// this overrides NSObject:init
– (id)init
// some code
[self moreInit]; // moreInit not declared in interface

– (void)moreInit
// some more code


In C++ that would be :

class MyClass : public Object
virtual void init();
virtual void moreInit();

void MyClass::init()
// some code

void MyClass::moreInit()
// some more code

All this makes of Objective C almost a scripted language in disguise. While in C++ you will spend a lot of time getting your types right, and any non-trivial refactoring will take a whole lot of time, Objective C lets you code much more freely.


(yes, this is an old topic, but I’m a slow blogger. Anyway…)

Of all the material that came under the spotlight shortly after Steve Jobs’s death, the most interesting one I’ve seen by far was his WWDC keynote from 1997 :

Let’s recap the situation : Apple is months away from bankruptcy, Gil Amelio is the current CEO, they’ve just bought NeXT and Steve Jobs has returned as “advisor”.

At the WWDC (that’s the Mac developers worldwide conference), Steve Jobs walks on stage and instead of doing a presentation, offers to take questions from the audience. And his answers have made me realise why the guy really was completely different from the other tech CEOs that run other IT companies.

Here’s a breakdown of the most interesting moments (time indications are approximative) :

4:00 – explains how he thinks that there’s a market for great products. Not “fancy products with an apple logo on them” – great products. Products that stand above the others in term of quality. All other companies do market studies, try to offer a variety of products tailored to each market segment… He wants Apple to do differently.

5:00 – “I know some of you worked on stuff that we put a bullet in the head of” : the way he acknowledges that is rather uncommon in my experience. Most would try to weasel around the issue and soften the blow. The reason he doesn’t is not because of his usual callous approach, it’s because he’s able to justify it with what comes next : “Focus is saying no”. This will echo to any developers who’s been involved in a project that has fallen to feature bloat. Raise your hand if you’ve met many managers with this kind of mindset. I haven’t.

10:00 – at this point he acknowledges that Apple should no longer reinvent everything, as they had done in the past. Pick the right elements (i.e. the Unix core technologies), figure out what they need to turn them into a product that is really better than the competition. And they did just that.

13:00 – “using computers not for computation intensive tasks, but as a window into communication intensive tasks” – coming from NeXT, he describes what his experience was using an OS which had the network built in from the start. A familiar vision to any Unix user, something very remote to Mac users at the time. In more ways than one, he also describes what cloud computing is aiming to bring to everybody now.

He also mentions gigabit ethernet, which will only be deployed 3 years later.

16:00 “what is really exciting to me is to look at that personal computer, and take out every moving part except the keyboard and the mouse”. That’s the Macbook Air, right there, which would be released 11 years later, in 2008. There was also the failed Sun/Oracle Network computer in between, but Apple pulled it off.

19:00 “Apple is vertically integrated – makes the hardware, the software, the marketing experience”. To this day, nobody else than Apple has this, and few still understand how fundamental a strength this is for them. So he got that while the PC world has the advantage of economies of scale, they can’t match Apple’s reactivity and ability to provide a much more seamless experience.

22:00 let’s not forget this is a developer convention – here he explains how cool the NeXTStep development platform is. Nothing special in itself, except that I don’t know of too many CEOs of IT companies who can convincingly sell a development environment to an audience of experienced developers. The part about “managing complexity” (at 25:00) really hits home.

41:00 “the way you get programmer productivity is not by increasing the number of lines of code per programmer per day. That doesn’t work. The way you get programmer productivity is by eliminating the lines of code you have to write. […] the goal here is to eliminate 80% of the code you have to write for your app”. Another thing that not too many tech managers get (although more do nowadays than back when this was recorded). That’s why he chose Objective C over C++.

01:01 About the Newton. “Most companies can be successful with 1 stack of system software. Rarely can they manage two and we are going to succeed at managing two during the next several years with MacOS and Rhapsody. I cannot imagine being successful at managing 3”. Let’s recap : this is still MacOS 9. Rhapsody, which will become Mac OS X, is in its infancy. So Apple will have to manage those two. The 3rd one is the Newton OS, therefore that will have to be shut down. Again, focus.

In a few years, once OS X is well established, they still will release the iPod, which did have its own (very simple) OS.

“Do you have a newton ?” asks a guy – He replies he bought one of the early ones, thought it was a piece of junk and threw it away, same with a Motorola Invoice. He grants that the new Newtons may be a lot better, the guy suggests he tries one, but he stops the argument with this : “the high-order bit is connectivity. It’s being in touch, connected to a network”. He then explains that using infrared to transfer data from your organizer to your computer when you get back is not what he wants. “If somebody would make a thing where you’re connected to the Net at all times… I’d love to buy one”.

Again, 1997. The Net is mostly accessed through modems. DSL is in its infancy. Wireless data access hardly exists at all. Yes, the concept itself is obvious, but at this point it’s clearly many years away… 10 years away, to be exact, when he took the stage at MacWorld and started with those words : “we’re gonna make some history today”. Others had implemented that concept before them (Treo, Blackberry), but they set the bar on how to do it.

Name one IT company which could see and plan 10 years ahead, and successfully achieve those plans. That’s focus.