Why? Why? [ ] Why? Why not . ???

ksignorini

Registered
Why on earth does Objective C use [someObject someMethod] to access an object's methods and properties instead of using standard C/C++ dot notation (someObject.someMethod)?

Kent!
 
Cause it's not C++/C? lol I dunno. Apple want's you to take the NeXTStep and Think different while programming! Yeah, that's it!
 
But, but, but....

ARGH!

I've done OOP in Pascal (yes, it can be done), C/C++, proprietary languages in the environmental controls industry, OOP type BASIC on multiple platforms, you name it, and all of them use dot notation. Yikes!

ARGH! (Oh, did I already say that?)

Kent!
 
I don't know, I kind of like the [ ] notation. It works doesn't it? :p
It keeps things pretty well under control too.

I guess it's just personal preference. I haven't ever seen anyone come up with reasons to not or to use [ ] :)

Is using . even work in obj-c? I saw docs on apples website with example code using . notation.
 
I hate the []'s too.

In theory, it's a pretty and clear way to separate things, whereas a string of .'s has no visual delineation of which result of which method is having the next method called on it.

The reason I HATE []'s though is that it requires you to figure out before hand how many levels of logic you're going to need on a line of code. In java or c++, you can just keep on adding method calls to the end of the line, whereas in obj-c you have to return to the front of the line to add another '[' each time.

I do like obj-C, but only because of what it allows me to do. On a personal level I'd like to kick the language in the junk.
 
It bothered me at first to, but you get use to it. It's not that hard, and although it maybe simpler to use dots, it is a lot clearer what's going on with brackets.

Every language has it's weirdness. Using dollar signs for variables in perl is really strange, and that whole -> reference thing is weird enough (was that a C++ thing? I can't remeber...).

Anyway, I really like the ability to make monsterous method names that make sense. Although sometimes I like java's way of having one big long convoluted string of characters. It just depends on the day.

My conclusion: use what you prefer. If it bothers you that much, use another language.

F-bacher
 
From http://www.dekorte.com/Objective-C/:

"Objective-C is a object oriented superset of C with a Smalltalk style (infix) message syntax. It was originally written by Brad Cox and the StepStone corppration in the early 1980s. In 1988, it was adopted as the development language for NeXTstep and was made a part of the GNU gcc compiler in 1992. It is currently used as the principle programming language for MacOSX (which is based on NeXTstep) and as the language for the GNUstep project on Linux and other platforms. Objective-C's weak typing and runtime features distinguish it from C++ and Java."

And...

"Objective-C was designed by Brad J. Cox, whose primary purpose was to add the main features of SmallTalk-80 to the C language. His work led to an object-oriented language, with a complete programming enviroment inspired by SmallTalk-80, even comprising a large part of the later's basic library. "
- Object Oriented Languages, Masini et all, Academic Press


Smalltalk uses a syntax like this:

object someMethod:arg another:arg2

Essentially Objective-C (which was conceived by Brad Cox) following this lead rather than the "dot syntax". However (I suspect for parsing reasons) they had to use the square brackets to delineate the calls. Someone else (I'm sure) knows the exact reason better than I...but the basic syntax comes from Smalltalk.

This FAQ http://www.cs.uu.nl/wais/html/na-dir/Objective-C/answers.html also has some good information.

[opinion begin];

I worked with Objective-C (on NEXTSTEP) for about 4-5 years, after which I turned to Java. Objective-C and the NEXTSTEP frameworks were (and to some extent, still are) head and shoulders above Java. There are SO many cool things that Java still does not have like "categories", real class objects, the syntax (I prefer it over the "dot syntax")...in fact many cool Java features were influenced by Objective-C (according to what I have read in the past anyway).

[opinion end];
 
Originally posted by Ghoser777
Anyway, I really like the ability to make monsterous method names that make sense. Although sometimes I like java's way of having one big long convoluted string of characters. It just depends on the day.

It can be nice. The problem, sometimes, is that with sensible method names, you need to remember not just what to put where (the order of the arguments) but also what the names of the arguments are.
 
all obj-c coders are mad, including myself. I do like the []-syntax, even though I agree that it is a bit clumsier.
 
In my opinion:

1) Brackets make code much more readable.
Using pseudocode:

[Color setRed:120 Green:100 Blue:210];

is much more understandable than

Color.getRGB(120,100,210);

2) Dots were not a standard when Obj-C was developed. Now they may be, but back then only C++ (which was under development too) used them. Nobody had heard anything about Java or OO-Basic-derivatives.

3) Of course there are likes and dislikes, but:

It took me 4 hours to do the classical

Hi! Whats your name?
>wagga
Hi wagga!

command line app in C++. I ceased to try learning. It took me 4 hours to do a calculator with Cocoa and Obj-C, having only previous knowledge of different flavours of oo-basic and java. Sure it may be uncomfortable if you come from c++, but for learning from the ground up, gee, Obj-C wins.
 
I just have not been able to get used to the [] notation. My eyes are just cannot correctly perceivw that construct. I found it easier to read
Color.getRGB(120,100,210);
Sorry stereoboy20 :)

But having, at least the option for, named arguments makes better code.

Color.setRGB( red:120, green:100, blue:210 );

The difference between -> and . should become moot. Parsers are intelligent enough to know when a component is retrieved from a reference or from a "value". I found I use the operators as overloads for stack-based wrappers (objects whose usage syntax like is a pointer to another). Obj-C has facilities to make this unnecessary.
 
Back
Top