Software Development: C++ vs. Obj-C
Thursday, May 20, 2004
16 Comments
0 TrackBacks
Alexei enters the argument between C++ and Objective-C. Alexei puts himself firmly in the C++ camp, of all reasons, because of templates.
As pal and FSS partner Andy said to me just now, templates are a work-around for a problem that doesn't exist in Objective-C." Templates rarely work well: a function that can take a float is very difficult to write if it's also supposed to take a complex object, or a string, or whatever.
Objective-C has one main model for programming, objects. C++ has two.
One unified method or "way of doing things" is better than two. Why force a decision? Objective-C handles both cases within its "one main model," and it handles them well. Want a template-like example from Objective-C that doesn't require a break in methodology or a separate way of doing things?
- (id)description
Too easy? Too obvious? Sorry. Let's do another:
- (id)addThis:(id)thing1 andThis:(id)thing2;
That's it! Bam. Templates, complete with type variability and anything else you could want. Add the method above as a category on your root object and, by golly, you're home free aren't you? Fortunately, Objective-C developers don't find themselves going down that road very often.
Objective-C classes are introspective as Alexei notes, but he seems to underestimate the importance - and use of - methods like +poseAsClass: and the underused -respondsToSelector:. Templates? They're a pain in the ass - a virtual piece of bubble gum and bailing wire grafted onto the ailing wretches of C++. I like C++ - and used to do a lot of work - but I avoided templates like the plague. Not because I didn't "get it" or couldn't do it, but because, in the end, I found that they created more problems than they solves. Problems Objective-C doesn't have.
Anyway, I'm running out of steam. I haven't got much passion for these kinds of holy wars. Use whatever tool works for you, I say. However, two more points of discussion:
But C++ has another, far more powerful, programming model: generics (aka templates).
Oy. Objective-C has "id." And later…
Java only has single inheritance, but since it has strong type-checking, you can’t use Cocoa-style delgation either.
WebObjects uses delegates in pure Java. Only Apple seems to use the concept, but there it is. And Java 1.1? How about Java 1.4, which supports both (java.lang.Object and generics)? (Thanks to Andy here too: Java ain't my bag).
Today's diversion into one of the holy wars of computing is now over. For me, at least.
Related Articles (0):
TrackBacks to: http://nslog.com/mt/mt-tb.cgi/1763
Comments (16)
Great article. I never felt comfortable with templates but now I know what to tell friends who ask why Objective-C has no templates.
Now if you could write something about the syntax of method declaration in Objective-C (- (id)addThis:(id)thing1 andThis:(id)thing2;) so I can answer their "what's the use of this overly complicated labelled syntax if you can't even change argument order?", it would be nice :)
Posted by: Olivier on May 20, 2004 10:25 AMI think you missed my point (that's okay, because I didn't have one). I love Objective-C; I use it on a day-to-day basis far more frequently than I do C++, and I agree with you about the things that make it powerful and useful. But I think C++ is misunderstood; there's a beauty to C++'s complexity and seeming mismatch of disparate and confusing technologies that only rarely exposes itself, and I had a moment where it was apparent to me, so I thought I'd share. Now I'll go back to programming in useful languages that people understand
That said: If you think "generics" and "id" are at all the same thing, you've never really explored the potential of C++ templates. You can do far far more with them then you can with id. One of the coolest thing about templates is that you can use them to do very powerful computation and get a surprising amount of program logic decided at compile time. It can speed your program up significantly. Most people will never need this, but there it is.
And yes, Java can do delegation; it has to be able to, since its underlying object model is identical to Objective-C's. But as I said in my article, you can't do it within the confines of the Java language the way you can with Objective-C; you have to go to the reflection API to implement it. Apple did this for the WebObjects and Java-Cocoa APIs, but no one else has.
Posted by: Alexei Kosut on May 20, 2004 11:44 AMThe names arguments are part of the method name, that's why you can't change the order (you can change it when you're using an NSDictionary). The reason for named parameters is that
[obj makeNewWindowAtPositionX:0 y:1 height:25.1 width:77.2 style:2];
is much easier to read than
obj->makeNewWindow(0,1,25.1,77.2,2);
Posted by: andy on May 20, 2004 12:13 PMI use (and admire) Objective-C, but generics have several strong advantages, including compile-time syntax checking. Because templates can specify which types they can be applied to, the developer can't be surprised by somebody throwing an unhandled object into the pile all of a sudden, which (in pre-generics Java and in Objective-C) is likely to break comparator functions, etc. C++ templates are also, of course, more efficient; not only do you not have to spend a lot of time in your container class functions checking isMemberOf, since your type checking is done at compile time, you generate only the typed functions you need.
C++ has horrible, horrible syntax. But the benefits of putting up with it are significant.
Posted by: Howard Finnegan on May 20, 2004 12:27 PMAnd if you still want templates, just use Objective-C++ :D
Posted by: Kevin Ballard on May 20, 2004 12:47 PMTwo minor comments on the original article:
1. "...a function that can take a float is very difficult to write if it's also supposed to take a complex object, or a string, or whatever." The parameterized types of a templated function have implicit semantic expectations; if your function is difficult to write, it's probably because you're violating those expectations. Here's a simple (to the point of being trivial) example (caveat: I haven't coded in C++ in about 5 years, so I might be getting some details wrong): template <class T> T min(T a, T b) { return (b < a ? b : a; } This function has the implicit expectation that type T is a meaningful type for the < operator. Obviously, the primitive types (float et.al.) are. Most user-defined types (graphics objects, for example) won't be, so the compiler will report an error if you try to use this function with them. For user-defined types for which the < operator does make sense (such as a complex type), rather than go to great lengths in the function definition, overload the < operator for that type.
2. "How about Java 1.4, which supports both (java.lang.Object and generics)?" Generics are a feature of Java 1.5, not 1.4.
Posted by: Craig Cottingham on May 20, 2004 02:22 PMI haven't written anything significant in C++ and have never used templates, but based on what I've seen about the syntax, don't protocols cover the same ground in Objective-C?
- (id) somethingFromNothing: (id <Nothing>)object;
In this case, you can use any object that conforms to the "Nothing" protocol. Is that relevant here?
- Scott
Posted by: Scott Stevenson on May 20, 2004 10:33 PMWhat I'm curious about is how many major Cocoa applications use Objective-C exclusively. Is PulpFiction all Objective-C (other than parts like the SQLite library)? What about the big Apple apps? The Omni stuff?
Personally I'm not a big fan of some of the Foundation containers, so having C++ and the STL available is nice.
Posted by: Manton Reece on May 20, 2004 11:20 PMHey Scott,
Posted by: Josh Anon on May 20, 2004 11:24 PMI think a big part of templates is that they can take fundamental types (float, int, etc.), but in ObjC, only objects can conform to protocols.
PulpFiction is largley Obj-C. SQLite is not, of course, and we have some pieces here and there that aren't as well (libxml, libxslt). Rock Star dips down into C a few times as well. Sometimes it's orders of magnitude faster. We have, though, very little C++.
Posted by: Erik J. Barzeski on May 20, 2004 11:33 PMI'm switching from Windows to Mac and I'm not a programmer but I use VBasic. What should I learn on Mac to make small software? Obj-C?
Posted by: Ernesto on May 21, 2004 06:26 PMErnesto - RealBasic will feel most comfortable coming from Visual Basic, but I think you'll get more long term value out of Cocoa, particularly if you plan to build bigger apps in the future. Cocoa will also give you the most functionality "for free."
Cocoa Dev Central is a good place to start.
Posted by: Scott Stevenson on May 21, 2004 06:50 PMI can program for mac os x, linux, windows, bsd.... on C++ and well objective C I can't
Posted by: Will on June 23, 2004 04:29 PMWell, any reasonable programmer should know at least 4-5 programming languages...
Posted by: andy on June 23, 2004 06:46 PMWłasnie wruciłem z premiery "one Last Dance" i muszę stwierdzić że wilm jest kiepski a Swayze się zestarzał i ma strasznie przepity głos.....
Posted by: 5t453k on October 26, 2004 11:30 AMObjective-C++ has three models of programming ?
Posted by: Eduardo Ahumada on January 27, 2006 07:38 PMPost a Comment