Monday, February 23, 2009

Objective-C

I'm just starting to learn Objective-C so that I can develop an iPhone application. To learn it I'm reading Programming in Objective-C 2.0, online at Safari Books. The book states that Objective-C is patterned after Smalltalk and I'm quite familiar with Smalltalk programming, having developed in it professionally for years. I'm going to spew my first thoughts on this language here and it will be interesting to maybe look back and see how wrong I was.

In Objective-C (OC for shorthand) you send a message to an object like this:

[ receiver message ]

In Smalltalk you do it like this:

receiver message

Why the extra brackets for OC? Are parsers more primitive? The parser programmers more lazy?

Objective C seems to have primitive types, making the language a mixture of objects and non-objects. Smalltalk has no such mixture. Obective C requires the programmer to declare the type of the variable. Smalltalk does not. It seems to be that Objective C is closer to Java or C++ than Smalltalk.

OC seems to like extra syntax. For example, I see strings used like this:

    @"The fraction is"
I realize that this creates an NSString object and maybe that is more special than a regular String object (if one exists), but if not, what's the purpose of the @? I guess to denote the different type, but why would this book not use a simple string in the early examples? My guess is that this is the simple string and then I wonder why the heck is the @ used?

OC seems to carry over the royal pain in the butt separate header file from C++ and C. Forcing the programming to separate the interface from the implementation when we can have the compiler do this for us. Why force the programmer to do this extra work?

When defining a class in OC you have an interface section like this:

@interface Fraction: NSObject
{
int numerator;
int denominator;
}

-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;

@end
The part between the curly braces are the PRIVATE instance variables of the object. They have NOTHING to do with the interface of the object, yet they must be declared in the interface section?! Either this book is extremely bad or this language made a ridiculous choice here.

Also, notice how the arguments to the method setNumerator and setDenominator are declared. The type is specified with parens around them? Why add the extra syntax? Poor parsing again?

At first glance, there seems to be plenty to be annoyed with in Objective-C.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home